refactor: 优化存储管理

This commit is contained in:
2024-04-15 22:10:38 +08:00
parent e68c4455a8
commit 6beceaa9a2
16 changed files with 74 additions and 78 deletions

View File

@@ -20,9 +20,10 @@ import top.charles7c.continew.admin.system.model.entity.StorageDO;
import top.charles7c.continew.starter.data.mybatis.plus.base.BaseMapper;
/**
* 存储 Mapper
* 存储 Mapper
*
* @author Charles7c
* @since 2023/12/26 22:09
*/
public interface StorageMapper extends BaseMapper<StorageDO> {}
public interface StorageMapper extends BaseMapper<StorageDO> {
}

View File

@@ -73,14 +73,14 @@ public class FileDO extends BaseDO {
private FileTypeEnum type;
/**
* 存储 ID
* 存储 ID
*/
private Long storageId;
/**
* 转换为 X-File-Storage 文件信息对象
*
* @param storageCode 存储编码
* @param storageCode 存储编码
* @return X-File-Storage 文件信息对象
*/
public FileInfo toFileInfo(String storageCode) {

View File

@@ -16,18 +16,16 @@
package top.charles7c.continew.admin.system.model.entity;
import java.io.Serial;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum;
import top.charles7c.continew.admin.system.enums.StorageTypeEnum;
import top.charles7c.continew.starter.extension.crud.model.entity.BaseDO;
import java.io.Serial;
/**
* 存储实体
* 存储实体
*
* @author Charles7c
* @since 2023/12/26 22:09
@@ -55,17 +53,17 @@ public class StorageDO extends BaseDO {
private StorageTypeEnum type;
/**
* Access Key
* Access Key(访问密钥)
*/
private String accessKey;
/**
* Secret Key
* Secret Key(私有密钥)
*/
private String secretKey;
/**
* Endpoint
* Endpoint(终端节点)
*/
private String endpoint;
@@ -75,7 +73,7 @@ public class StorageDO extends BaseDO {
private String bucketName;
/**
* 自定义域名
* 域名
*/
private String domain;

View File

@@ -25,13 +25,13 @@ import java.io.Serial;
import java.io.Serializable;
/**
* 存储查询条件
* 存储查询条件
*
* @author Charles7c
* @since 2023/12/26 22:09
*/
@Data
@Schema(description = "存储查询条件")
@Schema(description = "存储查询条件")
public class StorageQuery implements Serializable {
@Serial
@@ -41,7 +41,7 @@ public class StorageQuery implements Serializable {
* 关键词
*/
@Schema(description = "关键词", example = "本地存储")
@Query(columns = {"name", "description"}, type = QueryType.LIKE)
@Query(columns = {"name", "code", "description"}, type = QueryType.LIKE)
private String description;
/**

View File

@@ -30,13 +30,13 @@ import top.charles7c.continew.starter.extension.crud.model.req.BaseReq;
import java.io.Serial;
/**
* 创建或修改存储库信息
* 存储请求信息
*
* @author Charles7c
* @since 2023/12/26 22:09
*/
@Data
@Schema(description = "创建或修改存储库信息")
@Schema(description = "存储请求信息")
public class StorageReq extends BaseReq {
@Serial
@@ -45,7 +45,7 @@ public class StorageReq extends BaseReq {
/**
* 名称
*/
@Schema(description = "名称", example = "存储1")
@Schema(description = "名称", example = "存储1")
@NotBlank(message = "名称不能为空")
@Length(max = 100, message = "名称长度不能超过 {max} 个字符")
private String name;
@@ -94,10 +94,10 @@ public class StorageReq extends BaseReq {
private String bucketName;
/**
* 自定义域名
* 域名
*/
@Schema(description = "自定义域名", example = "http://localhost:8000/file")
@Length(max = 255, message = "自定义域名长度不能超过 {max} 个字符")
@Schema(description = "域名", example = "http://localhost:8000/file")
@Length(max = 255, message = "域名长度不能超过 {max} 个字符")
private String domain;
/**
@@ -109,7 +109,7 @@ public class StorageReq extends BaseReq {
/**
* 描述
*/
@Schema(description = "描述", example = "存储描述")
@Schema(description = "描述", example = "存储描述")
@Length(max = 200, message = "描述长度不能超过 {max} 个字符")
private String description;

View File

@@ -73,8 +73,8 @@ public class FileResp extends BaseDetailResp {
private FileTypeEnum type;
/**
* 存储 ID
* 存储 ID
*/
@Schema(description = "存储ID", example = "1")
@Schema(description = "存储ID", example = "1")
private Long storageId;
}

View File

@@ -26,13 +26,13 @@ import top.charles7c.continew.starter.extension.crud.model.resp.BaseDetailResp;
import java.io.Serial;
/**
* 存储信息
* 存储响应信息
*
* @author Charles7c
* @since 2023/12/26 22:09
*/
@Data
@Schema(description = "存储信息")
@Schema(description = "存储响应信息")
public class StorageResp extends BaseDetailResp {
@Serial
@@ -41,7 +41,7 @@ public class StorageResp extends BaseDetailResp {
/**
* 名称
*/
@Schema(description = "名称", example = "存储1")
@Schema(description = "名称", example = "存储1")
private String name;
/**
@@ -88,15 +88,15 @@ public class StorageResp extends BaseDetailResp {
private String bucketName;
/**
* 自定义域名
* 域名
*/
@Schema(description = "自定义域名", example = "http://localhost:8000/file")
@Schema(description = "域名", example = "http://localhost:8000/file")
private String domain;
/**
* 描述
*/
@Schema(description = "描述", example = "存储描述")
@Schema(description = "描述", example = "存储描述")
private String description;
/**

View File

@@ -36,7 +36,7 @@ import java.util.List;
public interface FileService extends BaseService<FileResp, FileResp, FileQuery, FileReq>, IService<FileDO> {
/**
* 上传到默认存储
* 上传到默认存储
*
* @param file 文件信息
* @return 文件信息
@@ -46,18 +46,18 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
}
/**
* 上传到指定存储
* 上传到指定存储
*
* @param file 文件信息
* @param storageCode 存储编码
* @param storageCode 存储编码
* @return 文件信息
*/
FileInfo upload(MultipartFile file, String storageCode);
/**
* 根据存储 ID 列表查询
* 根据存储 ID 列表查询
*
* @param storageIds 存储 ID 列表
* @param storageIds 存储 ID 列表
* @return 文件数量
*/
Long countByStorageIds(List<Long> storageIds);

View File

@@ -24,7 +24,7 @@ import top.charles7c.continew.starter.data.mybatis.plus.service.IService;
import top.charles7c.continew.starter.extension.crud.service.BaseService;
/**
* 存储业务接口
* 存储业务接口
*
* @author Charles7c
* @since 2023/12/26 22:09
@@ -32,9 +32,9 @@ import top.charles7c.continew.starter.extension.crud.service.BaseService;
public interface StorageService extends BaseService<StorageResp, StorageResp, StorageQuery, StorageReq>, IService<StorageDO> {
/**
* 查询默认存储
* 查询默认存储
*
* @return 存储信息
* @return 存储信息
*/
StorageDO getDefaultStorage();
@@ -42,23 +42,21 @@ public interface StorageService extends BaseService<StorageResp, StorageResp, St
* 根据编码查询
*
* @param code 编码
* @return 存储信息
* @return 存储信息
*/
StorageDO getByCode(String code);
/**
* 加载存储
* 加载存储
*
* @param req
* 存储库信息
* @param req 存储信息
*/
void load(StorageReq req);
/**
* 卸载存储
* 卸载存储
*
* @param req
* 存储库信息
* @param req 存储信息
*/
void unload(StorageReq req);
}

View File

@@ -78,7 +78,7 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
StorageDO storage;
if (StrUtil.isBlank(storageCode)) {
storage = storageService.getDefaultStorage();
CheckUtils.throwIfNull(storage, "请先指定默认存储");
CheckUtils.throwIfNull(storage, "请先指定默认存储");
} else {
storage = storageService.getByCode(storageCode);
CheckUtils.throwIfNotExists(storage, "StorageDO", "Code", storageCode);

View File

@@ -48,7 +48,7 @@ import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 存储业务实现
* 存储业务实现
*
* @author Charles7c
* @since 2023/12/26 22:09
@@ -57,13 +57,13 @@ import java.util.concurrent.CopyOnWriteArrayList;
@RequiredArgsConstructor
public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO, StorageResp, StorageResp, StorageQuery, StorageReq> implements StorageService {
private final FileStorageService fileStorageService;
@Resource
private FileService fileService;
private final FileStorageService fileStorageService;
@Override
protected void beforeAdd(StorageReq req) {
CheckUtils.throwIf(Boolean.TRUE.equals(req.getIsDefault()) && this.isDefaultExists(null), "请先取消原有默认存储");
CheckUtils.throwIf(Boolean.TRUE.equals(req.getIsDefault()) && this.isDefaultExists(null), "请先取消原有默认存储");
String code = req.getCode();
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
this.load(req);
@@ -76,7 +76,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
DisEnableStatusEnum newStatus = req.getStatus();
StorageDO oldStorage = super.getById(id);
CheckUtils.throwIf(Boolean.TRUE.equals(oldStorage.getIsDefault()) && DisEnableStatusEnum.DISABLE
.equals(newStatus), "[{}] 是默认存储,不允许禁用", oldStorage.getName());
.equals(newStatus), "[{}] 是默认存储,不允许禁用", oldStorage.getName());
DisEnableStatusEnum oldStatus = oldStorage.getStatus();
if (DisEnableStatusEnum.ENABLE.equals(oldStatus) || DisEnableStatusEnum.DISABLE.equals(newStatus)) {
this.unload(BeanUtil.copyProperties(oldStorage, StorageReq.class));
@@ -85,17 +85,17 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
this.load(req);
}
if (Boolean.TRUE.equals(req.getIsDefault())) {
CheckUtils.throwIf(!DisEnableStatusEnum.ENABLE.equals(oldStatus), "请先启用该存储");
CheckUtils.throwIf(this.isDefaultExists(id), "请先取消原有默认存储");
CheckUtils.throwIf(!DisEnableStatusEnum.ENABLE.equals(oldStatus), "请先启用该存储");
CheckUtils.throwIf(this.isDefaultExists(id), "请先取消原有默认存储");
}
}
@Override
protected void beforeDelete(List<Long> ids) {
CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储存在文件关联,请删除文件后重试");
CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储存在文件关联,请删除文件后重试");
List<StorageDO> storageList = baseMapper.lambdaQuery().in(StorageDO::getId, ids).list();
storageList.forEach(s -> {
CheckUtils.throwIfEqual(Boolean.TRUE, s.getIsDefault(), "[{}] 是默认存储,不允许禁用", s.getName());
CheckUtils.throwIfEqual(Boolean.TRUE, s.getIsDefault(), "[{}] 是默认存储,不允许禁用", s.getName());
this.unload(BeanUtil.copyProperties(s, StorageReq.class));
});
}
@@ -118,8 +118,8 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
StorageTypeEnum type = req.getType();
if (StorageTypeEnum.LOCAL.equals(type)) {
ValidationUtils.throwIfBlank(bucketName, "存储路径不能为空");
ValidationUtils.throwIfBlank(domain, "自定义域名不能为空");
ValidationUtils.throwIf(!URLUtils.isHttpUrl(domain), "自定义域名格式错误");
ValidationUtils.throwIfBlank(domain, "域名不能为空");
ValidationUtils.throwIf(!URLUtils.isHttpUrl(domain), "域名格式错误");
req.setBucketName(StrUtil.appendIfMissing(bucketName
.replace(StringConstants.BACKSLASH, StringConstants.SLASH), StringConstants.SLASH));
FileStorageProperties.LocalPlusConfig config = new FileStorageProperties.LocalPlusConfig();
@@ -159,7 +159,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
}
/**
* 默认存储是否存在
* 默认存储是否存在
*
* @param id ID
* @return 是否存在