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 8abbbb459..491846fbc 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 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 97312df85..bf0022165 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.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 StorageDO getByCode(String code) { @Override public void load(StorageReq req) { CopyOnWriteArrayList 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 void load(StorageReq req) { .singletonList(config))); SpringWebUtils.registerResourceHandler(MapUtil.of(URLUtil.url(req.getDomain()).getPath(), bucketName)); } else if (StorageTypeEnum.S3.equals(type)) { - String accessKey = req.getAccessKey(); - String secretKey = req.getSecretKey(); - String endpoint = req.getEndpoint(); - ValidationUtils.throwIfBlank(accessKey, "访问密钥不能为空"); - ValidationUtils.throwIfBlank(secretKey, "私有密钥不能为空"); - ValidationUtils.throwIfBlank(endpoint, "终端节点不能为空"); - ValidationUtils.throwIfBlank(bucketName, "桶名称不能为空"); - ValidationUtils.throwIfBlank(domain, "域名不能为空"); - ValidationUtils.throwIf(!URLUtils.isHttpUrl(domain), "域名格式错误"); + ValidationUtils.validate(req, ValidateGroup.Storage.S3.class); FileStorageProperties.AmazonS3Config config = new FileStorageProperties.AmazonS3Config(); config.setPlatform(req.getCode()); - config.setAccessKey(accessKey); - config.setSecretKey(secretKey); - config.setEndPoint(endpoint); + config.setAccessKey(req.getAccessKey()); + config.setSecretKey(req.getSecretKey()); + config.setEndPoint(req.getEndpoint()); config.setBucketName(bucketName); config.setDomain(domain); fileStorageList.addAll(FileStorageServiceBuilder.buildAmazonS3FileStorage(Collections diff --git a/continew-admin-system/src/main/java/top/continew/admin/system/util/ValidateGroup.java b/continew-admin-system/src/main/java/top/continew/admin/system/util/ValidateGroup.java new file mode 100644 index 000000000..aa30c8d3f --- /dev/null +++ b/continew-admin-system/src/main/java/top/continew/admin/system/util/ValidateGroup.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.admin.system.util; + +import jakarta.validation.groups.Default; + +/** + * 分组校验 + * + * @author Charles7c + * @since 2024/7/3 22:01 + */ +public interface ValidateGroup extends Default { + + /** + * 分组校验-增删改查 + */ + interface Storage extends ValidateGroup { + /** + * 本地存储 + */ + interface Local extends Storage { + } + + /** + * 兼容S3协议存储 + */ + interface S3 extends Storage { + } + } +} \ No newline at end of file diff --git a/continew-admin-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java b/continew-admin-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java index 62601f3cf..7c4525fb7 100644 --- a/continew-admin-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java +++ b/continew-admin-webapi/src/main/java/top/continew/admin/controller/common/CommonController.java @@ -96,7 +96,6 @@ public R> listRoleDict(RoleQuery query, SortQuery sortQuery return R.ok(roleService.listDict(query, sortQuery)); } - @SaIgnore @Operation(summary = "查询字典", description = "查询字典列表") @Parameter(name = "code", description = "字典编码", example = "notice_type", in = ParameterIn.PATH) @GetMapping("/dict/{code}")