📖 CRM:code review 前端直接上传

This commit is contained in:
YunaiV 2024-02-17 20:35:48 +08:00
parent b2782e1d9a
commit 63f322cb17
8 changed files with 34 additions and 30 deletions

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.file.core.client; package cn.iocoder.yudao.framework.file.core.client;
import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO; import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlRespDTO;
/** /**
* 文件客户端 * 文件客户端
@ -45,10 +45,10 @@ public interface FileClient {
/** /**
* 获得文件预签名地址 * 获得文件预签名地址
* *
* @param fileName 文件名称 * @param path 相对路径
* @return 文件预签名地址 * @return 文件预签名地址
*/ */
default FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception { default FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
throw new UnsupportedOperationException("不支持的操作"); throw new UnsupportedOperationException("不支持的操作");
} }

View File

@ -5,17 +5,19 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/** /**
* 文件预签名地址 BO * 文件预签名地址 Response DTO
* *
* @author owen * @author owen
*/ */
@Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Data public class FilePresignedUrlRespDTO {
public class FilePresignedUrlBO {
/** /**
* 文件上传 URL用于上传 * 文件上传 URL用于上传
*
* 例如说
*/ */
private String uploadUrl; private String uploadUrl;

View File

@ -120,18 +120,15 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
} }
@Override @Override
public FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception { public FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.PUT) .method(Method.PUT)
.bucket(config.getBucket()) .bucket(config.getBucket())
.object(fileName) .object(path)
/** .expiry(10, TimeUnit.MINUTES) // 过期时间秒数取值范围1 ~ 7
* 过期时间秒数取值范围1秒 ~ 7天
* {@link GetPresignedObjectUrlArgs.Builder#validateExpiry(int)}
*/
.expiry(10, TimeUnit.MINUTES)
.build() .build()
); );
return new FilePresignedUrlBO(uploadUrl, config.getDomain() + "/" + fileName); return new FilePresignedUrlRespDTO(uploadUrl, config.getDomain() + "/" + path);
} }
} }

View File

@ -39,7 +39,7 @@ public class FileController {
private FileService fileService; private FileService fileService;
@PostMapping("/upload") @PostMapping("/upload")
@Operation(summary = "上传文件") @Operation(summary = "上传文件", description = "模式一:后端上传文件")
@OperateLog(logArgs = false) // 上传文件没有记录操作日志的必要 @OperateLog(logArgs = false) // 上传文件没有记录操作日志的必要
public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception { public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
MultipartFile file = uploadReqVO.getFile(); MultipartFile file = uploadReqVO.getFile();
@ -48,13 +48,13 @@ public class FileController {
} }
@GetMapping("/presigned-url") @GetMapping("/presigned-url")
@Operation(summary = "获取文件预签名地址") @Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")
public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("fileName") String fileName) throws Exception { public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("path") String path) throws Exception {
return success(fileService.getFilePresignedUrl(fileName)); return success(fileService.getFilePresignedUrl(path));
} }
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "创建文件") @Operation(summary = "创建文件", description = "模式二:前端上传文件:配合 presigned-url 接口,记录上传了上传的文件")
public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) { public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) {
return success(fileService.createFile(createReqVO)); return success(fileService.createFile(createReqVO));
} }

View File

@ -24,7 +24,7 @@ public class FileCreateReqVO {
@Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") @Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg")
private String url; private String url;
@Schema(description = "文件MIME类型", example = "application/octet-stream") @Schema(description = "文件 MIME 类型", example = "application/octet-stream")
private String type; private String type;
@Schema(description = "文件大小", example = "2048", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "文件大小", example = "2048", requiredMode = Schema.RequiredMode.REQUIRED)

View File

@ -14,10 +14,16 @@ public class FilePresignedUrlRespVO {
@Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11") @Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11")
private Long configId; private Long configId;
@Schema(description = "文件上传 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") @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")
private String uploadUrl; private String uploadUrl;
@Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg") /**
* 为什么要返回 url 字段
*
* 前端上传完文件后需要使用该 URL 进行访问
*/
@Schema(description = "文件访问 URL", requiredMode = Schema.RequiredMode.REQUIRED,
example = "https://test.yudao.iocoder.cn/758d3a5387507358c7236de4c8f96de1c7f5097ff6a7722b34772fb7b76b140f.png")
private String url; private String url;
} }

View File

@ -58,9 +58,9 @@ public interface FileService {
/** /**
* 生成文件预签名地址信息 * 生成文件预签名地址信息
* *
* @param fileName 文件名称 * @param path 文件路径
* @return 预签名地址信息 * @return 预签名地址信息
*/ */
FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception; FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception;
} }

View File

@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.io.FileUtils; import cn.iocoder.yudao.framework.common.util.io.FileUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.file.core.client.FileClient; import cn.iocoder.yudao.framework.file.core.client.FileClient;
import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlBO; import cn.iocoder.yudao.framework.file.core.client.s3.FilePresignedUrlRespDTO;
import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO; 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.FilePageReqVO;
@ -71,10 +71,8 @@ public class FileServiceImpl implements FileService {
@Override @Override
public Long createFile(FileCreateReqVO createReqVO) { public Long createFile(FileCreateReqVO createReqVO) {
// 插入
FileDO file = BeanUtils.toBean(createReqVO, FileDO.class); FileDO file = BeanUtils.toBean(createReqVO, FileDO.class);
fileMapper.insert(file); fileMapper.insert(file);
// 返回
return file.getId(); return file.getId();
} }
@ -108,10 +106,11 @@ public class FileServiceImpl implements FileService {
} }
@Override @Override
public FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception { public FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception {
FileClient fileClient = fileConfigService.getMasterFileClient(); FileClient fileClient = fileConfigService.getMasterFileClient();
FilePresignedUrlBO bo = fileClient.getPresignedObjectUrl(fileName); FilePresignedUrlRespDTO presignedObjectUrl = fileClient.getPresignedObjectUrl(path);
return BeanUtils.toBean(bo, FilePresignedUrlRespVO.class, f -> f.setConfigId(fileClient.getId())); return BeanUtils.toBean(presignedObjectUrl, FilePresignedUrlRespVO.class,
object -> object.setConfigId(fileClient.getId()));
} }
} }