From 92959efdc66d7363b9efb771fb3e290e412929e3 Mon Sep 17 00:00:00 2001 From: chenbowen Date: Mon, 7 Jul 2025 18:14:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=84=E4=BB=B6=E6=96=B0=E5=A2=9E=E6=90=BA?= =?UTF-8?q?=E5=B8=A6=E9=99=84=E4=BB=B6=E5=AE=9E=E4=BD=93=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/file/FileController.java | 11 +++++++ .../infra/service/file/FileService.java | 5 +++ .../infra/service/file/FileServiceImpl.java | 31 +++++++++++++++---- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java index 119774b7..b7177fbb 100644 --- a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java +++ b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java @@ -26,6 +26,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; + import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment; @@ -48,6 +50,15 @@ public class FileController { uploadReqVO.getDirectory(), file.getContentType())); } + @PostMapping("/upload-with-return") + @Operation(summary = "上传文件携带返回实体", description = "上传文件携带返回实体") + public CommonResult uploadWithReturn(FileUploadReqVO uploadReqVO) throws IOException { + MultipartFile file = uploadReqVO.getFile(); + byte[] content = IoUtil.readBytes(file.getInputStream()); + return success(fileService.createFileWhitReturn(content, file.getOriginalFilename(), + uploadReqVO.getDirectory(), file.getContentType())); + } + @GetMapping("/presigned-url") @Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器") @Parameters({ diff --git a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java index 01c2bba7..a3976629 100644 --- a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java +++ b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java @@ -4,8 +4,10 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import jakarta.validation.constraints.NotEmpty; +import lombok.SneakyThrows; /** * 文件 Service 接口 @@ -34,6 +36,9 @@ public interface FileService { String createFile(@NotEmpty(message = "文件内容不能为空") byte[] content, String name, String directory, String type); + @SneakyThrows + FileRespVO createFileWhitReturn(byte[] content, String name, String directory, String type); + /** * 生成文件预签名地址信息 * diff --git a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index 4a0faada..59876b12 100644 --- a/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-server/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java @@ -10,6 +10,7 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClient; @@ -34,13 +35,11 @@ public class FileServiceImpl implements FileService { /** * 上传文件的前缀,是否包含日期(yyyyMMdd) - * * 目的:按照日期,进行分目录 */ static boolean PATH_PREFIX_DATE_ENABLE = true; /** * 上传文件的后缀,是否包含时间戳 - * * 目的:保证文件的唯一性,避免覆盖 * 定制:可按需调整成 UUID、或者其他方式 */ @@ -60,6 +59,25 @@ public class FileServiceImpl implements FileService { @Override @SneakyThrows public String createFile(byte[] content, String name, String directory, String type) { + FileDO entity = uploadFile(content, name, directory, type); + return entity.getUrl(); + } + + @SneakyThrows + @Override + public FileRespVO createFileWhitReturn(byte[] content, String name, String directory, String type) { + FileDO entity = uploadFile(content, name, directory, type); + return new FileRespVO() + .setId(entity.getId()) + .setName(entity.getName()) + .setPath(entity.getPath()) + .setUrl(entity.getUrl()) + .setType(entity.getType()) + .setSize(entity.getSize()) + .setConfigId(entity.getConfigId()); + } + + private FileDO uploadFile(byte[] content, String name, String directory, String type) throws Exception { // 1.1 处理 type 为空的情况 if (StrUtil.isEmpty(type)) { type = FileTypeUtils.getMineType(content, name); @@ -82,12 +100,13 @@ public class FileServiceImpl implements FileService { FileClient client = fileConfigService.getMasterFileClient(); Assert.notNull(client, "客户端(master) 不能为空"); String url = client.upload(content, path, type); + FileDO entity = new FileDO().setConfigId(client.getId()) + .setName(name).setPath(path).setUrl(url) + .setType(type).setSize(content.length); // 3. 保存到数据库 - fileMapper.insert(new FileDO().setConfigId(client.getId()) - .setName(name).setPath(path).setUrl(url) - .setType(type).setSize(content.length)); - return url; + fileMapper.insert(entity); + return entity; } @VisibleForTesting