📖 CRM:code review 前端直接上传
This commit is contained in:
parent
b2782e1d9a
commit
63f322cb17
|
@ -1,6 +1,6 @@
|
|||
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 文件预签名地址
|
||||
*/
|
||||
default FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception {
|
||||
default FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
|
||||
throw new UnsupportedOperationException("不支持的操作");
|
||||
}
|
||||
|
||||
|
|
|
@ -5,17 +5,19 @@ import lombok.Data;
|
|||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 文件预签名地址 BO
|
||||
* 文件预签名地址 Response DTO
|
||||
*
|
||||
* @author owen
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public class FilePresignedUrlBO {
|
||||
public class FilePresignedUrlRespDTO {
|
||||
|
||||
/**
|
||||
* 文件上传 URL(用于上传)
|
||||
*
|
||||
* 例如说:
|
||||
*/
|
||||
private String uploadUrl;
|
||||
|
|
@ -120,18 +120,15 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public FilePresignedUrlBO getPresignedObjectUrl(String fileName) throws Exception {
|
||||
public FilePresignedUrlRespDTO getPresignedObjectUrl(String path) throws Exception {
|
||||
String uploadUrl = client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.PUT)
|
||||
.bucket(config.getBucket())
|
||||
.object(fileName)
|
||||
/**
|
||||
* 过期时间(秒数)取值范围:1秒 ~ 7天
|
||||
* {@link GetPresignedObjectUrlArgs.Builder#validateExpiry(int)}
|
||||
*/
|
||||
.expiry(10, TimeUnit.MINUTES)
|
||||
.object(path)
|
||||
.expiry(10, TimeUnit.MINUTES) // 过期时间(秒数)取值范围:1 秒 ~ 7 天
|
||||
.build()
|
||||
);
|
||||
return new FilePresignedUrlBO(uploadUrl, config.getDomain() + "/" + fileName);
|
||||
return new FilePresignedUrlRespDTO(uploadUrl, config.getDomain() + "/" + path);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ public class FileController {
|
|||
private FileService fileService;
|
||||
|
||||
@PostMapping("/upload")
|
||||
@Operation(summary = "上传文件")
|
||||
@Operation(summary = "上传文件", description = "模式一:后端上传文件")
|
||||
@OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
|
||||
public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
|
||||
MultipartFile file = uploadReqVO.getFile();
|
||||
|
@ -48,13 +48,13 @@ public class FileController {
|
|||
}
|
||||
|
||||
@GetMapping("/presigned-url")
|
||||
@Operation(summary = "获取文件预签名地址")
|
||||
public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("fileName") String fileName) throws Exception {
|
||||
return success(fileService.getFilePresignedUrl(fileName));
|
||||
@Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")
|
||||
public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("path") String path) throws Exception {
|
||||
return success(fileService.getFilePresignedUrl(path));
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建文件")
|
||||
@Operation(summary = "创建文件", description = "模式二:前端上传文件:配合 presigned-url 接口,记录上传了上传的文件")
|
||||
public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) {
|
||||
return success(fileService.createFile(createReqVO));
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class FileCreateReqVO {
|
|||
@Schema(description = "文件 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/yudao.jpg")
|
||||
private String url;
|
||||
|
||||
@Schema(description = "文件MIME类型", example = "application/octet-stream")
|
||||
@Schema(description = "文件 MIME 类型", example = "application/octet-stream")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "文件大小", example = "2048", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
|
|
|
@ -14,10 +14,16 @@ public class FilePresignedUrlRespVO {
|
|||
@Schema(description = "配置编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "11")
|
||||
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;
|
||||
|
||||
@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;
|
||||
|
||||
}
|
||||
|
|
|
@ -58,9 +58,9 @@ public interface FileService {
|
|||
/**
|
||||
* 生成文件预签名地址信息
|
||||
*
|
||||
* @param fileName 文件名称
|
||||
* @param path 文件路径
|
||||
* @return 预签名地址信息
|
||||
*/
|
||||
FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception;
|
||||
FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception;
|
||||
|
||||
}
|
||||
|
|
|
@ -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.object.BeanUtils;
|
||||
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.module.infra.controller.admin.file.vo.file.FileCreateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
|
||||
|
@ -71,10 +71,8 @@ public class FileServiceImpl implements FileService {
|
|||
|
||||
@Override
|
||||
public Long createFile(FileCreateReqVO createReqVO) {
|
||||
// 插入
|
||||
FileDO file = BeanUtils.toBean(createReqVO, FileDO.class);
|
||||
fileMapper.insert(file);
|
||||
// 返回
|
||||
return file.getId();
|
||||
}
|
||||
|
||||
|
@ -108,10 +106,11 @@ public class FileServiceImpl implements FileService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public FilePresignedUrlRespVO getFilePresignedUrl(String fileName) throws Exception {
|
||||
public FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception {
|
||||
FileClient fileClient = fileConfigService.getMasterFileClient();
|
||||
FilePresignedUrlBO bo = fileClient.getPresignedObjectUrl(fileName);
|
||||
return BeanUtils.toBean(bo, FilePresignedUrlRespVO.class, f -> f.setConfigId(fileClient.getId()));
|
||||
FilePresignedUrlRespDTO presignedObjectUrl = fileClient.getPresignedObjectUrl(path);
|
||||
return BeanUtils.toBean(presignedObjectUrl, FilePresignedUrlRespVO.class,
|
||||
object -> object.setConfigId(fileClient.getId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue