From 9e485fa477531a639ca07087950a42cb2525baec Mon Sep 17 00:00:00 2001 From: chenbowen Date: Fri, 26 Sep 2025 11:41:38 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=8D=87=E7=BA=A7=203.0.39=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20FileApi=20=E8=8E=B7=E5=8F=96=E5=88=B0=E5=AE=8C?= =?UTF-8?q?=E6=95=B4=20fileDO=20=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plat/module/infra/api/file/FileApi.java | 26 ++++- .../module/infra/api/file/FileApiImpl.java | 97 ++++++++++++++++--- .../infra/service/file/FileServiceImpl.java | 5 +- .../contract/DemoContractController.java | 8 ++ 4 files changed, 118 insertions(+), 18 deletions(-) diff --git a/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/file/FileApi.java b/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/file/FileApi.java index ac773250..5424ac10 100644 --- a/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/file/FileApi.java +++ b/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/file/FileApi.java @@ -10,10 +10,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import org.springframework.cloud.openfeign.FeignClient; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; @FeignClient(name = ApiConstants.NAME) // TODO 芋艿:fallbackFactory = @Tag(name = "RPC 服务 - 文件") @@ -25,6 +22,16 @@ public interface FileApi { @Operation(summary = "保存文件,并返回文件的访问路径") CommonResult createFile(@Valid @RequestBody FileCreateReqDTO createReqDTO); + /** + * 创建文件并返回完整文件信息 + * + * @param createReqDTO 文件创建请求 + * @return 文件完整信息 + */ + @PostMapping(PREFIX + "/create-with-return") + @Operation(summary = "创建文件并返回完整文件信息") + CommonResult createFileWithReturn(@Valid @RequestBody FileCreateReqDTO createReqDTO); + /** * 根据文件编号获取文件信息和二进制内容 * @@ -36,4 +43,15 @@ public interface FileApi { CommonResult getFile(@Parameter(description = "文件编号", required = true, example = "1024") @RequestParam("fileId") @NotNull(message = "文件编号不能为空") Long fileId); + /** + * 删除文件 + * + * @param fileId 文件编号 + * @return 删除结果 + */ + @DeleteMapping(PREFIX + "/delete") + @Operation(summary = "删除文件") + CommonResult deleteFile(@Parameter(description = "文件编号", required = true, example = "1024") + @RequestParam("fileId") @NotNull(message = "文件编号不能为空") Long fileId); + } diff --git a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/api/file/FileApiImpl.java b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/api/file/FileApiImpl.java index 1dd7559e..0833b2d4 100644 --- a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/api/file/FileApiImpl.java +++ b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/api/file/FileApiImpl.java @@ -3,6 +3,7 @@ package com.zt.plat.module.infra.api.file; import com.zt.plat.framework.common.pojo.CommonResult; import com.zt.plat.module.infra.api.file.dto.FileCreateReqDTO; import com.zt.plat.module.infra.api.file.dto.FileRespDTO; +import com.zt.plat.module.infra.controller.admin.file.vo.file.FileRespVO; import com.zt.plat.module.infra.dal.dataobject.file.FileDO; import com.zt.plat.module.infra.service.file.FileService; import jakarta.annotation.Resource; @@ -24,6 +25,32 @@ public class FileApiImpl implements FileApi { createReqDTO.getDirectory(), createReqDTO.getType(), false)); } + @Override + public CommonResult createFileWithReturn(FileCreateReqDTO createReqDTO) { + try { + FileRespVO fileRespVO = fileService.createFileWhitReturn( + createReqDTO.getContent(), + createReqDTO.getName(), + createReqDTO.getDirectory(), + createReqDTO.getType(), + false + ); + + if (fileRespVO == null) { + return CommonResult.error(500, "文件创建失败"); + } + + // 将 FileRespVO 转换为 FileRespDTO + FileRespDTO respDTO = convertToRespDTO(fileRespVO); + // 设置文件内容 + respDTO.setContent(createReqDTO.getContent()); + + return success(respDTO); + } catch (Exception e) { + return CommonResult.error(500, "创建文件失败:" + e.getMessage()); + } + } + @Override public CommonResult getFile(Long fileId) { try { @@ -40,19 +67,8 @@ public class FileApiImpl implements FileApi { } // 构建响应对象 - FileRespDTO respDTO = new FileRespDTO(); - respDTO.setId(fileDO.getId()); - respDTO.setName(fileDO.getName()); - respDTO.setType(fileDO.getType()); - respDTO.setSize(fileDO.getSize()); + FileRespDTO respDTO = convertToRespDTO(fileDO); respDTO.setContent(content); - - // 从文件路径或URL中提取目录信息 - String path = fileDO.getPath(); - if (path != null && path.contains("/")) { - String directory = path.substring(0, path.lastIndexOf("/")); - respDTO.setDirectory(directory); - } return success(respDTO); } catch (Exception e) { @@ -60,4 +76,61 @@ public class FileApiImpl implements FileApi { } } + @Override + public CommonResult deleteFile(Long fileId) { + try { + // 检查文件是否存在 + FileDO fileDO = fileService.getActiveFileById(fileId); + if (fileDO == null) { + return CommonResult.error(404, "文件不存在"); + } + + // 删除文件 + fileService.deleteFile(fileId); + return success(true); + } catch (Exception e) { + return CommonResult.error(500, "删除文件失败:" + e.getMessage()); + } + } + + /** + * 将 FileDO 转换为 FileRespDTO + */ + private FileRespDTO convertToRespDTO(FileDO fileDO) { + FileRespDTO respDTO = new FileRespDTO(); + respDTO.setId(fileDO.getId()); + respDTO.setName(fileDO.getName()); + respDTO.setType(fileDO.getType()); + respDTO.setSize(fileDO.getSize()); + + // 从文件路径中提取目录信息 + String path = fileDO.getPath(); + if (path != null && path.contains("/")) { + String directory = path.substring(0, path.lastIndexOf("/")); + respDTO.setDirectory(directory); + } + + return respDTO; + } + + /** + * 将 FileRespVO 转换为 FileRespDTO + */ + private FileRespDTO convertToRespDTO(FileRespVO fileRespVO) { + FileRespDTO respDTO = new FileRespDTO(); + respDTO.setId(fileRespVO.getId()); + respDTO.setName(fileRespVO.getName()); + respDTO.setType(fileRespVO.getType()); + respDTO.setSize(fileRespVO.getSize()); + + // 从文件路径中提取目录信息 + String path = fileRespVO.getPath(); + if (path != null && path.contains("/")) { + String directory = path.substring(0, path.lastIndexOf("/")); + respDTO.setDirectory(directory); + } + + return respDTO; + } + } diff --git a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/file/FileServiceImpl.java b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/file/FileServiceImpl.java index bf505e38..8a699ce1 100644 --- a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/file/FileServiceImpl.java +++ b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/file/FileServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.digest.DigestUtil; +import com.google.common.annotations.VisibleForTesting; import com.zt.plat.framework.common.pojo.PageResult; import com.zt.plat.framework.common.util.object.BeanUtils; import com.zt.plat.module.infra.controller.admin.file.vo.file.FileCreateReqVO; @@ -18,7 +19,6 @@ import com.zt.plat.module.infra.framework.file.core.client.FileClient; import com.zt.plat.module.infra.framework.file.core.client.s3.FilePresignedUrlRespDTO; import com.zt.plat.module.infra.framework.file.core.utils.FileTypeUtils; import com.zt.plat.module.infra.util.VerificationCodeUtil; -import com.google.common.annotations.VisibleForTesting; import jakarta.annotation.Resource; import lombok.SneakyThrows; import org.apache.commons.lang3.StringUtils; @@ -122,7 +122,8 @@ public class FileServiceImpl implements FileService { @SneakyThrows public String createFile(byte[] content, String name, String directory, String type, Boolean encrypt) { FileDO entity = uploadFile(content, name, directory, type, encrypt); - return entity.getUrl(); + FileRespVO fileRespVO = BeanUtils.toBean(entity, FileRespVO.class); + return fileRespVO.getUrl(); } @SneakyThrows diff --git a/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/controller/admin/contract/DemoContractController.java b/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/controller/admin/contract/DemoContractController.java index b91e8e9d..66be3f09 100644 --- a/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/controller/admin/contract/DemoContractController.java +++ b/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/controller/admin/contract/DemoContractController.java @@ -15,6 +15,7 @@ import com.zt.plat.module.bpm.api.task.BpmProcessInstanceApi; import com.zt.plat.module.bpm.api.task.BpmTaskApi; import com.zt.plat.module.infra.api.businessfile.BusinessFileApi; import com.zt.plat.module.infra.api.file.FileApi; +import com.zt.plat.module.infra.api.file.dto.FileCreateReqDTO; import com.zt.plat.module.infra.api.file.dto.FileRespDTO; import com.zt.plat.module.system.api.dept.DeptApi; import com.zt.plat.module.template.controller.admin.contract.vo.DemoContractPageReqVO; @@ -114,6 +115,13 @@ public class DemoContractController extends AbstractFileUploadController impleme @PreAuthorize("@ss.hasPermission('template:demo-contract:query')") public CommonResult> getDemoContractPage(@Valid DemoContractPageReqVO pageReqVO) { CommonResult file = fileApi.getFile(1968928810422521857L); + + FileCreateReqDTO createReqDTO = new FileCreateReqDTO(); + createReqDTO.setContent(file.getCheckedData().getContent()); + createReqDTO.setType(file.getCheckedData().getType()); + createReqDTO.setName(file.getCheckedData().getName()); + createReqDTO.setDirectory(file.getCheckedData().getDirectory()); + CommonResult fileWithReturn = fileApi.createFileWithReturn(createReqDTO); Long id = IdWorker.getId(); System.out.println("Generated ID: " + id); PageResult pageResult = demoContractService.getDemoContractPage(pageReqVO);