From 9b79990cc0dc803de9f4181dad32d883679341b4 Mon Sep 17 00:00:00 2001 From: luoqiz Date: Wed, 16 Apr 2025 11:25:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(system/file):=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=88=E7=9B=AE=E5=BD=95=E5=BD=A2=E5=BC=8F?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=20(#151)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/config/file/FileRecorderImpl.java | 26 ++++++------- .../admin/system/model/req/FileReq.java | 6 +++ .../admin/system/service/FileService.java | 5 +++ .../system/service/impl/FileServiceImpl.java | 37 +++++++++++++++++++ .../controller/common/CommonController.java | 7 +++- .../controller/system/FileController.java | 19 +++++++++- 6 files changed, 82 insertions(+), 18 deletions(-) diff --git a/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java index dfaa593d..dc653f16 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java @@ -56,7 +56,7 @@ public class FileRecorderImpl implements FileRecorder { /** * 文件信息存储 - * + * * @param fileInfo 文件信息对象 * @return 是否保存成功 */ @@ -71,27 +71,25 @@ public class FileRecorderImpl implements FileRecorder { ? StrUtil.subBefore(originalFilename, StringConstants.DOT, true) : originalFilename); StorageDO storage = (StorageDO)fileInfo.getAttr().get(ClassUtil.getClassName(StorageDO.class, false)); - String domain = StrUtil.appendIfMissing(storage.getDomain(), StringConstants.SLASH); + String filePath = StrUtil.appendIfMissing(fileInfo.getPath(), StringConstants.SLASH); // 处理fileInfo中存储的地址 - fileInfo.setUrl(URLUtil.normalize(domain + fileInfo.getPath() + fileInfo.getFilename())); - fileInfo.setThUrl(URLUtil.normalize(domain + fileInfo.getPath() + fileInfo.getThFilename())); + fileInfo.setUrl(URLUtil.normalize(storage.getDomain() + filePath + fileInfo.getFilename())); + fileInfo.setThUrl(URLUtil.normalize(storage.getDomain() + filePath + fileInfo.getThFilename())); file.setUrl(fileInfo.getUrl()); file.setSize(fileInfo.getSize()); String absPath = fileInfo.getPath(); - if (absPath.endsWith(StringConstants.SLASH)) { - String tempAbsPath = absPath.substring(0, absPath.length() - 1); - String[] pathArr = tempAbsPath.split(StringConstants.SLASH); - if (pathArr.length > 1) { - file.setParentPath(pathArr[pathArr.length - 1]); - } else { - file.setParentPath(StringConstants.SLASH); - } + String tempAbsPath = absPath.length() > 1 ? StrUtil.removeSuffix(absPath, StringConstants.SLASH) : absPath; + String[] pathArr = tempAbsPath.split(StringConstants.SLASH); + if (pathArr.length > 1) { + file.setParentPath(pathArr[pathArr.length - 1]); + } else { + file.setParentPath(StringConstants.SLASH); } - file.setAbsPath(fileInfo.getPath()); + file.setAbsPath(tempAbsPath); file.setExtension(fileInfo.getExt()); file.setType(FileTypeEnum.getByExtension(file.getExtension())); file.setContentType(fileInfo.getContentType()); - file.setMd5(fileInfo.getHashInfo().getMd5()); + file.setMd5(fileInfo.getHashInfo().getSha256()); file.setMetadata(JSONUtil.toJsonStr(fileInfo.getMetadata())); file.setThumbnailUrl(fileInfo.getThUrl()); file.setThumbnailSize(fileInfo.getThSize()); diff --git a/continew-module-system/src/main/java/top/continew/admin/system/model/req/FileReq.java b/continew-module-system/src/main/java/top/continew/admin/system/model/req/FileReq.java index 3f7e145e..3c3915c3 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/model/req/FileReq.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/model/req/FileReq.java @@ -44,4 +44,10 @@ public class FileReq implements Serializable { @NotBlank(message = "文件名称不能为空") @Length(max = 255, message = "文件名称长度不能超过 {max} 个字符") private String name; + + /** + * 上级目录 + */ + @Schema(description = "上级目录", example = "25") + private String parentPath; } \ No newline at end of file diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/FileService.java b/continew-module-system/src/main/java/top/continew/admin/system/service/FileService.java index 3486d76e..c7a81288 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/FileService.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/FileService.java @@ -25,6 +25,7 @@ import top.continew.admin.system.model.resp.file.FileResp; import top.continew.admin.system.model.resp.file.FileStatisticsResp; import top.continew.starter.core.constant.StringConstants; 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.time.LocalDate; @@ -94,4 +95,8 @@ public interface FileService extends BaseService createDir(FileReq req); } \ No newline at end of file diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java index c36c2fd8..0251c47b 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java @@ -44,6 +44,7 @@ import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.util.StrUtils; import top.continew.starter.core.util.URLUtils; 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.util.List; @@ -91,7 +92,9 @@ public class FileServiceImpl extends BaseServiceImpl createDir(FileReq req) { + StorageDO storage = storageService.getDefaultStorage(); + FileDO fileDo = new FileDO(); + fileDo.setName(req.getName()); + fileDo.setSize(0L); + fileDo.setUrl(storage.getDomain() + req.getParentPath() + req.getName()); + String absPath = req.getParentPath(); + String tempAbsPath = absPath.length() > 1 ? StrUtil.removeSuffix(absPath, StringConstants.SLASH) : absPath; + String[] pathArr = tempAbsPath.split(StringConstants.SLASH); + if (pathArr.length > 1) { + fileDo.setParentPath(pathArr[pathArr.length - 1]); + } else { + fileDo.setParentPath(StringConstants.SLASH); + } + fileDo.setAbsPath(tempAbsPath); + fileDo.setExtension("dir"); + fileDo.setContentType(""); + fileDo.setType(FileTypeEnum.DIR); + fileDo.setMd5(""); + fileDo.setStorageId(storage.getId()); + baseMapper.insert(fileDo); + return new IdResp<>(fileDo.getId()); + } + @Override protected void fill(Object obj) { super.fill(obj); diff --git a/continew-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java b/continew-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java index 1d9bbf76..358d02da 100644 --- a/continew-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java +++ b/continew-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java @@ -35,6 +35,7 @@ import top.continew.admin.system.enums.OptionCategoryEnum; 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.validation.ValidationUtils; import top.continew.starter.extension.crud.model.query.SortQuery; import top.continew.starter.extension.crud.model.resp.LabelValueResp; @@ -66,9 +67,11 @@ public class CommonController { @Operation(summary = "上传文件", description = "上传文件") @PostMapping("/file") - public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file) { + public FileUploadResp upload(@NotNull(message = "文件不能为空") MultipartFile file, String path) { ValidationUtils.throwIf(file::isEmpty, "文件不能为空"); - FileInfo fileInfo = fileService.upload(file); + FileInfo fileInfo = fileService.upload(file, StrUtil.isNotBlank(path) + ? StrUtil.appendIfMissing(path, StringConstants.SLASH) + : "/"); return FileUploadResp.builder() .id(fileInfo.getId()) .url(fileInfo.getUrl()) diff --git a/continew-webapi/src/main/java/top/continew/admin/controller/system/FileController.java b/continew-webapi/src/main/java/top/continew/admin/controller/system/FileController.java index 78821350..0611b2d0 100644 --- a/continew-webapi/src/main/java/top/continew/admin/controller/system/FileController.java +++ b/continew-webapi/src/main/java/top/continew/admin/controller/system/FileController.java @@ -20,8 +20,7 @@ import cn.dev33.satoken.annotation.SaCheckPermission; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import top.continew.admin.common.controller.BaseController; import top.continew.admin.system.model.query.FileQuery; import top.continew.admin.system.model.req.FileReq; @@ -30,6 +29,7 @@ import top.continew.admin.system.model.resp.file.FileStatisticsResp; import top.continew.admin.system.service.FileService; import top.continew.starter.extension.crud.annotation.CrudRequestMapping; import top.continew.starter.extension.crud.enums.Api; +import top.continew.starter.extension.crud.model.resp.IdResp; import top.continew.starter.log.annotation.Log; /** @@ -51,4 +51,19 @@ public class FileController extends BaseController createDir(@RequestBody FileReq req) { + return baseService.createDir(req); + } } \ No newline at end of file