增加返回文件上传返回接口 增加文件Id返回

This commit is contained in:
limin 2024-12-27 13:55:31 +08:00
parent ce2497ede5
commit 3dec485c4b
3 changed files with 42 additions and 3 deletions

View File

@ -45,6 +45,13 @@ public class FileController {
String path = uploadReqVO.getPath();
return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
}
@PostMapping("/llmUpload")
@Operation(summary = "上传文件", description = "模式一增加Id后端上传文件")
public CommonResult<FileCreateRespVo> uploadFiles(FileUploadReqVO uploadReqVO) throws Exception {
MultipartFile file = uploadReqVO.getFile();
String path = uploadReqVO.getPath();
return success(fileService.llmcreateFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
}
@GetMapping("/presigned-url")
@Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.infra.service.file;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileCreateRespVo;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
@ -63,4 +64,5 @@ public interface FileService {
*/
FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception;
FileCreateRespVo llmcreateFile(String originalFilename, String path, byte[] bytes);
}

View File

@ -5,12 +5,10 @@ import cn.hutool.core.util.StrUtil;
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.module.infra.controller.admin.file.vo.file.*;
import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClient;
import cn.iocoder.yudao.module.infra.framework.file.core.client.s3.FilePresignedUrlRespDTO;
import cn.iocoder.yudao.module.infra.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;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
import lombok.SneakyThrows;
@ -114,4 +112,36 @@ public class FileServiceImpl implements FileService {
object -> object.setConfigId(fileClient.getId()));
}
@Override
@SneakyThrows
public FileCreateRespVo llmcreateFile(String name, String path, byte[] content) {
// 计算默认的 path
String type = FileTypeUtils.getMineType(content, name);
if (StrUtil.isEmpty(path)) {
path = FileUtils.generatePath(content, name);
}
// 如果 name 为空则使用 path 填充
if (StrUtil.isEmpty(name)) {
name = path;
}
// 上传到文件存储器
FileClient client = fileConfigService.getMasterFileClient();
Assert.notNull(client, "客户端(master) 不能为空");
String url = client.upload(content, path, type);
// 保存到数据库
FileDO file = new FileDO();
file.setConfigId(client.getId());
file.setName(name);
file.setPath(path);
file.setUrl(url);
file.setType(type);
file.setSize(content.length);
fileMapper.insert(file);
FileCreateRespVo fileCreateRespVo = new FileCreateRespVo();
fileCreateRespVo.setUrl(url);
fileCreateRespVo.setId(file.getId());
return fileCreateRespVo;
}
}