From c3a71851007111d11fe37c5b82e4d2da951a3102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=B3=BD=E5=A8=81?= <958142070@qq.com> Date: Sun, 19 Jan 2025 16:59:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(storage):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8=E8=B7=AF=E5=BE=84=E6=8B=BC?= =?UTF-8?q?=E6=8E=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/storage/util/StorageUtils.java | 17 +++++++++++++++-- .../storage/strategy/LocalStorageStrategy.java | 2 +- .../storage/strategy/OssStorageStrategy.java | 14 +++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/continew-starter-storage/continew-starter-storage-core/src/main/java/top/continew/starter/storage/util/StorageUtils.java b/continew-starter-storage/continew-starter-storage-core/src/main/java/top/continew/starter/storage/util/StorageUtils.java index 4cfca27a..1c1c1a15 100644 --- a/continew-starter-storage/continew-starter-storage-core/src/main/java/top/continew/starter/storage/util/StorageUtils.java +++ b/continew-starter-storage/continew-starter-storage-core/src/main/java/top/continew/starter/storage/util/StorageUtils.java @@ -71,16 +71,29 @@ public class StorageUtils { } /** - * 默认路径地址 格式 2024/03/10/ + * 本地存储默认路径地址 格式 + *

mac/linux : 2024/03/10/ + *

windows : 2024\03\10\ * * @return {@link String } */ - public static String defaultPath() { + public static String localDefaultPath() { LocalDate today = LocalDate.now(); return Paths.get(String.valueOf(today.getYear()), String.valueOf(today.getMonthValue()), String.valueOf(today .getDayOfMonth())) + StringConstants.SLASH; } + /** + * 对象存储默认路径 格式 2024/03/10/ + * + * @return {@link String } + */ + public static String ossDefaultPath() { + LocalDate today = LocalDate.now(); + return today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today + .getDayOfMonth() + StringConstants.SLASH; + } + /** * 根据 endpoint 判断是否带有 http 或 https,如果没有则加上 http 前缀。 * diff --git a/continew-starter-storage/continew-starter-storage-local/src/main/java/top/continew/starter/storage/strategy/LocalStorageStrategy.java b/continew-starter-storage/continew-starter-storage-local/src/main/java/top/continew/starter/storage/strategy/LocalStorageStrategy.java index 39bc36d5..f022d43c 100644 --- a/continew-starter-storage/continew-starter-storage-local/src/main/java/top/continew/starter/storage/strategy/LocalStorageStrategy.java +++ b/continew-starter-storage/continew-starter-storage-local/src/main/java/top/continew/starter/storage/strategy/LocalStorageStrategy.java @@ -125,7 +125,7 @@ public class LocalStorageStrategy implements StorageStrategy { String formatFileName = StorageUtils.formatFileName(fileName); // 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/ if (StrUtil.isEmpty(path)) { - path = StorageUtils.defaultPath(); + path = StorageUtils.localDefaultPath(); } // 判断文件夹是否存在 不存在则创建 Path folderPath = Paths.get(bucketName, path); diff --git a/continew-starter-storage/continew-starter-storage-oss/src/main/java/top/continew/starter/storage/strategy/OssStorageStrategy.java b/continew-starter-storage/continew-starter-storage-oss/src/main/java/top/continew/starter/storage/strategy/OssStorageStrategy.java index f6eb6f40..7db388d1 100644 --- a/continew-starter-storage/continew-starter-storage-oss/src/main/java/top/continew/starter/storage/strategy/OssStorageStrategy.java +++ b/continew-starter-storage/continew-starter-storage-oss/src/main/java/top/continew/starter/storage/strategy/OssStorageStrategy.java @@ -54,7 +54,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; -import java.nio.file.Paths; import java.time.Duration; import java.time.LocalDateTime; import java.util.Base64; @@ -157,7 +156,7 @@ public class OssStorageStrategy implements StorageStrategy { String formatFileName = StorageUtils.formatFileName(fileName); // 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/ if (StrUtil.isEmpty(path)) { - path = StorageUtils.defaultPath(); + path = StorageUtils.ossDefaultPath(); } ThumbnailResp thumbnailResp = null; //判断是否需要上传缩略图 前置条件 文件必须为图片 @@ -174,7 +173,7 @@ public class OssStorageStrategy implements StorageStrategy { } String eTag = etag; // 构建 上传后的文件路径地址 格式 xxx/xxx/xxx.jpg - String filePath = Paths.get(path, formatFileName).toString(); + String filePath = path + formatFileName; // 构建 文件上传记录 并返回 return buildStorageRecord(bucketName, fileName, filePath, eTag, fileBytes.length, thumbnailResp); } catch (IOException e) { @@ -185,7 +184,7 @@ public class OssStorageStrategy implements StorageStrategy { @Override public void upload(String bucketName, String fileName, String path, InputStream inputStream, String fileType) { // 构建 S3 存储 文件路径 - String filePath = Paths.get(path, fileName).toString(); + String filePath = path + fileName; try { long available = inputStream.available(); // 构建异步请求体,指定内容长度 @@ -203,7 +202,8 @@ public class OssStorageStrategy implements StorageStrategy { // 写入输入流内容到请求体 requestBody.writeInputStream(inputStream); CompletedUpload uploadResult = upload.completionFuture().join(); - etag = uploadResult.response().eTag(); + + etag = uploadResult.response().eTag().replace("\"", ""); } catch (IOException e) { throw new BusinessException("文件上传异常", e); } @@ -225,7 +225,7 @@ public class OssStorageStrategy implements StorageStrategy { inputStream = new ByteArrayInputStream(outputStream.toByteArray()); // 上传文件 this.upload(bucketName, thumbnailFileName, path, inputStream, fileType); - return new ThumbnailResp((long)outputStream.size(), Paths.get(path, thumbnailFileName).toString()); + return new ThumbnailResp((long)outputStream.size(), path + thumbnailFileName); } catch (IOException e) { throw new BusinessException("缩略图处理异常", e); } @@ -326,7 +326,7 @@ public class OssStorageStrategy implements StorageStrategy { uploadResp.setThumbnailUrl(thumbnailUrl); uploadResp.setThumbnailSize(thumbnailSize); uploadResp.seteTag(eTag); - uploadResp.setPath(Paths.get(bucketName, filePath).toString()); + uploadResp.setPath(bucketName + filePath); uploadResp.setBucketName(bucketName); uploadResp.setCreateTime(LocalDateTime.now()); storageDao.add(uploadResp);