refactor: 使用分组校验优化存储管理

This commit is contained in:
2024-07-18 22:35:37 +08:00
parent ce1acea153
commit 3a23db1a4b
4 changed files with 60 additions and 17 deletions

View File

@@ -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;
/**

View File

@@ -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<StorageMapper, StorageDO
@Override
public void load(StorageReq req) {
CopyOnWriteArrayList<FileStorage> 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<StorageMapper, StorageDO
.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

View File

@@ -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 {
}
}
}

View File

@@ -96,7 +96,6 @@ public class CommonController {
return R.ok(roleService.listDict(query, sortQuery));
}
@SaIgnore
@Operation(summary = "查询字典", description = "查询字典列表")
@Parameter(name = "code", description = "字典编码", example = "notice_type", in = ParameterIn.PATH)
@GetMapping("/dict/{code}")