Skip to content

Commit a2c1764

Browse files
authored
feat(system/file): 支持原生文件上传 (#166)
1 parent 1ef5417 commit a2c1764

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import top.continew.starter.data.mp.service.IService;
2828
import top.continew.starter.extension.crud.service.BaseService;
2929

30+
import java.io.File;
3031
import java.io.IOException;
3132
import java.time.LocalDate;
3233
import java.util.List;
@@ -73,6 +74,40 @@ default FileInfo upload(MultipartFile file, String parentPath) throws IOExceptio
7374
*/
7475
FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException;
7576

77+
/**
78+
* 上传到默认存储
79+
*
80+
* @param file 文件信息
81+
* @return 文件信息
82+
* @throws IOException /
83+
*/
84+
default FileInfo upload(File file) throws IOException {
85+
return upload(file, getDefaultParentPath(), null);
86+
}
87+
88+
/**
89+
* 上传到默认存储
90+
*
91+
* @param file 文件信息
92+
* @param parentPath 上级目录
93+
* @return 文件信息
94+
* @throws IOException /
95+
*/
96+
default FileInfo upload(File file, String parentPath) throws IOException {
97+
return upload(file, parentPath, null);
98+
}
99+
100+
/**
101+
* 上传到指定存储
102+
*
103+
* @param file 文件信息
104+
* @param parentPath 上级目录
105+
* @param storageCode 存储编码
106+
* @return 文件信息
107+
* @throws IOException /
108+
*/
109+
FileInfo upload(File file, String parentPath, String storageCode) throws IOException;
110+
76111
/**
77112
* 创建目录
78113
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import top.continew.starter.core.validation.ValidationUtils;
4848
import top.continew.starter.extension.crud.service.BaseServiceImpl;
4949

50+
import java.io.File;
5051
import java.io.IOException;
5152
import java.util.List;
5253
import java.util.Map;
@@ -93,6 +94,19 @@ public void beforeDelete(List<Long> ids) {
9394
public FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException {
9495
// 校验文件格式
9596
String extName = FileNameUtil.extName(file.getOriginalFilename());
97+
return getFileInfo(file, parentPath, storageCode, extName);
98+
}
99+
100+
/**
101+
* 上传文件并返回上传后的文件信息
102+
*
103+
* @param file
104+
* @param parentPath
105+
* @param storageCode
106+
* @param extName
107+
* @return
108+
*/
109+
private FileInfo getFileInfo(Object file, String parentPath, String storageCode, String extName) {
96110
List<String> allExtensions = FileTypeEnum.getAllExtensions();
97111
CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String
98112
.join(StringConstants.COMMA, allExtensions));
@@ -131,6 +145,13 @@ public void finish() {
131145
return uploadPretreatment.upload();
132146
}
133147

148+
@Override
149+
public FileInfo upload(File file, String parentPath, String storageCode) throws IOException {
150+
// 校验文件格式
151+
String extName = FileNameUtil.extName(file.getName());
152+
return getFileInfo(file, parentPath, storageCode, extName);
153+
}
154+
134155
@Override
135156
public Long createDir(FileReq req) {
136157
String parentPath = req.getParentPath();

0 commit comments

Comments
 (0)