From 0fd5de2d73deab7f8dd300e47aca03f0f6bca017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=8D=8E=E5=AE=81?= <670853534@qq.com> Date: Sun, 29 May 2022 16:19:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=A1=A8=E5=BB=BA?= =?UTF-8?q?=E5=8A=A0=E5=8E=9F=E6=96=87=E4=BB=B6=E5=90=8D=E7=A7=B0=E5=AD=97?= =?UTF-8?q?=E6=AE=B5original=5Fname=EF=BC=8C=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/module/infra/api/file/FileApi.java | 16 ++-------------- .../yudao/module/infra/api/file/FileApiImpl.java | 4 ++-- .../controller/admin/file/FileController.java | 8 +++----- .../admin/file/vo/file/FileRespVO.java | 3 +++ .../module/infra/dal/dataobject/file/FileDO.java | 4 ++++ .../module/infra/service/file/FileService.java | 4 ++-- .../infra/service/file/FileServiceImpl.java | 9 ++++----- .../infra/service/file/FileServiceTest.java | 4 ++-- .../controller/app/user/AppUserController.java | 2 +- .../member/service/user/MemberUserService.java | 3 ++- .../service/user/MemberUserServiceImpl.java | 4 ++-- .../service/user/MemberUserServiceImplTest.java | 5 +++-- .../admin/user/UserProfileController.java | 5 +++-- .../system/service/user/AdminUserService.java | 3 ++- .../service/user/AdminUserServiceImpl.java | 4 ++-- .../service/user/AdminUserServiceImplTest.java | 5 +++-- 16 files changed, 40 insertions(+), 43 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java index fa3ec5c6e..0fbce9200 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java +++ b/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 接口 * @@ -12,20 +10,10 @@ public interface FileApi { /** * 保存文件,并返回文件的访问路径 * + * @param originalName 原文件名称 * @param content 文件内容 * @return 文件路径 */ - default String createFile(byte[] content) throws Exception { - return createFile(IdUtil.fastUUID(), content); - } - - /** - * 保存文件,并返回文件的访问路径 - * - * @param path 文件路径 - * @param content 文件内容 - * @return 文件路径 - */ - String createFile(String path, byte[] content) throws Exception; + String createFile(String originalName, byte[] content) throws Exception; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java index d43eac1d5..1eb70688f 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java +++ b/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 originalName, byte[] content) throws Exception { + return fileService.createFile(originalName, content); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java index 4500133ed..90a415ebf 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java +++ b/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 { @PostMapping("/upload") @ApiOperation("上传文件") @ApiImplicitParams({ - @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), - @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) + @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class) }) @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 - public CommonResult uploadFile(@RequestParam("file") MultipartFile file, - @RequestParam(value = "path", required = false) String path) throws Exception { - return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); + public CommonResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception { + return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream()))); } @DeleteMapping("/delete") diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java index 8e39a7520..3a5c56553 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java +++ b/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 originalName; + @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg") private String url; diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java index 4ad155e94..11f1c2ddb 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java +++ b/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 originalName; /** * 路径,即文件名 */ diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java index 2d893c082..3805a7aba 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java +++ b/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 { /** * 保存文件,并返回文件的访问路径 * - * @param path 文件路径 + * @param originalName 原文件名称 * @param content 文件内容 * @return 文件路径 */ - String createFile(String path, byte[] content) throws Exception; + String createFile(String originalName, byte[] content) throws Exception; /** * 删除文件 diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index fc3eedbac..b9c31a5b4 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/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 { } @Override - public String createFile(String path, byte[] content) throws Exception { + public String createFile(String originalName, byte[] content) throws Exception { // 计算默认的 path 名 - String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path); - if (StrUtil.isEmpty(path)) { - path = DigestUtil.md5Hex(content) + '.' + type; - } + String type = FileTypeUtil.getType(new ByteArrayInputStream(content)); + String path = DigestUtil.md5Hex(content) + '.' + type; // 上传到文件存储器 FileClient client = fileConfigService.getMasterFileClient(); @@ -52,6 +50,7 @@ public class FileServiceImpl implements FileService { // 保存到数据库 FileDO file = new FileDO(); file.setConfigId(client.getId()); + file.setOriginalName(originalName); file.setPath(path); file.setUrl(url); file.setType(type); diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java index da377e79b..a1ae03972 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java +++ b/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 originalName = "单测文件名"; // 调用 - String result = fileService.createFile(path, content); + String result = fileService.createFile(originalName, content); // 断言 assertEquals(result, url); // 校验数据 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppUserController.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppUserController.java index edefcdba0..328b524cc 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppUserController.java +++ b/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 { if (file.isEmpty()) { throw exception(FILE_IS_EMPTY); } - String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream()); + String avatar = userService.updateUserAvatar(getLoginUserId(), file.getOriginalFilename(), file.getInputStream()); return success(avatar); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java index 3b63c8d71..b714adac2 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java +++ b/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 { /** * 修改用户头像 * @param userId 用户id + * @param originalName 原文件名 * @param inputStream 头像文件 * @return 头像url */ - String updateUserAvatar(Long userId, InputStream inputStream) throws Exception; + String updateUserAvatar(Long userId, String originalName, InputStream inputStream) throws Exception; /** * 修改手机 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java index ff91b0781..6278ab384 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java +++ b/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 { } @Override - public String updateUserAvatar(Long userId, InputStream avatarFile) throws Exception { + public String updateUserAvatar(Long userId, String originalName, InputStream avatarFile) throws Exception { this.checkUserExists(userId); // 创建文件 - String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile)); + String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile)); // 更新头像路径 memberUserMapper.updateById(MemberUserDO.builder().id(userId).avatar(avatar).build()); return avatar; diff --git a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java b/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java index 3077a84d9..26de8a8ed 100644 --- a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java +++ b/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 { ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); // mock 方法 String avatar = randomString(); - when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar); + String originalName = "单测文件名"; + when(fileApi.createFile(originalName, eq(avatarFileBytes))).thenReturn(avatar); // 调用 - String str = memberUserService.updateUserAvatar(userId, avatarFile); + String str = memberUserService.updateUserAvatar(userId, originalName, avatarFile); // 断言 assertEquals(avatar, str); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java index 3cc3a44dd..b02130cc5 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java +++ b/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 { @PutMapping("/update-avatar") @ApiOperation("上传用户个人头像") - public CommonResult updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception { + public CommonResult updateUserAvatar(@RequestParam("avatarFile") MultipartFile file, + @RequestParam(value = "originalName", required = false) String originalName) throws Exception { if (file.isEmpty()) { throw ServiceExceptionUtil.exception(FILE_IS_EMPTY); } - String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream()); + String avatar = userService.updateUserAvatar(getLoginUserId(), originalName, file.getInputStream()); return success(avatar); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java index ae3245de2..4573a8849 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java +++ b/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 { * 更新用户头像 * * @param id 用户 id + * @param originalName 原文件名称 * @param avatarFile 头像文件 */ - String updateUserAvatar(Long id, InputStream avatarFile) throws Exception; + String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception; /** * 修改密码 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java index 57fd49a7d..ca9f8f161 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java +++ b/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 { } @Override - public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception { + public String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception { checkUserExists(id); // 存储文件 - String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile)); + String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile)); // 更新路径 AdminUserDO sysUserDO = new AdminUserDO(); sysUserDO.setId(id); diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java index 4bacb2657..d0023c639 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java +++ b/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 { ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); // mock 方法 String avatar = randomString(); - when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar); + String originalName = "单测文件名"; + when(fileApi.createFile(originalName, eq( avatarFileBytes))).thenReturn(avatar); // 调用 - userService.updateUserAvatar(userId, avatarFile); + userService.updateUserAvatar(userId, originalName, avatarFile); // 断言 AdminUserDO user = userMapper.selectById(userId); assertEquals(avatar, user.getAvatar()); From 20411fa6b5cf6adc2555a5399f49e229f0583208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=8D=8E=E5=AE=81?= <670853534@qq.com> Date: Sun, 29 May 2022 22:40:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=A1=A8=E5=BB=BA?= =?UTF-8?q?=E5=8A=A0=E5=8E=9F=E6=96=87=E4=BB=B6=E5=90=8D=E7=A7=B0=E5=AD=97?= =?UTF-8?q?=E6=AE=B5name=EF=BC=8C=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/module/infra/api/file/FileApi.java | 14 ++++++++++++-- .../yudao/module/infra/api/file/FileApiImpl.java | 4 ++-- .../controller/admin/file/vo/file/FileRespVO.java | 2 +- .../module/infra/dal/dataobject/file/FileDO.java | 2 +- .../module/infra/service/file/FileService.java | 4 ++-- .../module/infra/service/file/FileServiceImpl.java | 6 +++--- .../module/infra/service/file/FileServiceTest.java | 4 ++-- .../controller/app/user/AppUserController.java | 2 +- .../member/service/user/MemberUserService.java | 3 +-- .../member/service/user/MemberUserServiceImpl.java | 4 ++-- .../service/user/MemberUserServiceImplTest.java | 5 ++--- .../admin/user/UserProfileController.java | 5 ++--- .../system/service/user/AdminUserService.java | 3 +-- .../system/service/user/AdminUserServiceImpl.java | 4 ++-- .../service/user/AdminUserServiceImplTest.java | 5 ++--- yudao-ui-admin/src/views/infra/file/index.vue | 1 + 16 files changed, 37 insertions(+), 31 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java index 0fbce9200..d2962ccbe 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java +++ b/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 { /** * 保存文件,并返回文件的访问路径 * - * @param originalName 原文件名称 * @param content 文件内容 * @return 文件路径 */ - String createFile(String originalName, byte[] content) throws Exception; + default String createFile(byte[] content) throws Exception { + return createFile(null, content); + } + + /** + * 保存文件,并返回文件的访问路径 + * + * @param name 原文件名称 + * @param content 文件内容 + * @return 文件路径 + */ + String createFile(String name, byte[] content) throws Exception; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java index 1eb70688f..0762b6161 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java +++ b/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 originalName, byte[] content) throws Exception { - return fileService.createFile(originalName, content); + public String createFile(String name, byte[] content) throws Exception { + return fileService.createFile(name, content); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java index 3a5c56553..10737693d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java +++ b/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 { private String path; @ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg") - private String originalName; + private String name; @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg") private String url; diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java index 11f1c2ddb..7e81280da 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java +++ b/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 { /** * 原文件名 */ - private String originalName; + private String name; /** * 路径,即文件名 */ diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java index 3805a7aba..382814824 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java +++ b/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 { /** * 保存文件,并返回文件的访问路径 * - * @param originalName 原文件名称 + * @param name 原文件名称 * @param content 文件内容 * @return 文件路径 */ - String createFile(String originalName, byte[] content) throws Exception; + String createFile(String name, byte[] content) throws Exception; /** * 删除文件 diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index b9c31a5b4..78570879a 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/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 originalName, byte[] content) throws Exception { + public String createFile(String name, byte[] content) throws Exception { // 计算默认的 path 名 - String type = FileTypeUtil.getType(new ByteArrayInputStream(content)); + String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name); String path = DigestUtil.md5Hex(content) + '.' + type; // 上传到文件存储器 @@ -50,7 +50,7 @@ public class FileServiceImpl implements FileService { // 保存到数据库 FileDO file = new FileDO(); file.setConfigId(client.getId()); - file.setOriginalName(originalName); + file.setName(name); file.setPath(path); file.setUrl(url); file.setType(type); diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java index a1ae03972..73b1499aa 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java +++ b/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 originalName = "单测文件名"; + String name = "单测文件名"; // 调用 - String result = fileService.createFile(originalName, content); + String result = fileService.createFile(name, content); // 断言 assertEquals(result, url); // 校验数据 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppUserController.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppUserController.java index 328b524cc..edefcdba0 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/user/AppUserController.java +++ b/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 { if (file.isEmpty()) { throw exception(FILE_IS_EMPTY); } - String avatar = userService.updateUserAvatar(getLoginUserId(), file.getOriginalFilename(), file.getInputStream()); + String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream()); return success(avatar); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java index b714adac2..3b63c8d71 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java +++ b/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 { /** * 修改用户头像 * @param userId 用户id - * @param originalName 原文件名 * @param inputStream 头像文件 * @return 头像url */ - String updateUserAvatar(Long userId, String originalName, InputStream inputStream) throws Exception; + String updateUserAvatar(Long userId, InputStream inputStream) throws Exception; /** * 修改手机 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java index 6278ab384..ff91b0781 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java +++ b/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 { } @Override - public String updateUserAvatar(Long userId, String originalName, InputStream avatarFile) throws Exception { + public String updateUserAvatar(Long userId, InputStream avatarFile) throws Exception { this.checkUserExists(userId); // 创建文件 - String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile)); + String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile)); // 更新头像路径 memberUserMapper.updateById(MemberUserDO.builder().id(userId).avatar(avatar).build()); return avatar; diff --git a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java b/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java index 26de8a8ed..3077a84d9 100644 --- a/yudao-module-member/yudao-module-member-biz/src/test/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImplTest.java +++ b/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 { ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); // mock 方法 String avatar = randomString(); - String originalName = "单测文件名"; - when(fileApi.createFile(originalName, eq(avatarFileBytes))).thenReturn(avatar); + when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar); // 调用 - String str = memberUserService.updateUserAvatar(userId, originalName, avatarFile); + String str = memberUserService.updateUserAvatar(userId, avatarFile); // 断言 assertEquals(avatar, str); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java index b02130cc5..3cc3a44dd 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java +++ b/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 { @PutMapping("/update-avatar") @ApiOperation("上传用户个人头像") - public CommonResult updateUserAvatar(@RequestParam("avatarFile") MultipartFile file, - @RequestParam(value = "originalName", required = false) String originalName) throws Exception { + public CommonResult updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception { if (file.isEmpty()) { throw ServiceExceptionUtil.exception(FILE_IS_EMPTY); } - String avatar = userService.updateUserAvatar(getLoginUserId(), originalName, file.getInputStream()); + String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream()); return success(avatar); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java index 4573a8849..ae3245de2 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java +++ b/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 { * 更新用户头像 * * @param id 用户 id - * @param originalName 原文件名称 * @param avatarFile 头像文件 */ - String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception; + String updateUserAvatar(Long id, InputStream avatarFile) throws Exception; /** * 修改密码 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java index ca9f8f161..57fd49a7d 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java +++ b/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 { } @Override - public String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception { + public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception { checkUserExists(id); // 存储文件 - String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile)); + String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile)); // 更新路径 AdminUserDO sysUserDO = new AdminUserDO(); sysUserDO.setId(id); diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java index d0023c639..39f4c0152 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java +++ b/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 { ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); // mock 方法 String avatar = randomString(); - String originalName = "单测文件名"; - when(fileApi.createFile(originalName, eq( avatarFileBytes))).thenReturn(avatar); + when(fileApi.createFile(eq( avatarFileBytes))).thenReturn(avatar); // 调用 - userService.updateUserAvatar(userId, originalName, avatarFile); + userService.updateUserAvatar(userId, avatarFile); // 断言 AdminUserDO user = userMapper.selectById(userId); assertEquals(avatar, user.getAvatar()); diff --git a/yudao-ui-admin/src/views/infra/file/index.vue b/yudao-ui-admin/src/views/infra/file/index.vue index 0b02a3dae..2242e7409 100644 --- a/yudao-ui-admin/src/views/infra/file/index.vue +++ b/yudao-ui-admin/src/views/infra/file/index.vue @@ -26,6 +26,7 @@ + From 250db847f6da7c8ab8faea8d4a90f2ebf26324fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=8D=8E=E5=AE=81?= <670853534@qq.com> Date: Tue, 31 May 2022 22:26:26 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=9D=E7=95=99path=E5=8F=82=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E6=96=B9=E4=BE=BF=E8=A6=86=E7=9B=96=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/module/infra/api/file/FileApi.java | 16 ++++++++++++++-- .../yudao/module/infra/api/file/FileApiImpl.java | 4 ++-- .../controller/admin/file/FileController.java | 8 +++++--- .../module/infra/service/file/FileService.java | 3 ++- .../infra/service/file/FileServiceImpl.java | 6 ++++-- .../infra/service/file/FileServiceTest.java | 2 +- yudao-ui-admin/src/views/infra/file/index.vue | 2 +- 7 files changed, 29 insertions(+), 12 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java index d2962ccbe..39a0ed2a5 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java @@ -14,16 +14,28 @@ public interface FileApi { * @return 文件路径 */ default String createFile(byte[] content) throws Exception { - return createFile(null, content); + 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 name, byte[] content) throws Exception; + String createFile(String name, String path, byte[] content) throws Exception; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java index 0762b6161..344d7c10d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java +++ b/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 name, byte[] content) throws Exception { - return fileService.createFile(name, content); + public String createFile(String name, String path, byte[] content) throws Exception { + return fileService.createFile(name, path, content); } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java index 90a415ebf..fcdca025f 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java @@ -40,11 +40,13 @@ public class FileController { @PostMapping("/upload") @ApiOperation("上传文件") @ApiImplicitParams({ - @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class) + @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), + @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) }) @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 - public CommonResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception { - return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream()))); + public CommonResult uploadFile(@RequestParam("file") MultipartFile file, + @RequestParam(value = "path", required = false) String path) throws Exception { + return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()))); } @DeleteMapping("/delete") diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java index 382814824..a153c8ba2 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java @@ -23,10 +23,11 @@ public interface FileService { * 保存文件,并返回文件的访问路径 * * @param name 原文件名称 + * @param path 文件路径 * @param content 文件内容 * @return 文件路径 */ - String createFile(String name, byte[] content) throws Exception; + String createFile(String name, String path, byte[] content) throws Exception; /** * 删除文件 diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index 78570879a..09f75b6c5 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java @@ -37,10 +37,12 @@ public class FileServiceImpl implements FileService { } @Override - public String createFile(String name, byte[] content) throws Exception { + public String createFile(String name, String path, byte[] content) throws Exception { // 计算默认的 path 名 String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name); - String path = DigestUtil.md5Hex(content) + '.' + type; + if (StrUtil.isEmpty(path)) { + path = DigestUtil.md5Hex(content) + '.' + type; + } // 上传到文件存储器 FileClient client = fileConfigService.getMasterFileClient(); diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java index 73b1499aa..e61039385 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java @@ -82,7 +82,7 @@ public class FileServiceTest extends BaseDbUnitTest { when(client.getId()).thenReturn(10L); String name = "单测文件名"; // 调用 - String result = fileService.createFile(name, content); + String result = fileService.createFile(name, path, content); // 断言 assertEquals(result, url); // 校验数据 diff --git a/yudao-ui-admin/src/views/infra/file/index.vue b/yudao-ui-admin/src/views/infra/file/index.vue index 2242e7409..45cf819c9 100644 --- a/yudao-ui-admin/src/views/infra/file/index.vue +++ b/yudao-ui-admin/src/views/infra/file/index.vue @@ -161,7 +161,7 @@ export default { }, /** 处理上传的文件发生变化 */ handleFileChange(file, fileList) { - this.upload.data.path = file.name; + }, /** 处理文件上传中 */ handleFileUploadProgress(event, file, fileList) {