Commit 63f322cb177a85b8e899bd15758a2f4c600fb041

Authored by YunaiV
1 parent b2782e1d

📖 CRM:code review 前端直接上传

yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/FileClient.java
1 package cn.iocoder.yudao.framework.file.core.client; 1 package cn.iocoder.yudao.framework.file.core.client;
2 2
3 -import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO; 3 +import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlRespDTO;
4 4
5 /** 5 /**
6 * 文件客户端 6 * 文件客户端
@@ -45,10 +45,10 @@ public interface FileClient { @@ -45,10 +45,10 @@ public interface FileClient {
45 /** 45 /**
46 * 获得文件预签名地址 46 * 获得文件预签名地址
47 * 47 *
48 - * @param fileName 文件名称 48 + * @param path 相对路径
49 * @return 文件预签名地址 49 * @return 文件预签名地址
50 */ 50 */
51 - default FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception { 51 + default FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
52 throw new UnsupportedOperationException("不支持的操作"); 52 throw new UnsupportedOperationException("不支持的操作");
53 } 53 }
54 54
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/FilePresignedUrlBO.java renamed to yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/FilePresignedUrlRespDTO.java
@@ -5,17 +5,19 @@ import lombok.Data; @@ -5,17 +5,19 @@ import lombok.Data;
5 import lombok.NoArgsConstructor; 5 import lombok.NoArgsConstructor;
6 6
7 /** 7 /**
8 - * 文件预签名地址 BO 8 + * 文件预签名地址 Response DTO
9 * 9 *
10 * @author owen 10 * @author owen
11 */ 11 */
  12 +@Data
12 @AllArgsConstructor 13 @AllArgsConstructor
13 @NoArgsConstructor 14 @NoArgsConstructor
14 -@Data  
15 -public class FilePresignedUrlBO { 15 +public class FilePresignedUrlRespDTO {
16 16
17 /** 17 /**
18 * 文件上传 URL(用于上传) 18 * 文件上传 URL(用于上传)
  19 + *
  20 + * 例如说:
19 */ 21 */
20 private String uploadUrl; 22 private String uploadUrl;
21 23
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java
@@ -120,18 +120,15 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { @@ -120,18 +120,15 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
120 } 120 }
121 121
122 @Override 122 @Override
123 - public FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception { 123 + public FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
124 String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() 124 String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
125 .method(Method.PUT) 125 .method(Method.PUT)
126 .bucket(config.getBucket()) 126 .bucket(config.getBucket())
127 - .object(fileName)  
128 - /**  
129 - * 过期时间(秒数)取值范围:1秒 ~ 7天  
130 - * {@link GetPresignedObjectUrlArgs.Builder#validateExpiry(int)}  
131 - */  
132 - .expiry(10, TimeUnit.MINUTES) 127 + .object(path)
  128 + .expiry(10, TimeUnit.MINUTES) // 过期时间(秒数)取值范围:1 秒 ~ 7 天
133 .build() 129 .build()
134 ); 130 );
135 - return new FilePresignedUrlBO(uploadUrl, config.getDomain() + "/" + fileName); 131 + return new FilePresignedUrlRespDTO(uploadUrl, config.getDomain() + "/" + path);
136 } 132 }
  133 +
137 } 134 }
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java
@@ -39,7 +39,7 @@ public class FileController { @@ -39,7 +39,7 @@ public class FileController {
39 private FileService fileService; 39 private FileService fileService;
40 40
41 @PostMapping("/upload") 41 @PostMapping("/upload")
42 - @Operation(summary = "上传文件") 42 + @Operation(summary = "上传文件", description = "模式一:后端上传文件")
43 @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 43 @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
44 public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception { 44 public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
45 MultipartFile file = uploadReqVO.getFile(); 45 MultipartFile file = uploadReqVO.getFile();
@@ -48,13 +48,13 @@ public class FileController { @@ -48,13 +48,13 @@ public class FileController {
48 } 48 }
49 49
50 @GetMapping("/presigned-url") 50 @GetMapping("/presigned-url")
51 - @Operation(summary = "获取文件预签名地址")  
52 - public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("fileName") String fileName) throws Exception {  
53 - return success(fileService.getFilePresignedUrl(fileName)); 51 + @Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")
  52 + public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("path") String path) throws Exception {
  53 + return success(fileService.getFilePresignedUrl(path));
54 } 54 }
55 55
56 @PostMapping("/create") 56 @PostMapping("/create")
57 - @Operation(summary = "创建文件") 57 + @Operation(summary = "创建文件", description = "模式二:前端上传文件:配合 presigned-url 接口,记录上传了上传的文件")
58 public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) { 58 public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) {
59 return success(fileService.createFile(createReqVO)); 59 return success(fileService.createFile(createReqVO));
60 } 60 }
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileCreateReqVO.java
@@ -24,7 +24,7 @@ public class FileCreateReqVO { @@ -24,7 +24,7 @@ public class FileCreateReqVO {
24 @Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") 24 @Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg")
25 private String url; 25 private String url;
26 26
27 - @Schema(description = "文件MIME类型", example = "application/octet-stream") 27 + @Schema(description = "文件 MIME 类型", example = "application/octet-stream")
28 private String type; 28 private String type;
29 29
30 @Schema(description = "文件大小", example = "2048", requiredMode = Schema.RequiredMode.REQUIRED) 30 @Schema(description = "文件大小", example = "2048", requiredMode = Schema.RequiredMode.REQUIRED)
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FilePresignedUrlRespVO.java
@@ -14,10 +14,16 @@ public class FilePresignedUrlRespVO { @@ -14,10 +14,16 @@ public class FilePresignedUrlRespVO {
14 @Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11") 14 @Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11")
15 private Long configId; 15 private Long configId;
16 16
17 - @Schema(description = "文件上传 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") 17 + @Schema(description = "文件上传 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://s3.cn-south-1.qiniucs.com/ruoyi-vue-pro/758d3a5387507358c7236de4c8f96de1c7f5097ff6a7722b34772fb7b76b140f.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=3TvrJ70gl2Gt6IBe7_IZT1F6i_k0iMuRtyEv4EyS%2F20240217%2Fcn-south-1%2Fs3%2Faws4_request&X-Amz-Date=20240217T123222Z&X-Amz-Expires=600&X-Amz-SignedHeaders=host&X-Amz-Signature=a29f33770ab79bf523ccd4034d0752ac545f3c2a3b17baa1eb4e280cfdccfda5")
18 private String uploadUrl; 18 private String uploadUrl;
19 19
20 - @Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") 20 + /**
  21 + * 为什么要返回 url 字段?
  22 + *
  23 + * 前端上传完文件后,需要使用该 URL 进行访问
  24 + */
  25 + @Schema(description = "文件访问 URL", requiredMode = Schema.RequiredMode.REQUIRED,
  26 + example = "https://test.yudao.iocoder.cn/758d3a5387507358c7236de4c8f96de1c7f5097ff6a7722b34772fb7b76b140f.png")
21 private String url; 27 private String url;
22 28
23 } 29 }
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java
@@ -58,9 +58,9 @@ public interface FileService { @@ -58,9 +58,9 @@ public interface FileService {
58 /** 58 /**
59 * 生成文件预签名地址信息 59 * 生成文件预签名地址信息
60 * 60 *
61 - * @param fileName 文件名称 61 + * @param path 文件路径
62 * @return 预签名地址信息 62 * @return 预签名地址信息
63 */ 63 */
64 - FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception; 64 + FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception;
65 65
66 } 66 }
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
@@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; @@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 import cn.iocoder.yudao.framework.common.util.io.FileUtils; 6 import cn.iocoder.yudao.framework.common.util.io.FileUtils;
7 import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 7 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
8 import cn.iocoder.yudao.framework.file.core.client.FileClient; 8 import cn.iocoder.yudao.framework.file.core.client.FileClient;
9 -import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO; 9 +import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlRespDTO;
10 import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; 10 import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils;
11 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO; 11 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO;
12 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; 12 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
@@ -71,10 +71,8 @@ public class FileServiceImpl implements FileService { @@ -71,10 +71,8 @@ public class FileServiceImpl implements FileService {
71 71
72 @Override 72 @Override
73 public Long createFile(FileCreateReqVO createReqVO) { 73 public Long createFile(FileCreateReqVO createReqVO) {
74 - // 插入  
75 FileDO file = BeanUtils.toBean(createReqVO, FileDO.class); 74 FileDO file = BeanUtils.toBean(createReqVO, FileDO.class);
76 fileMapper.insert(file); 75 fileMapper.insert(file);
77 - // 返回  
78 return file.getId(); 76 return file.getId();
79 } 77 }
80 78
@@ -108,10 +106,11 @@ public class FileServiceImpl implements FileService { @@ -108,10 +106,11 @@ public class FileServiceImpl implements FileService {
108 } 106 }
109 107
110 @Override 108 @Override
111 - public FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception { 109 + public FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception {
112 FileClient fileClient = fileConfigService.getMasterFileClient(); 110 FileClient fileClient = fileConfigService.getMasterFileClient();
113 - FilePresignedUrlBO bo = fileClient.getPresignedObjectUrl(fileName);  
114 - return BeanUtils.toBean(bo, FilePresignedUrlRespVO.class, f -> f.setConfigId(fileClient.getId())); 111 + FilePresignedUrlRespDTO presignedObjectUrl = fileClient.getPresignedObjectUrl(path);
  112 + return BeanUtils.toBean(presignedObjectUrl, FilePresignedUrlRespVO.class,
  113 + object -> object.setConfigId(fileClient.getId()));
115 } 114 }
116 115
117 } 116 }