From 3a23db1a4bdfefc26de67ed9e0317097699d5db6 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 18 Jul 2024 22:35:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E5=88=86?= =?UTF-8?q?=E7=BB=84=E6=A0=A1=E9=AA=8C=E4=BC=98=E5=8C=96=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/system/model/req/StorageReq.java | 7 +++ .../service/impl/StorageServiceImpl.java | 24 ++++------ .../admin/system/util/ValidateGroup.java | 45 +++++++++++++++++++ .../controller/common/CommonController.java | 1 - 4 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 continew-admin-system/src/main/java/top/continew/admin/system/util/ValidateGroup.java diff --git a/continew-admin-system/src/main/java/top/continew/admin/system/model/req/StorageReq.java b/continew-admin-system/src/main/java/top/continew/admin/system/model/req/StorageReq.java index 8abbbb45..491846fb 100644 --- a/continew-admin-system/src/main/java/top/continew/admin/system/model/req/StorageReq.java +++ b/continew-admin-system/src/main/java/top/continew/admin/system/model/req/StorageReq.java @@ -25,6 +25,7 @@ import org.hibernate.validator.constraints.Length; import top.continew.admin.common.constant.RegexConstants; import top.continew.admin.common.enums.DisEnableStatusEnum; import top.continew.admin.system.enums.StorageTypeEnum; +import top.continew.admin.system.util.ValidateGroup; import top.continew.starter.extension.crud.model.req.BaseReq; import java.io.Serial; @@ -70,12 +71,14 @@ public class StorageReq extends BaseReq { */ @Schema(description = "访问密钥", example = "") @Length(max = 255, message = "访问密钥长度不能超过 {max} 个字符") + @NotBlank(message = "访问密钥不能为空", groups = ValidateGroup.Storage.S3.class) private String accessKey; /** * 私有密钥 */ @Schema(description = "私有密钥", example = "") + @NotBlank(message = "私有密钥不能为空", groups = ValidateGroup.Storage.S3.class) private String secretKey; /** @@ -83,6 +86,7 @@ public class StorageReq extends BaseReq { */ @Schema(description = "终端节点", example = "") @Length(max = 255, message = "终端节点长度不能超过 {max} 个字符") + @NotBlank(message = "终端节点不能为空", groups = ValidateGroup.Storage.S3.class) private String endpoint; /** @@ -90,6 +94,8 @@ public class StorageReq extends BaseReq { */ @Schema(description = "桶名称", example = "C:/continew-admin/data/file/") @Length(max = 255, message = "桶名称长度不能超过 {max} 个字符") + @NotBlank(message = "桶名称不能为空", groups = ValidateGroup.Storage.S3.class) + @NotBlank(message = "存储路径不能为空", groups = ValidateGroup.Storage.Local.class) private String bucketName; /** @@ -97,6 +103,7 @@ public class StorageReq extends BaseReq { */ @Schema(description = "域名", example = "http://localhost:8000/file") @Length(max = 255, message = "域名长度不能超过 {max} 个字符") + @NotBlank(message = "域名不能为空") private String domain; /** diff --git a/continew-admin-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java b/continew-admin-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java index 97312df8..bf002216 100644 --- a/continew-admin-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java +++ b/continew-admin-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java @@ -37,6 +37,7 @@ import top.continew.admin.system.model.req.StorageReq; import top.continew.admin.system.model.resp.StorageResp; import top.continew.admin.system.service.FileService; import top.continew.admin.system.service.StorageService; +import top.continew.admin.system.util.ValidateGroup; import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.util.ExceptionUtils; import top.continew.starter.core.util.URLUtils; @@ -122,13 +123,12 @@ public class StorageServiceImpl extends BaseServiceImpl fileStorageList = fileStorageService.getFileStorageList(); - String bucketName = req.getBucketName(); String domain = req.getDomain(); + ValidationUtils.throwIf(!URLUtils.isHttpUrl(domain), "域名格式错误"); + String bucketName = req.getBucketName(); StorageTypeEnum type = req.getType(); if (StorageTypeEnum.LOCAL.equals(type)) { - ValidationUtils.throwIfBlank(bucketName, "存储路径不能为空"); - ValidationUtils.throwIfBlank(domain, "域名不能为空"); - ValidationUtils.throwIf(!URLUtils.isHttpUrl(domain), "域名格式错误"); + ValidationUtils.validate(req, ValidateGroup.Storage.Local.class); req.setBucketName(StrUtil.appendIfMissing(bucketName .replace(StringConstants.BACKSLASH, StringConstants.SLASH), StringConstants.SLASH)); FileStorageProperties.LocalPlusConfig config = new FileStorageProperties.LocalPlusConfig(); @@ -138,20 +138,12 @@ public class StorageServiceImpl extends BaseServiceImpl