Commit df7bba7f30c82c3e9191dc2e937b219576638c0c
Committed by
Gitee
!186 文件表建加原文件名称字段original_name,相关代码修改
Merge pull request !186 from 谢谢的谢/master
Showing
10 changed files
with
36 additions
and
16 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 | * |
| @@ -15,17 +13,29 @@ public interface FileApi { | @@ -15,17 +13,29 @@ public interface FileApi { | ||
| 15 | * @param content 文件内容 | 13 | * @param content 文件内容 |
| 16 | * @return 文件路径 | 14 | * @return 文件路径 |
| 17 | */ | 15 | */ |
| 18 | - default String createFile(byte[] content) throws Exception { | ||
| 19 | - return createFile(IdUtil.fastUUID(), content); | ||
| 20 | - } | 16 | + default String createFile(byte[] content) throws Exception { |
| 17 | + return createFile(null, null, content); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 保存文件,并返回文件的访问路径 | ||
| 22 | + * | ||
| 23 | + * @param path 文件路径 | ||
| 24 | + * @param content 文件内容 | ||
| 25 | + * @return 文件路径 | ||
| 26 | + */ | ||
| 27 | + default String createFile(String path, byte[] content) throws Exception { | ||
| 28 | + return createFile(null, path, content); | ||
| 29 | + } | ||
| 21 | 30 | ||
| 22 | /** | 31 | /** |
| 23 | * 保存文件,并返回文件的访问路径 | 32 | * 保存文件,并返回文件的访问路径 |
| 24 | * | 33 | * |
| 34 | + * @param name 原文件名称 | ||
| 25 | * @param path 文件路径 | 35 | * @param path 文件路径 |
| 26 | * @param content 文件内容 | 36 | * @param content 文件内容 |
| 27 | * @return 文件路径 | 37 | * @return 文件路径 |
| 28 | */ | 38 | */ |
| 29 | - String createFile(String path, byte[] content) throws Exception; | 39 | + String createFile(String name, String path, byte[] content) throws Exception; |
| 30 | 40 | ||
| 31 | } | 41 | } |
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 name, String path, byte[] content) throws Exception { |
| 23 | + return fileService.createFile(name, path, 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
| @@ -46,7 +46,7 @@ public class FileController { | @@ -46,7 +46,7 @@ public class FileController { | ||
| 46 | @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 | 46 | @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 |
| 47 | public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, | 47 | public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, |
| 48 | @RequestParam(value = "path", required = false) String path) throws Exception { | 48 | @RequestParam(value = "path", required = false) String path) throws Exception { |
| 49 | - return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); | 49 | + return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()))); |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | @DeleteMapping("/delete") | 52 | @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 name; | ||
| 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 name; | ||
| 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,12 @@ public interface FileService { | @@ -22,11 +22,12 @@ public interface FileService { | ||
| 22 | /** | 22 | /** |
| 23 | * 保存文件,并返回文件的访问路径 | 23 | * 保存文件,并返回文件的访问路径 |
| 24 | * | 24 | * |
| 25 | + * @param name 原文件名称 | ||
| 25 | * @param path 文件路径 | 26 | * @param path 文件路径 |
| 26 | * @param content 文件内容 | 27 | * @param content 文件内容 |
| 27 | * @return 文件路径 | 28 | * @return 文件路径 |
| 28 | */ | 29 | */ |
| 29 | - String createFile(String path, byte[] content) throws Exception; | 30 | + String createFile(String name, String path, byte[] content) throws Exception; |
| 30 | 31 | ||
| 31 | /** | 32 | /** |
| 32 | * 删除文件 | 33 | * 删除文件 |
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 path, byte[] content) throws Exception { | 40 | + public String createFile(String name, String path, byte[] content) throws Exception { |
| 41 | // 计算默认的 path 名 | 41 | // 计算默认的 path 名 |
| 42 | - String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path); | 42 | + String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name); |
| 43 | if (StrUtil.isEmpty(path)) { | 43 | if (StrUtil.isEmpty(path)) { |
| 44 | path = DigestUtil.md5Hex(content) + '.' + type; | 44 | path = DigestUtil.md5Hex(content) + '.' + type; |
| 45 | } | 45 | } |
| @@ -52,6 +52,7 @@ public class FileServiceImpl implements FileService { | @@ -52,6 +52,7 @@ public class FileServiceImpl implements FileService { | ||
| 52 | // 保存到数据库 | 52 | // 保存到数据库 |
| 53 | FileDO file = new FileDO(); | 53 | FileDO file = new FileDO(); |
| 54 | file.setConfigId(client.getId()); | 54 | file.setConfigId(client.getId()); |
| 55 | + file.setName(name); | ||
| 55 | file.setPath(path); | 56 | file.setPath(path); |
| 56 | file.setUrl(url); | 57 | file.setUrl(url); |
| 57 | file.setType(type); | 58 | 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 name = "单测文件名"; |
| 84 | // 调用 | 84 | // 调用 |
| 85 | - String result = fileService.createFile(path, content); | 85 | + String result = fileService.createFile(name, path, content); |
| 86 | // 断言 | 86 | // 断言 |
| 87 | assertEquals(result, url); | 87 | assertEquals(result, url); |
| 88 | // 校验数据 | 88 | // 校验数据 |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java
| @@ -225,7 +225,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { | @@ -225,7 +225,7 @@ 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 | + when(fileApi.createFile(eq( avatarFileBytes))).thenReturn(avatar); |
| 229 | 229 | ||
| 230 | // 调用 | 230 | // 调用 |
| 231 | userService.updateUserAvatar(userId, avatarFile); | 231 | userService.updateUserAvatar(userId, avatarFile); |
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" /> |
| @@ -160,7 +161,7 @@ export default { | @@ -160,7 +161,7 @@ export default { | ||
| 160 | }, | 161 | }, |
| 161 | /** 处理上传的文件发生变化 */ | 162 | /** 处理上传的文件发生变化 */ |
| 162 | handleFileChange(file, fileList) { | 163 | handleFileChange(file, fileList) { |
| 163 | - this.upload.data.path = file.name; | 164 | + |
| 164 | }, | 165 | }, |
| 165 | /** 处理文件上传中 */ | 166 | /** 处理文件上传中 */ |
| 166 | handleFileUploadProgress(event, file, fileList) { | 167 | handleFileUploadProgress(event, file, fileList) { |