Skip to content

Commit a2e156a

Browse files
committed
fix(system/file): 修复支持任意格式上传错误
1 parent 5bc657a commit a2e156a

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

continew-module-system/src/main/java/top/continew/admin/system/enums/FileTypeEnum.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ public static FileTypeEnum getByExtension(String extension) {
8282
.findFirst()
8383
.orElse(FileTypeEnum.UNKNOWN);
8484
}
85+
86+
/**
87+
* 获取所有扩展名
88+
*
89+
* @return 所有扩展名
90+
*/
91+
public static List<String> getAllExtensions() {
92+
return Arrays.stream(FileTypeEnum.values()).flatMap(t -> t.getExtensions().stream()).toList();
93+
}
8594
}

continew-module-system/src/main/java/top/continew/admin/system/service/FileService.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import top.continew.starter.extension.crud.model.resp.IdResp;
2929
import top.continew.starter.extension.crud.service.BaseService;
3030

31+
import java.io.IOException;
3132
import java.time.LocalDate;
3233
import java.util.List;
3334

@@ -44,8 +45,9 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
4445
*
4546
* @param file 文件信息
4647
* @return 文件信息
48+
* @throws IOException /
4749
*/
48-
default FileInfo upload(MultipartFile file) {
50+
default FileInfo upload(MultipartFile file) throws IOException {
4951
return upload(file, getDefaultFilePath(), null);
5052
}
5153

@@ -55,8 +57,9 @@ default FileInfo upload(MultipartFile file) {
5557
* @param file 文件信息
5658
* @param path 文件路径
5759
* @return 文件信息
60+
* @throws IOException /
5861
*/
59-
default FileInfo upload(MultipartFile file, String path) {
62+
default FileInfo upload(MultipartFile file, String path) throws IOException {
6063
return upload(file, path, null);
6164
}
6265

@@ -67,8 +70,9 @@ default FileInfo upload(MultipartFile file, String path) {
6770
* @param path 文件路径
6871
* @param storageCode 存储编码
6972
* @return 文件信息
73+
* @throws IOException /
7074
*/
71-
FileInfo upload(MultipartFile file, String path, String storageCode);
75+
FileInfo upload(MultipartFile file, String path, String storageCode) throws IOException;
7276

7377
/**
7478
* 根据存储 ID 列表查询

continew-module-system/src/main/java/top/continew/admin/system/service/UserService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ public interface UserService extends BaseService<UserResp, UserDetailResp, UserQ
8585
* @param avatar 头像文件
8686
* @param id ID
8787
* @return 新头像路径
88+
* @throws IOException /
8889
*/
89-
String updateAvatar(MultipartFile avatar, Long id);
90+
String updateAvatar(MultipartFile avatar, Long id) throws IOException;
9091

9192
/**
9293
* 修改基础信息

continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import top.continew.starter.extension.crud.model.resp.IdResp;
4848
import top.continew.starter.extension.crud.service.BaseServiceImpl;
4949

50+
import java.io.IOException;
5051
import java.util.List;
5152
import java.util.Map;
5253
import java.util.stream.Collectors;
@@ -80,7 +81,13 @@ protected void beforeDelete(List<Long> ids) {
8081
}
8182

8283
@Override
83-
public FileInfo upload(MultipartFile file, String path, String storageCode) {
84+
public FileInfo upload(MultipartFile file, String path, String storageCode) throws IOException {
85+
// 校验文件格式
86+
String extName = FileNameUtil.extName(file.getOriginalFilename());
87+
List<String> allExtensions = FileTypeEnum.getAllExtensions();
88+
CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String
89+
.join(StringConstants.CHINESE_COMMA, allExtensions));
90+
// 获取存储信息
8491
StorageDO storage;
8592
if (StrUtil.isBlank(storageCode)) {
8693
storage = storageService.getDefaultStorage();
@@ -89,13 +96,14 @@ public FileInfo upload(MultipartFile file, String path, String storageCode) {
8996
storage = storageService.getByCode(storageCode);
9097
CheckUtils.throwIfNotExists(storage, "StorageDO", "Code", storageCode);
9198
}
99+
// 构建上传预处理对象
92100
UploadPretreatment uploadPretreatment = fileStorageService.of(file)
93101
.setPlatform(storage.getCode())
94102
.setHashCalculatorSha256(true)
95103
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage)
96104
.setPath(path);
97105
// 图片文件生成缩略图
98-
if (FileTypeEnum.IMAGE.getExtensions().contains(FileNameUtil.extName(file.getOriginalFilename()))) {
106+
if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) {
99107
uploadPretreatment.thumbnail(img -> img.size(100, 100));
100108
}
101109
uploadPretreatment.setProgressMonitor(new ProgressListener() {
@@ -115,7 +123,6 @@ public void finish() {
115123
}
116124
});
117125
return uploadPretreatment.upload();
118-
119126
}
120127

121128
@Override

continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public void updateRole(UserRoleUpdateReq updateReq, Long id) {
389389
}
390390

391391
@Override
392-
public String updateAvatar(MultipartFile avatarFile, Long id) {
392+
public String updateAvatar(MultipartFile avatarFile, Long id) throws IOException {
393393
String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename());
394394
CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportSuffix), "头像仅支持 {} 格式的图片", String
395395
.join(StringConstants.CHINESE_COMMA, avatarSupportSuffix));

continew-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
import top.continew.admin.system.model.resp.file.FileUploadResp;
3737
import top.continew.admin.system.service.*;
3838
import top.continew.starter.core.constant.StringConstants;
39+
import top.continew.starter.core.util.StrUtils;
3940
import top.continew.starter.core.validation.ValidationUtils;
4041
import top.continew.starter.extension.crud.model.query.SortQuery;
4142
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
4243
import top.continew.starter.log.annotation.Log;
4344

45+
import java.io.IOException;
4446
import java.util.List;
4547

4648
/**
@@ -67,11 +69,11 @@ public class CommonController {
6769

6870
@Operation(summary = "上传文件", description = "上传文件")
6971
@PostMapping("/file")
70-
public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file, String path) {
72+
public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file, String path) throws IOException {
7173
ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
72-
FileInfo fileInfo = fileService.upload(file, StrUtil.isNotBlank(path)
73-
? StrUtil.appendIfMissing(path, StringConstants.SLASH)
74-
: "/");
74+
String fixedPath = StrUtils.blankToDefault(path, StringConstants.SLASH, p -> StrUtil
75+
.appendIfMissing(p, StringConstants.SLASH));
76+
FileInfo fileInfo = fileService.upload(file, fixedPath);
7577
return FileUploadResp.builder()
7678
.id(fileInfo.getId())
7779
.url(fileInfo.getUrl())

0 commit comments

Comments
 (0)