Skip to content

Commit

Permalink
refactor: 使用分组校验优化存储管理
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Jul 18, 2024
1 parent ce1acea commit 3a23db1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,33 +71,39 @@ 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;

/**
* 终端节点
*/
@Schema(description = "终端节点", example = "")
@Length(max = 255, message = "终端节点长度不能超过 {max} 个字符")
@NotBlank(message = "终端节点不能为空", groups = ValidateGroup.Storage.S3.class)
private String endpoint;

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

/**
* 域名
*/
@Schema(description = "域名", example = "http://localhost:8000/file")
@Length(max = 255, message = "域名长度不能超过 {max} 个字符")
@NotBlank(message = "域名不能为空")
private String domain;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,13 +123,12 @@ public StorageDO getByCode(String code) {
@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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public R<List<LabelValueResp>> 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}")
Expand Down

0 comments on commit 3a23db1

Please sign in to comment.