fix(system/file): 修复支持任意格式上传错误

This commit is contained in:
2025-05-06 22:58:17 +08:00
parent 5bc657ad88
commit a2e156aae8
6 changed files with 35 additions and 12 deletions

View File

@@ -82,4 +82,13 @@ public enum FileTypeEnum implements BaseEnum<Integer> {
.findFirst()
.orElse(FileTypeEnum.UNKNOWN);
}
/**
* 获取所有扩展名
*
* @return 所有扩展名
*/
public static List<String> getAllExtensions() {
return Arrays.stream(FileTypeEnum.values()).flatMap(t -> t.getExtensions().stream()).toList();
}
}

View File

@@ -28,6 +28,7 @@ import top.continew.starter.data.mp.service.IService;
import top.continew.starter.extension.crud.model.resp.IdResp;
import top.continew.starter.extension.crud.service.BaseService;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
@@ -44,8 +45,9 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
*
* @param file 文件信息
* @return 文件信息
* @throws IOException /
*/
default FileInfo upload(MultipartFile file) {
default FileInfo upload(MultipartFile file) throws IOException {
return upload(file, getDefaultFilePath(), null);
}
@@ -55,8 +57,9 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @param file 文件信息
* @param path 文件路径
* @return 文件信息
* @throws IOException /
*/
default FileInfo upload(MultipartFile file, String path) {
default FileInfo upload(MultipartFile file, String path) throws IOException {
return upload(file, path, null);
}
@@ -67,8 +70,9 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @param path 文件路径
* @param storageCode 存储编码
* @return 文件信息
* @throws IOException /
*/
FileInfo upload(MultipartFile file, String path, String storageCode);
FileInfo upload(MultipartFile file, String path, String storageCode) throws IOException;
/**
* 根据存储 ID 列表查询

View File

@@ -85,8 +85,9 @@ public interface UserService extends BaseService<UserResp, UserDetailResp, UserQ
* @param avatar 头像文件
* @param id ID
* @return 新头像路径
* @throws IOException /
*/
String updateAvatar(MultipartFile avatar, Long id);
String updateAvatar(MultipartFile avatar, Long id) throws IOException;
/**
* 修改基础信息

View File

@@ -47,6 +47,7 @@ import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.model.resp.IdResp;
import top.continew.starter.extension.crud.service.BaseServiceImpl;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -80,7 +81,13 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
}
@Override
public FileInfo upload(MultipartFile file, String path, String storageCode) {
public FileInfo upload(MultipartFile file, String path, String storageCode) throws IOException {
// 校验文件格式
String extName = FileNameUtil.extName(file.getOriginalFilename());
List<String> allExtensions = FileTypeEnum.getAllExtensions();
CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String
.join(StringConstants.CHINESE_COMMA, allExtensions));
// 获取存储信息
StorageDO storage;
if (StrUtil.isBlank(storageCode)) {
storage = storageService.getDefaultStorage();
@@ -89,13 +96,14 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
storage = storageService.getByCode(storageCode);
CheckUtils.throwIfNotExists(storage, "StorageDO", "Code", storageCode);
}
// 构建上传预处理对象
UploadPretreatment uploadPretreatment = fileStorageService.of(file)
.setPlatform(storage.getCode())
.setHashCalculatorSha256(true)
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage)
.setPath(path);
// 图片文件生成缩略图
if (FileTypeEnum.IMAGE.getExtensions().contains(FileNameUtil.extName(file.getOriginalFilename()))) {
if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) {
uploadPretreatment.thumbnail(img -> img.size(100, 100));
}
uploadPretreatment.setProgressMonitor(new ProgressListener() {
@@ -115,7 +123,6 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
}
});
return uploadPretreatment.upload();
}
@Override

View File

@@ -389,7 +389,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
}
@Override
public String updateAvatar(MultipartFile avatarFile, Long id) {
public String updateAvatar(MultipartFile avatarFile, Long id) throws IOException {
String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename());
CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportSuffix), "头像仅支持 {} 格式的图片", String
.join(StringConstants.CHINESE_COMMA, avatarSupportSuffix));

View File

@@ -36,11 +36,13 @@ import top.continew.admin.system.model.query.*;
import top.continew.admin.system.model.resp.file.FileUploadResp;
import top.continew.admin.system.service.*;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.StrUtils;
import top.continew.starter.core.validation.ValidationUtils;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import top.continew.starter.log.annotation.Log;
import java.io.IOException;
import java.util.List;
/**
@@ -67,11 +69,11 @@ public class CommonController {
@Operation(summary = "上传文件", description = "上传文件")
@PostMapping("/file")
public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file, String path) {
public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file, String path) throws IOException {
ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
FileInfo fileInfo = fileService.upload(file, StrUtil.isNotBlank(path)
? StrUtil.appendIfMissing(path, StringConstants.SLASH)
: "/");
String fixedPath = StrUtils.blankToDefault(path, StringConstants.SLASH, p -> StrUtil
.appendIfMissing(p, StringConstants.SLASH));
FileInfo fileInfo = fileService.upload(file, fixedPath);
return FileUploadResp.builder()
.id(fileInfo.getId())
.url(fileInfo.getUrl())