From 9b3092b3fd6cf62cff5211f933ea77d4da49e8e4 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 15 Jun 2022 21:05:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20FileApi=20=E7=9A=84=20feig?= =?UTF-8?q?n=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/module/infra/api/file/FileApi.java | 51 +++++++++++++++---- .../infra/api/file/dto/FileCreateReqDTO.java | 23 +++++++++ .../logger/dto/ApiErrorLogCreateReqDTO.java | 2 +- .../module/infra/api/file/FileApiImpl.java | 22 +++++--- .../controller/admin/file/FileController.java | 2 +- .../infra/dal/dataobject/file/FileDO.java | 4 ++ .../infra/service/file/FileService.java | 5 +- .../infra/service/file/FileServiceImpl.java | 11 +++- .../infra/service/file/FileServiceTest.java | 4 +- .../logger/dto/OperateLogCreateReqDTO.java | 2 +- .../system/framework/TmpConfiguration.java | 44 ---------------- .../rpc/config/RpcConfiguration.java | 11 ++++ .../system/framework/rpc/package-info.java | 4 ++ 13 files changed, 114 insertions(+), 71 deletions(-) create mode 100644 yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/dto/FileCreateReqDTO.java delete mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/TmpConfiguration.java create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/package-info.java 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 fa3ec5c6e..e58d14833 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 @@ -1,23 +1,32 @@ package cn.iocoder.yudao.module.infra.api.file; -import cn.hutool.core.util.IdUtil; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.infra.api.file.dto.FileCreateReqDTO; +import cn.iocoder.yudao.module.infra.enums.ApiConstants; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; -/** - * 文件 API 接口 - * - * @author 芋道源码 - */ +import javax.validation.Valid; + +@FeignClient(name = ApiConstants.NAME) // TODO 芋艿:fallbackFactory = +@Api(tags = "RPC 服务 - 文件") public interface FileApi { + String PREFIX = ApiConstants.PREFIX + "/file"; + /** * 保存文件,并返回文件的访问路径 * * @param content 文件内容 * @return 文件路径 */ - default String createFile(byte[] content) throws Exception { - return createFile(IdUtil.fastUUID(), content); - } + default String createFile(byte[] content) { + return createFile(null, null, content); + } /** * 保存文件,并返回文件的访问路径 @@ -26,6 +35,28 @@ public interface FileApi { * @param content 文件内容 * @return 文件路径 */ - String createFile(String path, byte[] content) throws Exception; + default String createFile(String path, byte[] content) { + return createFile(null, path, content); + } + + /** + * 保存文件,并返回文件的访问路径 + * + * @param name 原文件名称 + * @param path 文件路径 + * @param content 文件内容 + * @return 文件路径 + */ + default String createFile(@RequestParam("name") String name, + @RequestParam("path") String path, + @RequestParam("content") byte[] content) { + CommonResult result = createFile(new FileCreateReqDTO().setName(name).setPath(path).setContent(content)); + result.checkError(); + return result.getData(); + } + + @PostMapping(PREFIX + "/create") + @ApiOperation("保存文件,并返回文件的访问路径") + CommonResult createFile(@Valid @RequestBody FileCreateReqDTO createReqDTO); } diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/dto/FileCreateReqDTO.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/dto/FileCreateReqDTO.java new file mode 100644 index 000000000..bc6681088 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/dto/FileCreateReqDTO.java @@ -0,0 +1,23 @@ +package cn.iocoder.yudao.module.infra.api.file.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotEmpty; + +@ApiModel("RPC 服务 - 文件创建 Request DTO") +@Data +public class FileCreateReqDTO { + + @ApiModelProperty(value = "原文件名称", example = "xxx.png") + private String name; + + @ApiModelProperty(value = "文件路径", example = "xxx.png") + private String path; + + @ApiModelProperty(value = "文件内容", required = true) + @NotEmpty(message = "文件内容不能为空") + private byte[] content; + +} diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/dto/ApiErrorLogCreateReqDTO.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/dto/ApiErrorLogCreateReqDTO.java index 1e9c4933e..2da5046c2 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/dto/ApiErrorLogCreateReqDTO.java +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/dto/ApiErrorLogCreateReqDTO.java @@ -7,7 +7,7 @@ import lombok.Data; import javax.validation.constraints.NotNull; import java.util.Date; -@ApiModel("API 错误日志创建 Request DTO") +@ApiModel("RPC 服务 - API 错误日志创建 Request DTO") @Data public class ApiErrorLogCreateReqDTO { 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 d43eac1d5..bf403853e 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 @@ -1,17 +1,22 @@ package cn.iocoder.yudao.module.infra.api.file; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.infra.api.file.dto.FileCreateReqDTO; import cn.iocoder.yudao.module.infra.service.file.FileService; +import org.apache.commons.fileupload.FileItem; +import org.apache.dubbo.config.annotation.DubboService; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.annotation.Resource; -/** - * 文件 API 实现类 - * - * @author 芋道源码 - */ -@Service +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION; + +@RestController // 提供 RESTful API 接口,给 Feign 调用 +@DubboService(version = VERSION) // 提供 Dubbo RPC 接口,给 Dubbo Consumer 调用 @Validated public class FileApiImpl implements FileApi { @@ -19,8 +24,9 @@ public class FileApiImpl implements FileApi { private FileService fileService; @Override - public String createFile(String path, byte[] content) throws Exception { - return fileService.createFile(path, content); + public CommonResult createFile(FileCreateReqDTO createReqDTO) { + return success(fileService.createFile(createReqDTO.getName(), createReqDTO.getPath(), + createReqDTO.getContent())); } } 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 4500133ed..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 @@ -46,7 +46,7 @@ public class FileController { @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 public CommonResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "path", required = false) String path) throws Exception { - return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); + 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/dal/dataobject/file/FileDO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java index 4ad155e94..7e81280da 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java @@ -33,6 +33,10 @@ public class FileDO extends BaseDO { * 关联 {@link FileConfigDO#getId()} */ private Long configId; + /** + * 原文件名 + */ + private String name; /** * 路径,即文件名 */ 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 2d893c082..52710a990 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 @@ -1,7 +1,7 @@ package cn.iocoder.yudao.module.infra.service.file; -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; /** @@ -22,11 +22,12 @@ public interface FileService { /** * 保存文件,并返回文件的访问路径 * + * @param name 原文件名称 * @param path 文件路径 * @param content 文件内容 * @return 文件路径 */ - String createFile(String path, byte[] content) throws Exception; + String createFile(String name, String path, byte[] content); /** * 删除文件 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 fc3eedbac..b90e92752 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 @@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.file.core.client.FileClient; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; +import lombok.SneakyThrows; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -37,12 +38,17 @@ public class FileServiceImpl implements FileService { } @Override - public String createFile(String path, byte[] content) throws Exception { + @SneakyThrows + public String createFile(String name, String path, byte[] content) { // 计算默认的 path 名 - String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path); + String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name); if (StrUtil.isEmpty(path)) { path = DigestUtil.md5Hex(content) + '.' + type; } + // 如果 name 为空,则使用 path 填充 + if (StrUtil.isEmpty(name)) { + name = path; + } // 上传到文件存储器 FileClient client = fileConfigService.getMasterFileClient(); @@ -52,6 +58,7 @@ public class FileServiceImpl implements FileService { // 保存到数据库 FileDO file = new FileDO(); file.setConfigId(client.getId()); + file.setName(name); file.setPath(path); file.setUrl(url); file.setType(type); 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 da377e79b..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 @@ -80,9 +80,9 @@ public class FileServiceTest extends BaseDbUnitTest { String url = randomString(); when(client.upload(same(content), same(path))).thenReturn(url); when(client.getId()).thenReturn(10L); - + String name = "单测文件名"; // 调用 - String result = fileService.createFile(path, content); + String result = fileService.createFile(name, path, content); // 断言 assertEquals(result, url); // 校验数据 diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/logger/dto/OperateLogCreateReqDTO.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/logger/dto/OperateLogCreateReqDTO.java index dc6acef3a..9a9700e0f 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/logger/dto/OperateLogCreateReqDTO.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/logger/dto/OperateLogCreateReqDTO.java @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull; import java.util.Date; import java.util.Map; -@ApiModel("操作日志创建 Request DTO") +@ApiModel("RPC 服务 - 操作日志创建 Request DTO") @Data public class OperateLogCreateReqDTO { diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/TmpConfiguration.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/TmpConfiguration.java deleted file mode 100644 index 2386654e6..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/TmpConfiguration.java +++ /dev/null @@ -1,44 +0,0 @@ -package cn.iocoder.yudao.module.system.framework; - -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLog; -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLog; -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; -import cn.iocoder.yudao.module.infra.api.file.FileApi; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class TmpConfiguration { - - @Bean - public FileApi fileApi() { - return new FileApi() { - @Override - public String createFile(String path, byte[] content) throws Exception { - return null; - } - }; - } - - @Bean - public ApiAccessLogFrameworkService apiAccessLogFrameworkService() { - return new ApiAccessLogFrameworkService() { - @Override - public void createApiAccessLog(ApiAccessLog apiAccessLog) { - - } - }; - } - - @Bean - public ApiErrorLogFrameworkService apiErrorLogFrameworkService() { - return new ApiErrorLogFrameworkService() { - @Override - public void createApiErrorLog(ApiErrorLog apiErrorLog) { - - } - }; - } - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java new file mode 100644 index 000000000..46c250092 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java @@ -0,0 +1,11 @@ +package cn.iocoder.yudao.module.system.framework.rpc.config; + +import cn.iocoder.yudao.module.infra.api.file.FileApi; +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@EnableFeignClients(clients = FileApi.class) +public class RpcConfiguration { +} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/package-info.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/package-info.java new file mode 100644 index 000000000..61228c231 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/package-info.java @@ -0,0 +1,4 @@ +/** + * 占位 + */ +package cn.iocoder.yudao.module.system.framework.rpc;