文件上传更改
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled
yudao-ui-admin CI / build (14.x) (push) Has been cancelled
yudao-ui-admin CI / build (16.x) (push) Has been cancelled
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled
yudao-ui-admin CI / build (14.x) (push) Has been cancelled
yudao-ui-admin CI / build (16.x) (push) Has been cancelled
This commit is contained in:
parent
3b538ec9e9
commit
3c29866ec5
@ -13,7 +13,7 @@ public interface FileApi {
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
default String[] createFile(byte[] content) {
|
||||
default String createFile(byte[] content) {
|
||||
return createFile(null, null, content);
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public interface FileApi {
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
default String[] createFile(String path, byte[] content) {
|
||||
default String createFile(String path, byte[] content) {
|
||||
return createFile(null, path, content);
|
||||
}
|
||||
|
||||
@ -36,6 +36,6 @@ public interface FileApi {
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
String[] createFile(String name, String path, byte[] content);
|
||||
String createFile(String name, String path, byte[] content);
|
||||
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class FileApiImpl implements FileApi {
|
||||
private FileService fileService;
|
||||
|
||||
@Override
|
||||
public String[] createFile(String name, String path, byte[] content) {
|
||||
public String createFile(String name, String path, byte[] content) {
|
||||
return fileService.createFile(name, path, content);
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class FileController {
|
||||
public CommonResult<String[]> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
|
||||
MultipartFile file = uploadReqVO.getFile();
|
||||
String path = uploadReqVO.getPath();
|
||||
return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
|
||||
return success(fileService.createFile2(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
|
||||
}
|
||||
|
||||
@GetMapping("/presigned-url")
|
||||
|
@ -29,7 +29,7 @@ public class AppFileController {
|
||||
|
||||
@PostMapping("/upload")
|
||||
@Operation(summary = "上传文件")
|
||||
public CommonResult<String[]> uploadFile(AppFileUploadReqVO uploadReqVO) throws Exception {
|
||||
public CommonResult<String> uploadFile(AppFileUploadReqVO uploadReqVO) throws Exception {
|
||||
MultipartFile file = uploadReqVO.getFile();
|
||||
String path = uploadReqVO.getPath();
|
||||
return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
|
||||
|
@ -29,7 +29,8 @@ public interface FileService {
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
String[] createFile(String name, String path, byte[] content);
|
||||
String createFile(String name, String path, byte[] content);
|
||||
String[] createFile2(String name, String path, byte[] content);
|
||||
|
||||
/**
|
||||
* 创建文件
|
||||
|
@ -43,7 +43,7 @@ public class FileServiceImpl implements FileService {
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String[] createFile(String name, String path, byte[] content) {
|
||||
public String[] createFile2(String name, String path, byte[] content) {
|
||||
// 计算默认的 path 名
|
||||
String type = FileTypeUtils.getMineType(content, name);
|
||||
if (StrUtil.isEmpty(path)) {
|
||||
@ -73,6 +73,35 @@ public class FileServiceImpl implements FileService {
|
||||
liss[1] = url;
|
||||
return liss;
|
||||
}
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String createFile(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);
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long createFile(FileCreateReqVO createReqVO) {
|
||||
|
Loading…
Reference in New Issue
Block a user