From 63e632ceb7c9df92b406017b16039a83ee3e9218 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 11 May 2022 01:20:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=20ImageUpload=20=E4=B8=8A=E4=BC=A0=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/file/FileController.java | 4 +++- .../infra/service/file/FileServiceImpl.java | 10 ++++++++- .../src/components/ImageUpload/index.vue | 21 +++++++------------ 3 files changed, 20 insertions(+), 15 deletions(-) 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 e3d810dec..4500133ed 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 @@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; +import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; import cn.iocoder.yudao.module.infra.convert.file.FileConvert; @@ -42,8 +43,9 @@ public class FileController { @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, - @RequestParam("path") String path) throws Exception { + @RequestParam(value = "path", required = false) String path) throws Exception { return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); } 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 66af402e0..4f3736f1b 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 @@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.infra.service.file; import cn.hutool.core.io.FileTypeUtil; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.digest.DigestUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.file.core.client.FileClient; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; @@ -36,6 +38,12 @@ public class FileServiceImpl implements FileService { @Override public String createFile(String path, byte[] content) throws Exception { + // 计算默认的 path 名 + String type = FileTypeUtil.getType(new ByteArrayInputStream(content)); + if (StrUtil.isEmpty(path)) { + path = DigestUtil.md5Hex(content) + '.' + type; + } + // 上传到文件存储器 FileClient client = fileConfigService.getMasterFileClient(); Assert.notNull(client, "客户端(master) 不能为空"); @@ -46,7 +54,7 @@ public class FileServiceImpl implements FileService { file.setConfigId(client.getId()); file.setPath(path); file.setUrl(url); - file.setType(FileTypeUtil.getType(new ByteArrayInputStream(content))); + file.setType(type); file.setSize(content.length); fileMapper.insert(file); return url; diff --git a/yudao-ui-admin/src/components/ImageUpload/index.vue b/yudao-ui-admin/src/components/ImageUpload/index.vue index 6fb6b5e11..e43fa780c 100644 --- a/yudao-ui-admin/src/components/ImageUpload/index.vue +++ b/yudao-ui-admin/src/components/ImageUpload/index.vue @@ -2,7 +2,7 @@
{ if (typeof item === "string") { - if (item.indexOf(this.baseUrl) === -1) { - item = { name: this.baseUrl + item, url: this.baseUrl + item }; - } else { - item = { name: item, url: item }; - } + // edit by 芋道源码 + item = { name: item, url: item }; } return item; }); @@ -127,7 +121,8 @@ export default { }, // 上传成功回调 handleUploadSuccess(res) { - this.uploadList.push({ name: res.fileName, url: res.fileName }); + // edit by 芋道源码 + this.uploadList.push({ name: res.data, url: res.data }); if (this.uploadList.length === this.number) { this.fileList = this.fileList.concat(this.uploadList); this.uploadList = []; @@ -188,7 +183,7 @@ export default { for (let i in list) { strs += list[i].url.replace(this.baseUrl, "") + separator; } - return strs != '' ? strs.substr(0, strs.length - 1) : ''; + return strs !== '' ? strs.substr(0, strs.length - 1) : ''; } } };