Commit 0fd5de2d73deab7f8dd300e47aca03f0f6bca017

Authored by 谢华宁
1 parent add9317d

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

Showing 16 changed files with 40 additions and 43 deletions
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 *
... ... @@ -12,20 +10,10 @@ public interface FileApi {
12 10 /**
13 11 * 保存文件,并返回文件的访问路径
14 12 *
  13 + * @param originalName 原文件名称
15 14 * @param content 文件内容
16 15 * @return 文件路径
17 16 */
18   - default String createFile(byte[] content) throws Exception {
19   - return createFile(IdUtil.fastUUID(), content);
20   - }
21   -
22   - /**
23   - * 保存文件,并返回文件的访问路径
24   - *
25   - * @param path 文件路径
26   - * @param content 文件内容
27   - * @return 文件路径
28   - */
29   - String createFile(String path, byte[] content) throws Exception;
  17 + String createFile(String originalName, byte[] content) throws Exception;
30 18  
31 19 }
... ...
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 originalName, byte[] content) throws Exception {
  23 + return fileService.createFile(originalName, 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
... ... @@ -40,13 +40,11 @@ public class FileController {
40 40 @PostMapping("/upload")
41 41 @ApiOperation("上传文件")
42 42 @ApiImplicitParams({
43   - @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
44   - @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
  43 + @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class)
45 44 })
46 45 @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
47   - public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
48   - @RequestParam(value = "path", required = false) String path) throws Exception {
49   - return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
  46 + public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
  47 + return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream())));
50 48 }
51 49  
52 50 @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 originalName;
  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 originalName;
  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,11 @@ public interface FileService {
22 22 /**
23 23 * 保存文件,并返回文件的访问路径
24 24 *
25   - * @param path 文件路径
  25 + * @param originalName 原文件名称
26 26 * @param content 文件内容
27 27 * @return 文件路径
28 28 */
29   - String createFile(String path, byte[] content) throws Exception;
  29 + String createFile(String originalName, byte[] content) throws Exception;
30 30  
31 31 /**
32 32 * 删除文件
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
... ... @@ -37,12 +37,10 @@ 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 originalName, byte[] content) throws Exception {
41 41 // 计算默认的 path 名
42   - String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path);
43   - if (StrUtil.isEmpty(path)) {
44   - path = DigestUtil.md5Hex(content) + '.' + type;
45   - }
  42 + String type = FileTypeUtil.getType(new ByteArrayInputStream(content));
  43 + String path = DigestUtil.md5Hex(content) + '.' + type;
46 44  
47 45 // 上传到文件存储器
48 46 FileClient client = fileConfigService.getMasterFileClient();
... ... @@ -52,6 +50,7 @@ public class FileServiceImpl implements FileService {
52 50 // 保存到数据库
53 51 FileDO file = new FileDO();
54 52 file.setConfigId(client.getId());
  53 + file.setOriginalName(originalName);
55 54 file.setPath(path);
56 55 file.setUrl(url);
57 56 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 originalName = "单测文件名";
84 84 // 调用
85   - String result = fileService.createFile(path, content);
  85 + String result = fileService.createFile(originalName, content);
86 86 // 断言
87 87 assertEquals(result, url);
88 88 // 校验数据
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppUserController.java
... ... @@ -47,7 +47,7 @@ public class AppUserController {
47 47 if (file.isEmpty()) {
48 48 throw exception(FILE_IS_EMPTY);
49 49 }
50   - String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream());
  50 + String avatar = userService.updateUserAvatar(getLoginUserId(), file.getOriginalFilename(), file.getInputStream());
51 51 return success(avatar);
52 52 }
53 53  
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java
... ... @@ -57,10 +57,11 @@ public interface MemberUserService {
57 57 /**
58 58 * 修改用户头像
59 59 * @param userId 用户id
  60 + * @param originalName 原文件名
60 61 * @param inputStream 头像文件
61 62 * @return 头像url
62 63 */
63   - String updateUserAvatar(Long userId, InputStream inputStream) throws Exception;
  64 + String updateUserAvatar(Long userId, String originalName, InputStream inputStream) throws Exception;
64 65  
65 66 /**
66 67 * 修改手机
... ...
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java
... ... @@ -100,10 +100,10 @@ public class MemberUserServiceImpl implements MemberUserService {
100 100 }
101 101  
102 102 @Override
103   - public String updateUserAvatar(Long userId, InputStream avatarFile) throws Exception {
  103 + public String updateUserAvatar(Long userId, String originalName, InputStream avatarFile) throws Exception {
104 104 this.checkUserExists(userId);
105 105 // 创建文件
106   - String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile));
  106 + String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile));
107 107 // 更新头像路径
108 108 memberUserMapper.updateById(MemberUserDO.builder().id(userId).avatar(avatar).build());
109 109 return avatar;
... ...
yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java
... ... @@ -86,9 +86,10 @@ public class MemberUserServiceImplTest extends BaseDbAndRedisUnitTest {
86 86 ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
87 87 // mock 方法
88 88 String avatar = randomString();
89   - when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar);
  89 + String originalName = "单测文件名";
  90 + when(fileApi.createFile(originalName, eq(avatarFileBytes))).thenReturn(avatar);
90 91 // 调用
91   - String str = memberUserService.updateUserAvatar(userId, avatarFile);
  92 + String str = memberUserService.updateUserAvatar(userId, originalName, avatarFile);
92 93 // 断言
93 94 assertEquals(avatar, str);
94 95 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java
... ... @@ -97,11 +97,12 @@ public class UserProfileController {
97 97  
98 98 @PutMapping("/update-avatar")
99 99 @ApiOperation("上传用户个人头像")
100   - public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
  100 + public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file,
  101 + @RequestParam(value = "originalName", required = false) String originalName) throws Exception {
101 102 if (file.isEmpty()) {
102 103 throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
103 104 }
104   - String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream());
  105 + String avatar = userService.updateUserAvatar(getLoginUserId(), originalName, file.getInputStream());
105 106 return success(avatar);
106 107 }
107 108  
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java
... ... @@ -62,9 +62,10 @@ public interface AdminUserService {
62 62 * 更新用户头像
63 63 *
64 64 * @param id 用户 id
  65 + * @param originalName 原文件名称
65 66 * @param avatarFile 头像文件
66 67 */
67   - String updateUserAvatar(Long id, InputStream avatarFile) throws Exception;
  68 + String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception;
68 69  
69 70 /**
70 71 * 修改密码
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
... ... @@ -153,10 +153,10 @@ public class AdminUserServiceImpl implements AdminUserService {
153 153 }
154 154  
155 155 @Override
156   - public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception {
  156 + public String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception {
157 157 checkUserExists(id);
158 158 // 存储文件
159   - String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile));
  159 + String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile));
160 160 // 更新路径
161 161 AdminUserDO sysUserDO = new AdminUserDO();
162 162 sysUserDO.setId(id);
... ...
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java
... ... @@ -225,10 +225,11 @@ 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 + String originalName = "单测文件名";
  229 + when(fileApi.createFile(originalName, eq( avatarFileBytes))).thenReturn(avatar);
229 230  
230 231 // 调用
231   - userService.updateUserAvatar(userId, avatarFile);
  232 + userService.updateUserAvatar(userId, originalName, avatarFile);
232 233 // 断言
233 234 AdminUserDO user = userMapper.selectById(userId);
234 235 assertEquals(avatar, user.getAvatar());
... ...