Commit 20411fa6b5cf6adc2555a5399f49e229f0583208

Authored by 谢华宁
1 parent 0fd5de2d

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

Showing 16 changed files with 37 additions and 31 deletions
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java
@@ -10,10 +10,20 @@ public interface FileApi { @@ -10,10 +10,20 @@ public interface FileApi {
10 /** 10 /**
11 * 保存文件,并返回文件的访问路径 11 * 保存文件,并返回文件的访问路径
12 * 12 *
13 - * @param originalName 原文件名称  
14 * @param content 文件内容 13 * @param content 文件内容
15 * @return 文件路径 14 * @return 文件路径
16 */ 15 */
17 - String createFile(String originalName, byte[] content) throws Exception; 16 + default String createFile(byte[] content) throws Exception {
  17 + return createFile(null, content);
  18 + }
  19 +
  20 + /**
  21 + * 保存文件,并返回文件的访问路径
  22 + *
  23 + * @param name 原文件名称
  24 + * @param content 文件内容
  25 + * @return 文件路径
  26 + */
  27 + String createFile(String name, byte[] content) throws Exception;
18 28
19 } 29 }
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,8 +19,8 @@ public class FileApiImpl implements FileApi {
19 private FileService fileService; 19 private FileService fileService;
20 20
21 @Override 21 @Override
22 - public String createFile(String originalName, byte[] content) throws Exception {  
23 - return fileService.createFile(originalName, content); 22 + public String createFile(String name, byte[] content) throws Exception {
  23 + return fileService.createFile(name, 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/vo/file/FileRespVO.java
@@ -17,7 +17,7 @@ public class FileRespVO { @@ -17,7 +17,7 @@ public class FileRespVO {
17 private String path; 17 private String path;
18 18
19 @ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg") 19 @ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg")
20 - private String originalName; 20 + private String name;
21 21
22 @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg") 22 @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg")
23 private String url; 23 private String url;
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java
@@ -36,7 +36,7 @@ public class FileDO extends BaseDO { @@ -36,7 +36,7 @@ public class FileDO extends BaseDO {
36 /** 36 /**
37 * 原文件名 37 * 原文件名
38 */ 38 */
39 - private String originalName; 39 + private String name;
40 /** 40 /**
41 * 路径,即文件名 41 * 路径,即文件名
42 */ 42 */
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,11 +22,11 @@ public interface FileService {
22 /** 22 /**
23 * 保存文件,并返回文件的访问路径 23 * 保存文件,并返回文件的访问路径
24 * 24 *
25 - * @param originalName 原文件名称 25 + * @param name 原文件名称
26 * @param content 文件内容 26 * @param content 文件内容
27 * @return 文件路径 27 * @return 文件路径
28 */ 28 */
29 - String createFile(String originalName, byte[] content) throws Exception; 29 + String createFile(String name, 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,9 +37,9 @@ public class FileServiceImpl implements FileService { @@ -37,9 +37,9 @@ public class FileServiceImpl implements FileService {
37 } 37 }
38 38
39 @Override 39 @Override
40 - public String createFile(String originalName, byte[] content) throws Exception { 40 + public String createFile(String name, byte[] content) throws Exception {
41 // 计算默认的 path 名 41 // 计算默认的 path 名
42 - String type = FileTypeUtil.getType(new ByteArrayInputStream(content)); 42 + String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
43 String path = DigestUtil.md5Hex(content) + '.' + type; 43 String path = DigestUtil.md5Hex(content) + '.' + type;
44 44
45 // 上传到文件存储器 45 // 上传到文件存储器
@@ -50,7 +50,7 @@ public class FileServiceImpl implements FileService { @@ -50,7 +50,7 @@ public class FileServiceImpl implements FileService {
50 // 保存到数据库 50 // 保存到数据库
51 FileDO file = new FileDO(); 51 FileDO file = new FileDO();
52 file.setConfigId(client.getId()); 52 file.setConfigId(client.getId());
53 - file.setOriginalName(originalName); 53 + file.setName(name);
54 file.setPath(path); 54 file.setPath(path);
55 file.setUrl(url); 55 file.setUrl(url);
56 file.setType(type); 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,9 +80,9 @@ public class FileServiceTest extends BaseDbUnitTest {
80 String url = randomString(); 80 String url = randomString();
81 when(client.upload(same(content), same(path))).thenReturn(url); 81 when(client.upload(same(content), same(path))).thenReturn(url);
82 when(client.getId()).thenReturn(10L); 82 when(client.getId()).thenReturn(10L);
83 - String originalName = "单测文件名"; 83 + String name = "单测文件名";
84 // 调用 84 // 调用
85 - String result = fileService.createFile(originalName, content); 85 + String result = fileService.createFile(name, content);
86 // 断言 86 // 断言
87 assertEquals(result, url); 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,7 +47,7 @@ public class AppUserController {
47 if (file.isEmpty()) { 47 if (file.isEmpty()) {
48 throw exception(FILE_IS_EMPTY); 48 throw exception(FILE_IS_EMPTY);
49 } 49 }
50 - String avatar = userService.updateUserAvatar(getLoginUserId(), file.getOriginalFilename(), file.getInputStream()); 50 + String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream());
51 return success(avatar); 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,11 +57,10 @@ public interface MemberUserService { @@ -57,11 +57,10 @@ public interface MemberUserService {
57 /** 57 /**
58 * 修改用户头像 58 * 修改用户头像
59 * @param userId 用户id 59 * @param userId 用户id
60 - * @param originalName 原文件名  
61 * @param inputStream 头像文件 60 * @param inputStream 头像文件
62 * @return 头像url 61 * @return 头像url
63 */ 62 */
64 - String updateUserAvatar(Long userId, String originalName, InputStream inputStream) throws Exception; 63 + String updateUserAvatar(Long userId, InputStream inputStream) throws Exception;
65 64
66 /** 65 /**
67 * 修改手机 66 * 修改手机
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,10 +100,10 @@ public class MemberUserServiceImpl implements MemberUserService {
100 } 100 }
101 101
102 @Override 102 @Override
103 - public String updateUserAvatar(Long userId, String originalName, InputStream avatarFile) throws Exception { 103 + public String updateUserAvatar(Long userId, InputStream avatarFile) throws Exception {
104 this.checkUserExists(userId); 104 this.checkUserExists(userId);
105 // 创建文件 105 // 创建文件
106 - String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile)); 106 + String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile));
107 // 更新头像路径 107 // 更新头像路径
108 memberUserMapper.updateById(MemberUserDO.builder().id(userId).avatar(avatar).build()); 108 memberUserMapper.updateById(MemberUserDO.builder().id(userId).avatar(avatar).build());
109 return avatar; 109 return avatar;
yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java
@@ -86,10 +86,9 @@ public class MemberUserServiceImplTest extends BaseDbAndRedisUnitTest { @@ -86,10 +86,9 @@ public class MemberUserServiceImplTest extends BaseDbAndRedisUnitTest {
86 ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); 86 ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
87 // mock 方法 87 // mock 方法
88 String avatar = randomString(); 88 String avatar = randomString();
89 - String originalName = "单测文件名";  
90 - when(fileApi.createFile(originalName, eq(avatarFileBytes))).thenReturn(avatar); 89 + when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar);
91 // 调用 90 // 调用
92 - String str = memberUserService.updateUserAvatar(userId, originalName, avatarFile); 91 + String str = memberUserService.updateUserAvatar(userId, avatarFile);
93 // 断言 92 // 断言
94 assertEquals(avatar, str); 93 assertEquals(avatar, str);
95 } 94 }
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java
@@ -97,12 +97,11 @@ public class UserProfileController { @@ -97,12 +97,11 @@ public class UserProfileController {
97 97
98 @PutMapping("/update-avatar") 98 @PutMapping("/update-avatar")
99 @ApiOperation("上传用户个人头像") 99 @ApiOperation("上传用户个人头像")
100 - public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file,  
101 - @RequestParam(value = "originalName", required = false) String originalName) throws Exception { 100 + public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
102 if (file.isEmpty()) { 101 if (file.isEmpty()) {
103 throw ServiceExceptionUtil.exception(FILE_IS_EMPTY); 102 throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
104 } 103 }
105 - String avatar = userService.updateUserAvatar(getLoginUserId(), originalName, file.getInputStream()); 104 + String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream());
106 return success(avatar); 105 return success(avatar);
107 } 106 }
108 107
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java
@@ -62,10 +62,9 @@ public interface AdminUserService { @@ -62,10 +62,9 @@ public interface AdminUserService {
62 * 更新用户头像 62 * 更新用户头像
63 * 63 *
64 * @param id 用户 id 64 * @param id 用户 id
65 - * @param originalName 原文件名称  
66 * @param avatarFile 头像文件 65 * @param avatarFile 头像文件
67 */ 66 */
68 - String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception; 67 + String updateUserAvatar(Long id, InputStream avatarFile) throws Exception;
69 68
70 /** 69 /**
71 * 修改密码 70 * 修改密码
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,10 +153,10 @@ public class AdminUserServiceImpl implements AdminUserService {
153 } 153 }
154 154
155 @Override 155 @Override
156 - public String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception { 156 + public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception {
157 checkUserExists(id); 157 checkUserExists(id);
158 // 存储文件 158 // 存储文件
159 - String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile)); 159 + String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile));
160 // 更新路径 160 // 更新路径
161 AdminUserDO sysUserDO = new AdminUserDO(); 161 AdminUserDO sysUserDO = new AdminUserDO();
162 sysUserDO.setId(id); 162 sysUserDO.setId(id);
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java
@@ -225,11 +225,10 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { @@ -225,11 +225,10 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
225 ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); 225 ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
226 // mock 方法 226 // mock 方法
227 String avatar = randomString(); 227 String avatar = randomString();
228 - String originalName = "单测文件名";  
229 - when(fileApi.createFile(originalName, eq( avatarFileBytes))).thenReturn(avatar); 228 + when(fileApi.createFile(eq( avatarFileBytes))).thenReturn(avatar);
230 229
231 // 调用 230 // 调用
232 - userService.updateUserAvatar(userId, originalName, avatarFile); 231 + userService.updateUserAvatar(userId, avatarFile);
233 // 断言 232 // 断言
234 AdminUserDO user = userMapper.selectById(userId); 233 AdminUserDO user = userMapper.selectById(userId);
235 assertEquals(avatar, user.getAvatar()); 234 assertEquals(avatar, user.getAvatar());
yudao-ui-admin/src/views/infra/file/index.vue
@@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
26 26
27 <!-- 列表 --> 27 <!-- 列表 -->
28 <el-table v-loading="loading" :data="list"> 28 <el-table v-loading="loading" :data="list">
  29 + <el-table-column label="原文件名" align="center" prop="name" />
29 <el-table-column label="文件名" align="center" prop="path" /> 30 <el-table-column label="文件名" align="center" prop="path" />
30 <el-table-column label="URL" align="center" prop="url" /> 31 <el-table-column label="URL" align="center" prop="url" />
31 <el-table-column label="文件大小" align="center" prop="size" width="120" :formatter="sizeFormat" /> 32 <el-table-column label="文件大小" align="center" prop="size" width="120" :formatter="sizeFormat" />