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 @@ +