chore(storage): 优化部分代码格式和注释

This commit is contained in:
2025-02-14 21:18:23 +08:00
parent f386fd7d95
commit a040473746
22 changed files with 58 additions and 51 deletions

View File

@@ -59,7 +59,7 @@ public class QueryWrapperHelper {
* 设置排序 * 设置排序
* *
* @param queryWrapper 查询条件封装对象 * @param queryWrapper 查询条件封装对象
* @param sort 排序条件 * @param sort 排序条件
* @since 2.9.0 * @since 2.9.0
*/ */
public static <T> void sort(QueryWrapper<T> queryWrapper, Sort sort) { public static <T> void sort(QueryWrapper<T> queryWrapper, Sort sort) {

View File

@@ -17,10 +17,10 @@
package top.continew.starter.storage.constant; package top.continew.starter.storage.constant;
/** /**
* 存储 常量 * 存储常量
* *
* @author echo * @author echo
* @date 2024/12/16 19:09 * @since 2.9.0
*/ */
public class StorageConstant { public class StorageConstant {

View File

@@ -22,7 +22,7 @@ import top.continew.starter.storage.model.resp.UploadResp;
* 存储记录持久层接口 * 存储记录持久层接口
* *
* @author echo * @author echo
* @date 2024/12/17 16:49 * @since 2.9.0
*/ */
public interface StorageDao { public interface StorageDao {

View File

@@ -20,14 +20,15 @@ import top.continew.starter.storage.dao.StorageDao;
import top.continew.starter.storage.model.resp.UploadResp; import top.continew.starter.storage.model.resp.UploadResp;
/** /**
* 默认记录实现,此类并不能真正保存记录,只是用来脱离数据库运行,保证文件上传功能可以正常使用 * 存储记录持久层接口默认实现
* <p>此类并不能真正保存记录,只是用来脱离数据库运行,保证文件上传功能可以正常使用</p>
* *
* @author echo * @author echo
* @date 2024/12/18 08:48 * @since 2.9.0
**/ */
public class StorageDaoDefaultImpl implements StorageDao { public class StorageDaoDefaultImpl implements StorageDao {
@Override @Override
public void add(UploadResp uploadResp) { public void add(UploadResp uploadResp) {
} }
} }

View File

@@ -23,10 +23,10 @@ import top.continew.starter.storage.strategy.StorageStrategy;
import java.io.InputStream; import java.io.InputStream;
/** /**
* 装饰器基类 - 用于重写 * 装饰器基类
* *
* @author echo * @author echo
* @date 2024/12/30 19:33 * @since 2.9.0
*/ */
public abstract class AbstractStorageDecorator<C> implements StorageStrategy<C> { public abstract class AbstractStorageDecorator<C> implements StorageStrategy<C> {

View File

@@ -27,9 +27,9 @@ import java.util.List;
* 文件类型枚举 * 文件类型枚举
* *
* @author Charles7c * @author Charles7c
* @since 2023/12/23 13:38 * @since 2.9.0
*/ */
public enum FileTypeEnum implements BaseEnum<Integer> { public enum FileType implements BaseEnum<Integer> {
/** /**
* 其他 * 其他
@@ -67,14 +67,14 @@ public enum FileTypeEnum implements BaseEnum<Integer> {
* @param extension 扩展名 * @param extension 扩展名
* @return 文件类型 * @return 文件类型
*/ */
public static FileTypeEnum getByExtension(String extension) { public static FileType getByExtension(String extension) {
return Arrays.stream(FileTypeEnum.values()) return Arrays.stream(FileType.values())
.filter(t -> t.getExtensions().contains(StrUtil.emptyIfNull(extension).toLowerCase())) .filter(t -> t.getExtensions().contains(StrUtil.emptyIfNull(extension).toLowerCase()))
.findFirst() .findFirst()
.orElse(FileTypeEnum.UNKNOWN); .orElse(FileType.UNKNOWN);
} }
FileTypeEnum(Integer value, String description, List<String> extensions) { FileType(Integer value, String description, List<String> extensions) {
this.value = value; this.value = value;
this.description = description; this.description = description;
this.extensions = extensions; this.extensions = extensions;
@@ -98,5 +98,4 @@ public enum FileTypeEnum implements BaseEnum<Integer> {
public String getColor() { public String getColor() {
return BaseEnum.super.getColor(); return BaseEnum.super.getColor();
} }
} }

View File

@@ -28,7 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
* 存储策略管理器 * 存储策略管理器
* *
* @author echo * @author echo
* @date 2024/12/16 * @since 2.9.0
*/ */
public class StorageManager { public class StorageManager {
@@ -64,7 +64,7 @@ public class StorageManager {
*/ */
public static StorageStrategy<?> instance(String code) { public static StorageStrategy<?> instance(String code) {
StorageStrategy<?> strategy = STORAGE_STRATEGY.get(code); StorageStrategy<?> strategy = STORAGE_STRATEGY.get(code);
ValidationUtils.throwIfEmpty(strategy, "未找到存储配置:" + code); ValidationUtils.throwIfEmpty(strategy, "未找到存储配置: {}", code);
return strategy; return strategy;
} }

View File

@@ -20,7 +20,7 @@ package top.continew.starter.storage.model.resp;
* 缩略图 * 缩略图
* *
* @author echo * @author echo
* @date 2024/12/20 17:00 * @since 2.9.0
*/ */
public class ThumbnailResp { public class ThumbnailResp {

View File

@@ -22,7 +22,7 @@ import java.time.LocalDateTime;
* 上传结果 * 上传结果
* *
* @author echo * @author echo
* @date 2024/12/10 * @since 2.9.0
*/ */
public class UploadResp { public class UploadResp {

View File

@@ -25,7 +25,7 @@ import java.io.InputStream;
* 存储策略接口 * 存储策略接口
* *
* @author echo * @author echo
* @date 2024/12/16 11:19 * @since 2.9.0
*/ */
public interface StorageStrategy<C> { public interface StorageStrategy<C> {
@@ -49,7 +49,7 @@ public interface StorageStrategy<C> {
/** /**
* 创建桶 * 创建桶
* <p> S3: 创建桶 </p> * <p> S3: 创建桶 </p>
* <p> local: 创建 默认路径下 指定文件夹 </p> * <p> local: 默认路径下创建指定文件夹 </p>
* *
* @param bucketName 桶名称 * @param bucketName 桶名称
*/ */

View File

@@ -27,7 +27,7 @@ import java.io.OutputStream;
* 图像缩略图工具 * 图像缩略图工具
* *
* @author echo * @author echo
* @date 2024/12/20 16:49 * @since 2.9.0
*/ */
public class ImageThumbnailUtils { public class ImageThumbnailUtils {

View File

@@ -34,7 +34,7 @@ import java.time.format.DateTimeFormatter;
* 储存工具 * 储存工具
* *
* @author echo * @author echo
* @date 2024/12/16 19:55 * @since 2.9.0
*/ */
public class StorageUtils { public class StorageUtils {
public StorageUtils() { public StorageUtils() {

View File

@@ -23,18 +23,20 @@ import top.continew.starter.storage.dao.StorageDao;
import top.continew.starter.storage.dao.impl.StorageDaoDefaultImpl; import top.continew.starter.storage.dao.impl.StorageDaoDefaultImpl;
/** /**
* 对象存储 - 存储自动配置 * 本地存储自动配置
* *
* @author echo * @author echo
* @date 2024/12/17 20:23 * @since 2.9.0
*/ */
@AutoConfiguration @AutoConfiguration
public class OssStorageAutoconfigure { public class LocalStorageAutoConfiguration {
/**
* 存储记录持久层默认实现
*/
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public StorageDao storageDao() { public StorageDao storageDao() {
return new StorageDaoDefaultImpl(); return new StorageDaoDefaultImpl();
} }
} }

View File

@@ -28,9 +28,10 @@ import java.nio.file.Path;
* 本地客户端 * 本地客户端
* *
* @author echo * @author echo
* @date 2024/12/16 19:37 * @since 2.9.0
*/ */
public class LocalClient { public class LocalClient {
private static final Logger log = LoggerFactory.getLogger(LocalClient.class); private static final Logger log = LoggerFactory.getLogger(LocalClient.class);
/** /**

View File

@@ -28,7 +28,7 @@ import top.continew.starter.core.validation.ValidationUtils;
import top.continew.starter.storage.client.LocalClient; import top.continew.starter.storage.client.LocalClient;
import top.continew.starter.storage.constant.StorageConstant; import top.continew.starter.storage.constant.StorageConstant;
import top.continew.starter.storage.dao.StorageDao; import top.continew.starter.storage.dao.StorageDao;
import top.continew.starter.storage.enums.FileTypeEnum; import top.continew.starter.storage.enums.FileType;
import top.continew.starter.storage.model.req.StorageProperties; import top.continew.starter.storage.model.req.StorageProperties;
import top.continew.starter.storage.model.resp.ThumbnailResp; import top.continew.starter.storage.model.resp.ThumbnailResp;
import top.continew.starter.storage.model.resp.UploadResp; import top.continew.starter.storage.model.resp.UploadResp;
@@ -48,7 +48,7 @@ import java.util.Base64;
* 本地存储策略 * 本地存储策略
* *
* @author echo * @author echo
* @date 2024/12/16 19:48 * @since 2.9.0
*/ */
public class LocalStorageStrategy implements StorageStrategy<LocalClient> { public class LocalStorageStrategy implements StorageStrategy<LocalClient> {
@@ -134,7 +134,7 @@ public class LocalStorageStrategy implements StorageStrategy<LocalClient> {
} }
ThumbnailResp thumbnailResp = null; ThumbnailResp thumbnailResp = null;
//判断是否需要上传缩略图 前置条件 文件必须为图片 //判断是否需要上传缩略图 前置条件 文件必须为图片
boolean contains = FileTypeEnum.IMAGE.getExtensions().contains(fileExtension); boolean contains = FileType.IMAGE.getExtensions().contains(fileExtension);
if (contains && isThumbnail) { if (contains && isThumbnail) {
try (InputStream thumbnailStream = new ByteArrayInputStream(originalBytes)) { try (InputStream thumbnailStream = new ByteArrayInputStream(originalBytes)) {
thumbnailResp = this.uploadThumbnail(bucketName, formatFileName, path, thumbnailStream, fileType); thumbnailResp = this.uploadThumbnail(bucketName, formatFileName, path, thumbnailStream, fileType);
@@ -225,7 +225,7 @@ public class LocalStorageStrategy implements StorageStrategy<LocalClient> {
return null; return null;
} }
String extName = FileUtil.extName(fileName); String extName = FileUtil.extName(fileName);
CheckUtils.throwIf(!FileTypeEnum.IMAGE.getExtensions().contains(extName), "{} 不是图像格式", extName); CheckUtils.throwIf(!FileType.IMAGE.getExtensions().contains(extName), "{} 不是图像格式", extName);
return Base64.getEncoder().encodeToString(inputStream.readAllBytes()); return Base64.getEncoder().encodeToString(inputStream.readAllBytes());
} catch (Exception e) { } catch (Exception e) {
throw new BusinessException("无法查看图片", e); throw new BusinessException("无法查看图片", e);

View File

@@ -26,7 +26,7 @@ import java.security.NoSuchAlgorithmException;
* 本地存储工具 * 本地存储工具
* *
* @author echo * @author echo
* @date 2024/12/27 11:58 * @since 2.9.0
*/ */
public class LocalUtils { public class LocalUtils {
public LocalUtils() { public LocalUtils() {

View File

@@ -1 +1 @@
top.continew.starter.storage.autoconfigure.LocalStorageAutoconfigure top.continew.starter.storage.autoconfigure.LocalStorageAutoConfiguration

View File

@@ -23,14 +23,17 @@ import top.continew.starter.storage.dao.StorageDao;
import top.continew.starter.storage.dao.impl.StorageDaoDefaultImpl; import top.continew.starter.storage.dao.impl.StorageDaoDefaultImpl;
/** /**
* 本地存储 - 存储自动配置 * 对象存储自动配置
* *
* @author echo * @author echo
* @date 2024/12/17 20:23 * @since 2.9.0
*/ */
@AutoConfiguration @AutoConfiguration
public class LocalStorageAutoconfigure { public class OssStorageAutoConfiguration {
/**
* 存储记录持久层默认实现
*/
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public StorageDao storageDao() { public StorageDao storageDao() {

View File

@@ -39,7 +39,7 @@ import java.time.Duration;
* S3客户端 * S3客户端
* *
* @author echo * @author echo
* @date 2024/12/16 * @since 2.9.0
*/ */
public class OssClient { public class OssClient {

View File

@@ -41,7 +41,7 @@ import top.continew.starter.core.validation.ValidationUtils;
import top.continew.starter.storage.client.OssClient; import top.continew.starter.storage.client.OssClient;
import top.continew.starter.storage.constant.StorageConstant; import top.continew.starter.storage.constant.StorageConstant;
import top.continew.starter.storage.dao.StorageDao; import top.continew.starter.storage.dao.StorageDao;
import top.continew.starter.storage.enums.FileTypeEnum; import top.continew.starter.storage.enums.FileType;
import top.continew.starter.storage.model.req.StorageProperties; import top.continew.starter.storage.model.req.StorageProperties;
import top.continew.starter.storage.model.resp.ThumbnailResp; import top.continew.starter.storage.model.resp.ThumbnailResp;
import top.continew.starter.storage.model.resp.UploadResp; import top.continew.starter.storage.model.resp.UploadResp;
@@ -65,10 +65,11 @@ import java.util.concurrent.CompletionException;
* <p><a href="https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/home.html">...</a></p> * <p><a href="https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/home.html">...</a></p>
* *
* @author echo * @author echo
* @date 2024/12/16 20:29 * @since 2.9.0
*/ */
public class OssStorageStrategy implements StorageStrategy<OssClient> { public class OssStorageStrategy implements StorageStrategy<OssClient> {
private final static Logger log = LoggerFactory.getLogger(OssStorageStrategy.class);
private static final Logger log = LoggerFactory.getLogger(OssStorageStrategy.class);
private final OssClient client; private final OssClient client;
private final StorageDao storageDao; private final StorageDao storageDao;
@@ -93,7 +94,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
try { try {
// 调用 headBucket 请求,检查桶是否存在 // 调用 headBucket 请求,检查桶是否存在
client.getClient().headBucket(HeadBucketRequest.builder().bucket(bucketName).build()).join(); client.getClient().headBucket(HeadBucketRequest.builder().bucket(bucketName).build()).join();
return true; // 桶存在 return true;
} catch (Exception e) { } catch (Exception e) {
// 捕获异常,详细判断具体原因 // 捕获异常,详细判断具体原因
if (e.getCause() instanceof NoSuchBucketException) { if (e.getCause() instanceof NoSuchBucketException) {
@@ -160,7 +161,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
} }
ThumbnailResp thumbnailResp = null; ThumbnailResp thumbnailResp = null;
//判断是否需要上传缩略图 前置条件 文件必须为图片 //判断是否需要上传缩略图 前置条件 文件必须为图片
boolean contains = FileTypeEnum.IMAGE.getExtensions().contains(fileExtension); boolean contains = FileType.IMAGE.getExtensions().contains(fileExtension);
if (contains && isThumbnail) { if (contains && isThumbnail) {
try (InputStream thumbnailStream = new ByteArrayInputStream(fileBytes)) { try (InputStream thumbnailStream = new ByteArrayInputStream(fileBytes)) {
thumbnailResp = this.uploadThumbnail(bucketName, formatFileName, path, thumbnailStream, fileType); thumbnailResp = this.uploadThumbnail(bucketName, formatFileName, path, thumbnailStream, fileType);
@@ -236,9 +237,9 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
try { try {
// 构建下载请求 // 构建下载请求
DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder() DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder()
.getObjectRequest(req -> req.bucket(bucketName).key(fileName).build()) // 设置桶名和对象名 .getObjectRequest(req -> req.bucket(bucketName).key(fileName).build())
.addTransferListener(LoggingTransferListener.create()) // 添加传输监听器 .addTransferListener(LoggingTransferListener.create())
.responseTransformer(AsyncResponseTransformer.toBlockingInputStream()) // 转换为阻塞输入流 .responseTransformer(AsyncResponseTransformer.toBlockingInputStream())
.build(); .build();
// 执行下载操作 // 执行下载操作
Download<ResponseInputStream<GetObjectResponse>> download = client.getTransferManager() Download<ResponseInputStream<GetObjectResponse>> download = client.getTransferManager()
@@ -271,7 +272,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
return null; return null;
} }
String extName = FileUtil.extName(fileName); String extName = FileUtil.extName(fileName);
boolean contains = FileTypeEnum.IMAGE.getExtensions().contains(extName); boolean contains = FileType.IMAGE.getExtensions().contains(extName);
CheckUtils.throwIf(!contains, "{}非图片格式,无法获取", extName); CheckUtils.throwIf(!contains, "{}非图片格式,无法获取", extName);
return Base64.getEncoder().encodeToString(inputStream.readAllBytes()); return Base64.getEncoder().encodeToString(inputStream.readAllBytes());
} catch (Exception e) { } catch (Exception e) {

View File

@@ -25,7 +25,7 @@ import top.continew.starter.storage.constant.StorageConstant;
* OSS 工具 * OSS 工具
* *
* @author echo * @author echo
* @date 2024/12/17 13:48 * @since 2.9.0
*/ */
public class OssUtils { public class OssUtils {
public OssUtils() { public OssUtils() {

View File

@@ -1 +1 @@
top.continew.starter.storage.autoconfigure.OssStorageAutoconfigure top.continew.starter.storage.autoconfigure.OssStorageAutoConfiguration