mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-15 02:57:10 +08:00
refactor: 优化存储管理
This commit is contained in:
@@ -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> {
|
||||
}
|
@@ -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) {
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
/**
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
}
|
@@ -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;
|
||||
|
||||
/**
|
||||
|
@@ -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);
|
||||
|
@@ -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);
|
||||
}
|
@@ -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);
|
||||
|
@@ -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 是否存在
|
||||
|
Reference in New Issue
Block a user