Преглед изворни кода

!186 文件表建加原文件名称字段original_name,相关代码修改
Merge pull request !186 from 谢谢的谢/master

芋道源码 пре 2 година
родитељ
комит
df7bba7f30

+ 16 - 6
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java

@@ -1,7 +1,5 @@
 package cn.iocoder.yudao.module.infra.api.file;
 
-import cn.hutool.core.util.IdUtil;
-
 /**
  * 文件 API 接口
  *
@@ -15,17 +13,29 @@ public interface FileApi {
      * @param content 文件内容
      * @return 文件路径
      */
-   default String createFile(byte[] content) throws Exception {
-       return createFile(IdUtil.fastUUID(), content);
-   }
+    default String createFile(byte[] content) throws Exception {
+        return createFile(null, null, content);
+    }
+
+    /**
+     * 保存文件,并返回文件的访问路径
+     *
+     * @param path 文件路径
+     * @param content 文件内容
+     * @return 文件路径
+     */
+    default String createFile(String path, byte[] content) throws Exception {
+        return createFile(null, path, content);
+    }
 
     /**
      * 保存文件,并返回文件的访问路径
      *
+     * @param name 原文件名称
      * @param path 文件路径
      * @param content 文件内容
      * @return 文件路径
      */
-    String createFile(String path, byte[] content) throws Exception;
+    String createFile(String name, String path, byte[] content) throws Exception;
 
 }

+ 2 - 2
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java

@@ -19,8 +19,8 @@ public class FileApiImpl implements FileApi {
     private FileService fileService;
 
     @Override
-    public String createFile(String path, byte[] content) throws Exception {
-        return fileService.createFile(path, content);
+    public String createFile(String name, String path, byte[] content) throws Exception {
+        return fileService.createFile(name, path, content);
     }
 
 }

+ 1 - 1
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java

@@ -46,7 +46,7 @@ public class FileController {
     @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
     public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
                                            @RequestParam(value = "path", required = false) String path) throws Exception {
-        return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
+        return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
     }
 
     @DeleteMapping("/delete")

+ 3 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java

@@ -16,6 +16,9 @@ public class FileRespVO {
     @ApiModelProperty(value = "文件路径", required = true, example = "yudao.jpg")
     private String path;
 
+    @ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg")
+    private String name;
+
     @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg")
     private String url;
 

+ 4 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java

@@ -33,6 +33,10 @@ public class FileDO extends BaseDO {
      * 关联 {@link FileConfigDO#getId()}
      */
     private Long configId;
+    /**
+     * 原文件名
+     */
+    private String name;
     /**
      * 路径,即文件名
      */

+ 2 - 1
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java

@@ -22,11 +22,12 @@ public interface FileService {
     /**
      * 保存文件,并返回文件的访问路径
      *
+     * @param name 原文件名称
      * @param path 文件路径
      * @param content 文件内容
      * @return 文件路径
      */
-    String createFile(String path, byte[] content) throws Exception;
+    String createFile(String name, String path, byte[] content) throws Exception;
 
     /**
      * 删除文件

+ 3 - 2
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java

@@ -37,9 +37,9 @@ public class FileServiceImpl implements FileService {
     }
 
     @Override
-    public String createFile(String path, byte[] content) throws Exception {
+    public String createFile(String name, String path, byte[] content) throws Exception {
         // 计算默认的 path 名
-        String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path);
+        String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
         if (StrUtil.isEmpty(path)) {
             path = DigestUtil.md5Hex(content) + '.' + type;
         }
@@ -52,6 +52,7 @@ public class FileServiceImpl implements FileService {
         // 保存到数据库
         FileDO file = new FileDO();
         file.setConfigId(client.getId());
+        file.setName(name);
         file.setPath(path);
         file.setUrl(url);
         file.setType(type);

+ 2 - 2
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java

@@ -80,9 +80,9 @@ public class FileServiceTest extends BaseDbUnitTest {
         String url = randomString();
         when(client.upload(same(content), same(path))).thenReturn(url);
         when(client.getId()).thenReturn(10L);
-
+        String name = "单测文件名";
         // 调用
-        String result = fileService.createFile(path, content);
+        String result = fileService.createFile(name, path, content);
         // 断言
         assertEquals(result, url);
         // 校验数据

+ 1 - 1
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java

@@ -225,7 +225,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
         ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
         // mock 方法
         String avatar = randomString();
-        when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar);
+        when(fileApi.createFile(eq( avatarFileBytes))).thenReturn(avatar);
 
         // 调用
         userService.updateUserAvatar(userId, avatarFile);

+ 2 - 1
yudao-ui-admin/src/views/infra/file/index.vue

@@ -26,6 +26,7 @@
 
     <!-- 列表 -->
     <el-table v-loading="loading" :data="list">
+      <el-table-column label="原文件名" align="center" prop="name" />
       <el-table-column label="文件名" align="center" prop="path" />
       <el-table-column label="URL" align="center" prop="url" />
       <el-table-column label="文件大小" align="center" prop="size" width="120" :formatter="sizeFormat" />
@@ -160,7 +161,7 @@ export default {
     },
     /** 处理上传的文件发生变化 */
     handleFileChange(file, fileList) {
-      this.upload.data.path = file.name;
+      
     },
     /** 处理文件上传中 */
     handleFileUploadProgress(event, file, fileList) {