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

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

View File

@@ -39,7 +39,7 @@ import java.time.Duration;
* S3客户端
*
* @author echo
* @date 2024/12/16
* @since 2.9.0
*/
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.constant.StorageConstant;
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.resp.ThumbnailResp;
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>
*
* @author echo
* @date 2024/12/16 20:29
* @since 2.9.0
*/
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 StorageDao storageDao;
@@ -93,7 +94,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
try {
// 调用 headBucket 请求,检查桶是否存在
client.getClient().headBucket(HeadBucketRequest.builder().bucket(bucketName).build()).join();
return true; // 桶存在
return true;
} catch (Exception e) {
// 捕获异常,详细判断具体原因
if (e.getCause() instanceof NoSuchBucketException) {
@@ -160,7 +161,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
}
ThumbnailResp thumbnailResp = null;
//判断是否需要上传缩略图 前置条件 文件必须为图片
boolean contains = FileTypeEnum.IMAGE.getExtensions().contains(fileExtension);
boolean contains = FileType.IMAGE.getExtensions().contains(fileExtension);
if (contains && isThumbnail) {
try (InputStream thumbnailStream = new ByteArrayInputStream(fileBytes)) {
thumbnailResp = this.uploadThumbnail(bucketName, formatFileName, path, thumbnailStream, fileType);
@@ -236,9 +237,9 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
try {
// 构建下载请求
DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder()
.getObjectRequest(req -> req.bucket(bucketName).key(fileName).build()) // 设置桶名和对象名
.addTransferListener(LoggingTransferListener.create()) // 添加传输监听器
.responseTransformer(AsyncResponseTransformer.toBlockingInputStream()) // 转换为阻塞输入流
.getObjectRequest(req -> req.bucket(bucketName).key(fileName).build())
.addTransferListener(LoggingTransferListener.create())
.responseTransformer(AsyncResponseTransformer.toBlockingInputStream())
.build();
// 执行下载操作
Download<ResponseInputStream<GetObjectResponse>> download = client.getTransferManager()
@@ -271,7 +272,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
return null;
}
String extName = FileUtil.extName(fileName);
boolean contains = FileTypeEnum.IMAGE.getExtensions().contains(extName);
boolean contains = FileType.IMAGE.getExtensions().contains(extName);
CheckUtils.throwIf(!contains, "{}非图片格式,无法获取", extName);
return Base64.getEncoder().encodeToString(inputStream.readAllBytes());
} catch (Exception e) {

View File

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

View File

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