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] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=9D=E7=95=99path=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=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) {