diff --git a/continew-common/src/main/java/top/continew/admin/common/constant/RegexConstants.java b/continew-common/src/main/java/top/continew/admin/common/constant/RegexConstants.java index b73e5e56..82bb04e1 100644 --- a/continew-common/src/main/java/top/continew/admin/common/constant/RegexConstants.java +++ b/continew-common/src/main/java/top/continew/admin/common/constant/RegexConstants.java @@ -59,6 +59,11 @@ public class RegexConstants { */ public static final String PACKAGE_NAME = "^(?:[a-zA-Z_][a-zA-Z0-9_]*\\.)*[a-zA-Z_][a-zA-Z0-9_]*$"; + /** + * HTTP 域名 URL 正则 + */ + public static final String HTTP_DOMAIN_URL = "^(https?:\\/\\/)([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(\\/[^\\s]*)?$"; + private RegexConstants() { } } diff --git a/continew-server/src/main/java/top/continew/admin/ContiNewAdminApplication.java b/continew-server/src/main/java/top/continew/admin/ContiNewAdminApplication.java index da4b4f94..c0cae737 100644 --- a/continew-server/src/main/java/top/continew/admin/ContiNewAdminApplication.java +++ b/continew-server/src/main/java/top/continew/admin/ContiNewAdminApplication.java @@ -79,7 +79,8 @@ public class ContiNewAdminApplication implements ApplicationRunner { log.info("----------------------------------------------"); log.info("{} service started successfully.", projectProperties.getName()); log.info("Profile: {}", SpringUtil.getProperty("spring.profiles.active")); - log.info("项目版本: v{} (ContiNew Starter: v{})", projectProperties.getVersion(), SpringUtil.getProperty("project.starter")); + log.info("项目版本: v{} (ContiNew Starter: v{})", projectProperties.getVersion(), SpringUtil + .getProperty("project.starter")); log.info("API 地址: {}", baseUrl); Knife4jProperties knife4jProperties = SpringUtil.getBean(Knife4jProperties.class); if (!knife4jProperties.isProduction()) { diff --git a/continew-system/src/main/java/top/continew/admin/system/enums/StorageTypeEnum.java b/continew-system/src/main/java/top/continew/admin/system/enums/StorageTypeEnum.java index 25a23bef..d38644bd 100644 --- a/continew-system/src/main/java/top/continew/admin/system/enums/StorageTypeEnum.java +++ b/continew-system/src/main/java/top/continew/admin/system/enums/StorageTypeEnum.java @@ -16,9 +16,11 @@ package top.continew.admin.system.enums; +import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import lombok.Getter; import lombok.RequiredArgsConstructor; +import top.continew.admin.common.constant.RegexConstants; import top.continew.admin.system.model.req.StorageReq; import top.continew.admin.system.validation.ValidationGroup; import top.continew.starter.core.constant.StringConstants; @@ -62,8 +64,8 @@ public enum StorageTypeEnum implements BaseEnum { @Override public void validate(StorageReq req) { ValidationUtils.validate(req, ValidationGroup.Storage.OSS.class); - ValidationUtils.throwIf(StrUtil.isNotBlank(req.getDomain()) && !URLUtils.isHttpUrl(req - .getDomain()), "域名格式不正确"); + ValidationUtils.throwIf(StrUtil.isNotBlank(req.getDomain()) && !ReUtil + .isMatch(RegexConstants.HTTP_DOMAIN_URL, req.getDomain()), "域名格式不正确"); } }; diff --git a/continew-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java b/continew-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java index 7028f8fb..17803cb7 100644 --- a/continew-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java +++ b/continew-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java @@ -48,7 +48,6 @@ 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; import java.util.stream.Collectors; @@ -91,65 +90,13 @@ public class FileServiceImpl extends BaseServiceImpl allExtensions = FileTypeEnum.getAllExtensions(); - CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String - .join(StringConstants.COMMA, allExtensions)); - // 构建上传预处理对象 - StorageDO storage = storageService.getByCode(storageCode); - CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(storage.getStatus()), "请先启用存储 [{}]", storage.getCode()); - UploadPretreatment uploadPretreatment = fileStorageService.of(file) - .setPlatform(storage.getCode()) - .setHashCalculatorSha256(true) - .putAttr(ClassUtil.getClassName(StorageDO.class, false), storage) - .setPath(this.pretreatmentPath(parentPath)); - // 图片文件生成缩略图 - if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) { - uploadPretreatment.setIgnoreThumbnailException(true, true); - uploadPretreatment.thumbnail(img -> img.size(100, 100)); - } - uploadPretreatment.setProgressMonitor(new ProgressListener() { - @Override - public void start() { - log.info("开始上传"); - } - - @Override - public void progress(long progressSize, Long allSize) { - log.info("已上传 [{}],总大小 [{}]", progressSize, allSize); - } - - @Override - public void finish() { - log.info("上传结束"); - } - }); - // 创建父级目录 - this.createParentDir(parentPath, storage); - // 上传 - return uploadPretreatment.upload(); + public FileInfo upload(MultipartFile file, String parentPath, String storageCode) { + return this.upload(file, parentPath, storageCode, FileNameUtil.extName(file.getOriginalFilename())); } @Override - public FileInfo upload(File file, String parentPath, String storageCode) throws IOException { - // 校验文件格式 - String extName = FileNameUtil.extName(file.getName()); - return getFileInfo(file, parentPath, storageCode, extName); + public FileInfo upload(File file, String parentPath, String storageCode) { + return this.upload(file, parentPath, storageCode, FileNameUtil.extName(file.getName())); } @Override @@ -251,6 +198,54 @@ public class FileServiceImpl extends BaseServiceImpl allExtensions = FileTypeEnum.getAllExtensions(); + CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String + .join(StringConstants.COMMA, allExtensions)); + // 构建上传预处理对象 + StorageDO storage = storageService.getByCode(storageCode); + CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(storage.getStatus()), "请先启用存储 [{}]", storage.getCode()); + UploadPretreatment uploadPretreatment = fileStorageService.of(file) + .setPlatform(storage.getCode()) + .setHashCalculatorSha256(true) + .putAttr(ClassUtil.getClassName(StorageDO.class, false), storage) + .setPath(this.pretreatmentPath(parentPath)); + // 图片文件生成缩略图 + if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) { + uploadPretreatment.setIgnoreThumbnailException(true, true); + uploadPretreatment.thumbnail(img -> img.size(100, 100)); + } + uploadPretreatment.setProgressMonitor(new ProgressListener() { + @Override + public void start() { + log.info("开始上传"); + } + + @Override + public void progress(long progressSize, Long allSize) { + log.info("已上传 [{}],总大小 [{}]", progressSize, allSize); + } + + @Override + public void finish() { + log.info("上传结束"); + } + }); + // 创建父级目录 + this.createParentDir(parentPath, storage); + // 上传 + return uploadPretreatment.upload(); + } + /** * 处理路径 * diff --git a/continew-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java b/continew-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java index d3c99450..c627084e 100644 --- a/continew-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java +++ b/continew-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java @@ -198,7 +198,6 @@ public class StorageServiceImpl extends BaseServiceImpl