Skip to content

Commit 9b79990

Browse files
authored
feat(system/file): 文件管理呈目录形式展示 (#151)
1 parent be5bfc8 commit 9b79990

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class FileRecorderImpl implements FileRecorder {
5656

5757
/**
5858
* 文件信息存储
59-
*
59+
*
6060
* @param fileInfo 文件信息对象
6161
* @return 是否保存成功
6262
*/
@@ -71,27 +71,25 @@ public boolean save(FileInfo fileInfo) {
7171
? StrUtil.subBefore(originalFilename, StringConstants.DOT, true)
7272
: originalFilename);
7373
StorageDO storage = (StorageDO)fileInfo.getAttr().get(ClassUtil.getClassName(StorageDO.class, false));
74-
String domain = StrUtil.appendIfMissing(storage.getDomain(), StringConstants.SLASH);
74+
String filePath = StrUtil.appendIfMissing(fileInfo.getPath(), StringConstants.SLASH);
7575
// 处理fileInfo中存储的地址
76-
fileInfo.setUrl(URLUtil.normalize(domain + fileInfo.getPath() + fileInfo.getFilename()));
77-
fileInfo.setThUrl(URLUtil.normalize(domain + fileInfo.getPath() + fileInfo.getThFilename()));
76+
fileInfo.setUrl(URLUtil.normalize(storage.getDomain() + filePath + fileInfo.getFilename()));
77+
fileInfo.setThUrl(URLUtil.normalize(storage.getDomain() + filePath + fileInfo.getThFilename()));
7878
file.setUrl(fileInfo.getUrl());
7979
file.setSize(fileInfo.getSize());
8080
String absPath = fileInfo.getPath();
81-
if (absPath.endsWith(StringConstants.SLASH)) {
82-
String tempAbsPath = absPath.substring(0, absPath.length() - 1);
83-
String[] pathArr = tempAbsPath.split(StringConstants.SLASH);
84-
if (pathArr.length > 1) {
85-
file.setParentPath(pathArr[pathArr.length - 1]);
86-
} else {
87-
file.setParentPath(StringConstants.SLASH);
88-
}
81+
String tempAbsPath = absPath.length() > 1 ? StrUtil.removeSuffix(absPath, StringConstants.SLASH) : absPath;
82+
String[] pathArr = tempAbsPath.split(StringConstants.SLASH);
83+
if (pathArr.length > 1) {
84+
file.setParentPath(pathArr[pathArr.length - 1]);
85+
} else {
86+
file.setParentPath(StringConstants.SLASH);
8987
}
90-
file.setAbsPath(fileInfo.getPath());
88+
file.setAbsPath(tempAbsPath);
9189
file.setExtension(fileInfo.getExt());
9290
file.setType(FileTypeEnum.getByExtension(file.getExtension()));
9391
file.setContentType(fileInfo.getContentType());
94-
file.setMd5(fileInfo.getHashInfo().getMd5());
92+
file.setMd5(fileInfo.getHashInfo().getSha256());
9593
file.setMetadata(JSONUtil.toJsonStr(fileInfo.getMetadata()));
9694
file.setThumbnailUrl(fileInfo.getThUrl());
9795
file.setThumbnailSize(fileInfo.getThSize());

continew-module-system/src/main/java/top/continew/admin/system/model/req/FileReq.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@ public class FileReq implements Serializable {
4444
@NotBlank(message = "文件名称不能为空")
4545
@Length(max = 255, message = "文件名称长度不能超过 {max} 个字符")
4646
private String name;
47+
48+
/**
49+
* 上级目录
50+
*/
51+
@Schema(description = "上级目录", example = "25")
52+
private String parentPath;
4753
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import top.continew.admin.system.model.resp.file.FileStatisticsResp;
2626
import top.continew.starter.core.constant.StringConstants;
2727
import top.continew.starter.data.mp.service.IService;
28+
import top.continew.starter.extension.crud.model.resp.IdResp;
2829
import top.continew.starter.extension.crud.service.BaseService;
2930

3031
import java.time.LocalDate;
@@ -94,4 +95,8 @@ default String getDefaultFilePath() {
9495
return today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
9596
.getDayOfMonth() + StringConstants.SLASH;
9697
}
98+
99+
FileResp check(String fileHash);
100+
101+
IdResp<Long> createDir(FileReq req);
97102
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import top.continew.starter.core.util.StrUtils;
4545
import top.continew.starter.core.util.URLUtils;
4646
import top.continew.starter.core.validation.CheckUtils;
47+
import top.continew.starter.extension.crud.model.resp.IdResp;
4748
import top.continew.starter.extension.crud.service.BaseServiceImpl;
4849

4950
import java.util.List;
@@ -91,7 +92,9 @@ public FileInfo upload(MultipartFile file, String path, String storageCode) {
9192
UploadPretreatment uploadPretreatment = fileStorageService.of(file)
9293
.setPlatform(storage.getCode())
9394
.setHashCalculatorMd5(true)
95+
.setHashCalculatorSha256(true)
9496
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage)
97+
// .setPath(StrUtil.removePrefix(path, StringConstants.SLASH));
9598
.setPath(path);
9699
// 图片文件生成缩略图
97100
if (FileTypeEnum.IMAGE.getExtensions().contains(FileNameUtil.extName(file.getOriginalFilename()))) {
@@ -138,6 +141,40 @@ public FileStatisticsResp statistics() {
138141
return resp;
139142
}
140143

144+
@Override
145+
public FileResp check(String fileHash) {
146+
FileDO file = baseMapper.lambdaQuery().eq(FileDO::getMd5, fileHash).one();
147+
if (file != null) {
148+
return get(file.getId());
149+
}
150+
return null;
151+
}
152+
153+
@Override
154+
public IdResp<Long> createDir(FileReq req) {
155+
StorageDO storage = storageService.getDefaultStorage();
156+
FileDO fileDo = new FileDO();
157+
fileDo.setName(req.getName());
158+
fileDo.setSize(0L);
159+
fileDo.setUrl(storage.getDomain() + req.getParentPath() + req.getName());
160+
String absPath = req.getParentPath();
161+
String tempAbsPath = absPath.length() > 1 ? StrUtil.removeSuffix(absPath, StringConstants.SLASH) : absPath;
162+
String[] pathArr = tempAbsPath.split(StringConstants.SLASH);
163+
if (pathArr.length > 1) {
164+
fileDo.setParentPath(pathArr[pathArr.length - 1]);
165+
} else {
166+
fileDo.setParentPath(StringConstants.SLASH);
167+
}
168+
fileDo.setAbsPath(tempAbsPath);
169+
fileDo.setExtension("dir");
170+
fileDo.setContentType("");
171+
fileDo.setType(FileTypeEnum.DIR);
172+
fileDo.setMd5("");
173+
fileDo.setStorageId(storage.getId());
174+
baseMapper.insert(fileDo);
175+
return new IdResp<>(fileDo.getId());
176+
}
177+
141178
@Override
142179
protected void fill(Object obj) {
143180
super.fill(obj);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import top.continew.admin.system.model.query.*;
3636
import top.continew.admin.system.model.resp.file.FileUploadResp;
3737
import top.continew.admin.system.service.*;
38+
import top.continew.starter.core.constant.StringConstants;
3839
import top.continew.starter.core.validation.ValidationUtils;
3940
import top.continew.starter.extension.crud.model.query.SortQuery;
4041
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
@@ -66,9 +67,11 @@ public class CommonController {
6667

6768
@Operation(summary = "上传文件", description = "上传文件")
6869
@PostMapping("/file")
69-
public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file) {
70+
public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file, String path) {
7071
ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
71-
FileInfo fileInfo = fileService.upload(file);
72+
FileInfo fileInfo = fileService.upload(file, StrUtil.isNotBlank(path)
73+
? StrUtil.appendIfMissing(path, StringConstants.SLASH)
74+
: "/");
7275
return FileUploadResp.builder()
7376
.id(fileInfo.getId())
7477
.url(fileInfo.getUrl())

continew-webapi/src/main/java/top/continew/admin/controller/system/FileController.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import io.swagger.v3.oas.annotations.Operation;
2121
import io.swagger.v3.oas.annotations.tags.Tag;
2222
import lombok.RequiredArgsConstructor;
23-
import org.springframework.web.bind.annotation.GetMapping;
24-
import org.springframework.web.bind.annotation.RestController;
23+
import org.springframework.web.bind.annotation.*;
2524
import top.continew.admin.common.controller.BaseController;
2625
import top.continew.admin.system.model.query.FileQuery;
2726
import top.continew.admin.system.model.req.FileReq;
@@ -30,6 +29,7 @@
3029
import top.continew.admin.system.service.FileService;
3130
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
3231
import top.continew.starter.extension.crud.enums.Api;
32+
import top.continew.starter.extension.crud.model.resp.IdResp;
3333
import top.continew.starter.log.annotation.Log;
3434

3535
/**
@@ -51,4 +51,19 @@ public class FileController extends BaseController<FileService, FileResp, FileRe
5151
public FileStatisticsResp statistics() {
5252
return baseService.statistics();
5353
}
54+
55+
@Log(ignore = true)
56+
@Operation(summary = "检测文件是否存在", description = "检测文件是否存在")
57+
@SaCheckPermission("system:file:check")
58+
@GetMapping("/check")
59+
public FileResp checkFile(String fileHash) {
60+
return baseService.check(fileHash);
61+
}
62+
63+
@Operation(summary = "创建文件夹", description = "创建文件夹")
64+
@ResponseBody
65+
@PostMapping("/createDir")
66+
public IdResp<Long> createDir(@RequestBody FileReq req) {
67+
return baseService.createDir(req);
68+
}
5469
}

0 commit comments

Comments
 (0)