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 50f878672..4e38d9d0c 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 @@ -27,6 +27,7 @@ import top.continew.starter.data.mp.service.IService; import top.continew.starter.extension.crud.service.BaseService; +import java.io.File; import java.io.IOException; import java.time.LocalDate; import java.util.List; @@ -73,6 +74,40 @@ default FileInfo upload(MultipartFile file, String parentPath) throws IOExceptio */ FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException; + /** + * 上传到默认存储 + * + * @param file 文件信息 + * @return 文件信息 + * @throws IOException / + */ + default FileInfo upload(File file) throws IOException { + return upload(file, getDefaultParentPath(), null); + } + + /** + * 上传到默认存储 + * + * @param file 文件信息 + * @param parentPath 上级目录 + * @return 文件信息 + * @throws IOException / + */ + default FileInfo upload(File file, String parentPath) throws IOException { + return upload(file, parentPath, null); + } + + /** + * 上传到指定存储 + * + * @param file 文件信息 + * @param parentPath 上级目录 + * @param storageCode 存储编码 + * @return 文件信息 + * @throws IOException / + */ + FileInfo upload(File file, String parentPath, String storageCode) throws IOException; + /** * 创建目录 * 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 33104a05d..7028f8fbe 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 @@ -47,6 +47,7 @@ import top.continew.starter.core.validation.ValidationUtils; import top.continew.starter.extension.crud.service.BaseServiceImpl; +import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; @@ -93,6 +94,19 @@ public void beforeDelete(List ids) { public FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException { // 校验文件格式 String extName = FileNameUtil.extName(file.getOriginalFilename()); + return getFileInfo(file, parentPath, storageCode, extName); + } + + /** + * 上传文件并返回上传后的文件信息 + * + * @param file + * @param parentPath + * @param storageCode + * @param extName + * @return + */ + private FileInfo getFileInfo(Object file, String parentPath, String storageCode, String extName) { List allExtensions = FileTypeEnum.getAllExtensions(); CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String .join(StringConstants.COMMA, allExtensions)); @@ -131,6 +145,13 @@ public void finish() { return uploadPretreatment.upload(); } + @Override + public FileInfo upload(File file, String parentPath, String storageCode) throws IOException { + // 校验文件格式 + String extName = FileNameUtil.extName(file.getName()); + return getFileInfo(file, parentPath, storageCode, extName); + } + @Override public Long createDir(FileReq req) { String parentPath = req.getParentPath();