Commit df7bba7f30c82c3e9191dc2e937b219576638c0c

Authored by 芋道源码
Committed by Gitee
2 parents 3276ad00 250db847

!186 文件表建加原文件名称字段original_name,相关代码修改

Merge pull request !186 from 谢谢的谢/master
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java
1 1 package cn.iocoder.yudao.module.infra.api.file;
2 2  
3   -import cn.hutool.core.util.IdUtil;
4   -
5 3 /**
6 4 * 文件 API 接口
7 5 *
... ... @@ -15,17 +13,29 @@ public interface FileApi {
15 13 * @param content 文件内容
16 14 * @return 文件路径
17 15 */
18   - default String createFile(byte[] content) throws Exception {
19   - return createFile(IdUtil.fastUUID(), content);
20   - }
  16 + default String createFile(byte[] content) throws Exception {
  17 + return createFile(null, null, content);
  18 + }
  19 +
  20 + /**
  21 + * 保存文件,并返回文件的访问路径
  22 + *
  23 + * @param path 文件路径
  24 + * @param content 文件内容
  25 + * @return 文件路径
  26 + */
  27 + default String createFile(String path, byte[] content) throws Exception {
  28 + return createFile(null, path, content);
  29 + }
21 30  
22 31 /**
23 32 * 保存文件,并返回文件的访问路径
24 33 *
  34 + * @param name 原文件名称
25 35 * @param path 文件路径
26 36 * @param content 文件内容
27 37 * @return 文件路径
28 38 */
29   - String createFile(String path, byte[] content) throws Exception;
  39 + String createFile(String name, String path, byte[] content) throws Exception;
30 40  
31 41 }
... ...
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 {
19 19 private FileService fileService;
20 20  
21 21 @Override
22   - public String createFile(String path, byte[] content) throws Exception {
23   - return fileService.createFile(path, content);
  22 + public String createFile(String name, String path, byte[] content) throws Exception {
  23 + return fileService.createFile(name, path, content);
24 24 }
25 25  
26 26 }
... ...
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 {
46 46 @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
47 47 public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
48 48 @RequestParam(value = "path", required = false) String path) throws Exception {
49   - return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
  49 + return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
50 50 }
51 51  
52 52 @DeleteMapping("/delete")
... ...
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 {
16 16 @ApiModelProperty(value = "文件路径", required = true, example = "yudao.jpg")
17 17 private String path;
18 18  
  19 + @ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg")
  20 + private String name;
  21 +
19 22 @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg")
20 23 private String url;
21 24  
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java
... ... @@ -34,6 +34,10 @@ public class FileDO extends BaseDO {
34 34 */
35 35 private Long configId;
36 36 /**
  37 + * 原文件名
  38 + */
  39 + private String name;
  40 + /**
37 41 * 路径,即文件名
38 42 */
39 43 private String path;
... ...
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 {
22 22 /**
23 23 * 保存文件,并返回文件的访问路径
24 24 *
  25 + * @param name 原文件名称
25 26 * @param path 文件路径
26 27 * @param content 文件内容
27 28 * @return 文件路径
28 29 */
29   - String createFile(String path, byte[] content) throws Exception;
  30 + String createFile(String name, String path, byte[] content) throws Exception;
30 31  
31 32 /**
32 33 * 删除文件
... ...
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 {
37 37 }
38 38  
39 39 @Override
40   - public String createFile(String path, byte[] content) throws Exception {
  40 + public String createFile(String name, String path, byte[] content) throws Exception {
41 41 // 计算默认的 path 名
42   - String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path);
  42 + String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
43 43 if (StrUtil.isEmpty(path)) {
44 44 path = DigestUtil.md5Hex(content) + '.' + type;
45 45 }
... ... @@ -52,6 +52,7 @@ public class FileServiceImpl implements FileService {
52 52 // 保存到数据库
53 53 FileDO file = new FileDO();
54 54 file.setConfigId(client.getId());
  55 + file.setName(name);
55 56 file.setPath(path);
56 57 file.setUrl(url);
57 58 file.setType(type);
... ...
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 {
80 80 String url = randomString();
81 81 when(client.upload(same(content), same(path))).thenReturn(url);
82 82 when(client.getId()).thenReturn(10L);
83   -
  83 + String name = "单测文件名";
84 84 // 调用
85   - String result = fileService.createFile(path, content);
  85 + String result = fileService.createFile(name, path, content);
86 86 // 断言
87 87 assertEquals(result, url);
88 88 // 校验数据
... ...
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 {
225 225 ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
226 226 // mock 方法
227 227 String avatar = randomString();
228   - when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar);
  228 + when(fileApi.createFile(eq( avatarFileBytes))).thenReturn(avatar);
229 229  
230 230 // 调用
231 231 userService.updateUserAvatar(userId, avatarFile);
... ...
yudao-ui-admin/src/views/infra/file/index.vue
... ... @@ -26,6 +26,7 @@
26 26  
27 27 <!-- 列表 -->
28 28 <el-table v-loading="loading" :data="list">
  29 + <el-table-column label="原文件名" align="center" prop="name" />
29 30 <el-table-column label="文件名" align="center" prop="path" />
30 31 <el-table-column label="URL" align="center" prop="url" />
31 32 <el-table-column label="文件大小" align="center" prop="size" width="120" :formatter="sizeFormat" />
... ... @@ -160,7 +161,7 @@ export default {
160 161 },
161 162 /** 处理上传的文件发生变化 */
162 163 handleFileChange(file, fileList) {
163   - this.upload.data.path = file.name;
  164 +
164 165 },
165 166 /** 处理文件上传中 */
166 167 handleFileUploadProgress(event, file, fileList) {
... ...