Commit 0fd5de2d73deab7f8dd300e47aca03f0f6bca017
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 | package cn.iocoder.yudao.module.infra.api.file; | 1 | package cn.iocoder.yudao.module.infra.api.file; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.IdUtil; | ||
| 4 | - | ||
| 5 | /** | 3 | /** |
| 6 | * 文件 API 接口 | 4 | * 文件 API 接口 |
| 7 | * | 5 | * |
| @@ -12,20 +10,10 @@ public interface FileApi { | @@ -12,20 +10,10 @@ public interface FileApi { | ||
| 12 | /** | 10 | /** |
| 13 | * 保存文件,并返回文件的访问路径 | 11 | * 保存文件,并返回文件的访问路径 |
| 14 | * | 12 | * |
| 13 | + * @param originalName 原文件名称 | ||
| 15 | * @param content 文件内容 | 14 | * @param content 文件内容 |
| 16 | * @return 文件路径 | 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,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 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,13 +40,11 @@ public class FileController { | ||
| 40 | @PostMapping("/upload") | 40 | @PostMapping("/upload") |
| 41 | @ApiOperation("上传文件") | 41 | @ApiOperation("上传文件") |
| 42 | @ApiImplicitParams({ | 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 | @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 | 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 | @DeleteMapping("/delete") | 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,6 +16,9 @@ public class FileRespVO { | ||
| 16 | @ApiModelProperty(value = "文件路径", required = true, example = "yudao.jpg") | 16 | @ApiModelProperty(value = "文件路径", required = true, example = "yudao.jpg") |
| 17 | private String path; | 17 | private String path; |
| 18 | 18 | ||
| 19 | + @ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg") | ||
| 20 | + private String originalName; | ||
| 21 | + | ||
| 19 | @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") |
| 20 | private String url; | 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,6 +34,10 @@ public class FileDO extends BaseDO { | ||
| 34 | */ | 34 | */ |
| 35 | private Long configId; | 35 | private Long configId; |
| 36 | /** | 36 | /** |
| 37 | + * 原文件名 | ||
| 38 | + */ | ||
| 39 | + private String originalName; | ||
| 40 | + /** | ||
| 37 | * 路径,即文件名 | 41 | * 路径,即文件名 |
| 38 | */ | 42 | */ |
| 39 | private String path; | 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,11 +22,11 @@ public interface FileService { | ||
| 22 | /** | 22 | /** |
| 23 | * 保存文件,并返回文件的访问路径 | 23 | * 保存文件,并返回文件的访问路径 |
| 24 | * | 24 | * |
| 25 | - * @param path 文件路径 | 25 | + * @param originalName 原文件名称 |
| 26 | * @param content 文件内容 | 26 | * @param content 文件内容 |
| 27 | * @return 文件路径 | 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,12 +37,10 @@ public class FileServiceImpl implements FileService { | ||
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | @Override | 39 | @Override |
| 40 | - public String createFile(String path, byte[] content) throws Exception { | 40 | + public String createFile(String originalName, byte[] content) throws Exception { |
| 41 | // 计算默认的 path 名 | 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 | FileClient client = fileConfigService.getMasterFileClient(); | 46 | FileClient client = fileConfigService.getMasterFileClient(); |
| @@ -52,6 +50,7 @@ public class FileServiceImpl implements FileService { | @@ -52,6 +50,7 @@ public class FileServiceImpl implements FileService { | ||
| 52 | // 保存到数据库 | 50 | // 保存到数据库 |
| 53 | FileDO file = new FileDO(); | 51 | FileDO file = new FileDO(); |
| 54 | file.setConfigId(client.getId()); | 52 | file.setConfigId(client.getId()); |
| 53 | + file.setOriginalName(originalName); | ||
| 55 | file.setPath(path); | 54 | file.setPath(path); |
| 56 | file.setUrl(url); | 55 | file.setUrl(url); |
| 57 | 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 | - | 83 | + String originalName = "单测文件名"; |
| 84 | // 调用 | 84 | // 调用 |
| 85 | - String result = fileService.createFile(path, content); | 85 | + String result = fileService.createFile(originalName, 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.getInputStream()); | 50 | + String avatar = userService.updateUserAvatar(getLoginUserId(), file.getOriginalFilename(), 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,10 +57,11 @@ public interface MemberUserService { | @@ -57,10 +57,11 @@ public interface MemberUserService { | ||
| 57 | /** | 57 | /** |
| 58 | * 修改用户头像 | 58 | * 修改用户头像 |
| 59 | * @param userId 用户id | 59 | * @param userId 用户id |
| 60 | + * @param originalName 原文件名 | ||
| 60 | * @param inputStream 头像文件 | 61 | * @param inputStream 头像文件 |
| 61 | * @return 头像url | 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,10 +100,10 @@ public class MemberUserServiceImpl implements MemberUserService { | ||
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | @Override | 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 | this.checkUserExists(userId); | 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 | 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,9 +86,10 @@ public class MemberUserServiceImplTest extends BaseDbAndRedisUnitTest { | @@ -86,9 +86,10 @@ 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 | - 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 | assertEquals(avatar, str); | 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,11 +97,12 @@ 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) throws Exception { | 100 | + public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file, |
| 101 | + @RequestParam(value = "originalName", required = false) String originalName) throws Exception { | ||
| 101 | if (file.isEmpty()) { | 102 | if (file.isEmpty()) { |
| 102 | throw ServiceExceptionUtil.exception(FILE_IS_EMPTY); | 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 | return success(avatar); | 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,9 +62,10 @@ public interface AdminUserService { | ||
| 62 | * 更新用户头像 | 62 | * 更新用户头像 |
| 63 | * | 63 | * |
| 64 | * @param id 用户 id | 64 | * @param id 用户 id |
| 65 | + * @param originalName 原文件名称 | ||
| 65 | * @param avatarFile 头像文件 | 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,10 +153,10 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | @Override | 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 | checkUserExists(id); | 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 | 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,10 +225,11 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { | @@ -225,10 +225,11 @@ 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 | - 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 | AdminUserDO user = userMapper.selectById(userId); | 234 | AdminUserDO user = userMapper.selectById(userId); |
| 234 | assertEquals(avatar, user.getAvatar()); | 235 | assertEquals(avatar, user.getAvatar()); |