Skip to content

Commit efb0eea

Browse files
Charles7cgitee-org
authored andcommitted
fix(storage): 修复对象存储路径拼接问题
2 parents e11c7a0 + c3a7185 commit efb0eea

File tree

3 files changed

+23
-10
lines changed
  • continew-starter-storage

3 files changed

+23
-10
lines changed

continew-starter-storage/continew-starter-storage-core/src/main/java/top/continew/starter/storage/util/StorageUtils.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,29 @@ public static String defaultFileDir(String fileName) {
7171
}
7272

7373
/**
74-
* 默认路径地址 格式 2024/03/10/
74+
* 本地存储默认路径地址 格式
75+
* <p>mac/linux : 2024/03/10/</>
76+
* <p>windows : 2024\03\10\</>
7577
*
7678
* @return {@link String }
7779
*/
78-
public static String defaultPath() {
80+
public static String localDefaultPath() {
7981
LocalDate today = LocalDate.now();
8082
return Paths.get(String.valueOf(today.getYear()), String.valueOf(today.getMonthValue()), String.valueOf(today
8183
.getDayOfMonth())) + StringConstants.SLASH;
8284
}
8385

86+
/**
87+
* 对象存储默认路径 格式 2024/03/10/
88+
*
89+
* @return {@link String }
90+
*/
91+
public static String ossDefaultPath() {
92+
LocalDate today = LocalDate.now();
93+
return today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
94+
.getDayOfMonth() + StringConstants.SLASH;
95+
}
96+
8497
/**
8598
* 根据 endpoint 判断是否带有 http 或 https,如果没有则加上 http 前缀。
8699
*

continew-starter-storage/continew-starter-storage-local/src/main/java/top/continew/starter/storage/strategy/LocalStorageStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public UploadResp upload(String bucketName,
125125
String formatFileName = StorageUtils.formatFileName(fileName);
126126
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
127127
if (StrUtil.isEmpty(path)) {
128-
path = StorageUtils.defaultPath();
128+
path = StorageUtils.localDefaultPath();
129129
}
130130
// 判断文件夹是否存在 不存在则创建
131131
Path folderPath = Paths.get(bucketName, path);

continew-starter-storage/continew-starter-storage-oss/src/main/java/top/continew/starter/storage/strategy/OssStorageStrategy.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import java.io.IOException;
5555
import java.io.InputStream;
5656
import java.net.HttpURLConnection;
57-
import java.nio.file.Paths;
5857
import java.time.Duration;
5958
import java.time.LocalDateTime;
6059
import java.util.Base64;
@@ -157,7 +156,7 @@ public UploadResp upload(String bucketName,
157156
String formatFileName = StorageUtils.formatFileName(fileName);
158157
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
159158
if (StrUtil.isEmpty(path)) {
160-
path = StorageUtils.defaultPath();
159+
path = StorageUtils.ossDefaultPath();
161160
}
162161
ThumbnailResp thumbnailResp = null;
163162
//判断是否需要上传缩略图 前置条件 文件必须为图片
@@ -174,7 +173,7 @@ public UploadResp upload(String bucketName,
174173
}
175174
String eTag = etag;
176175
// 构建 上传后的文件路径地址 格式 xxx/xxx/xxx.jpg
177-
String filePath = Paths.get(path, formatFileName).toString();
176+
String filePath = path + formatFileName;
178177
// 构建 文件上传记录 并返回
179178
return buildStorageRecord(bucketName, fileName, filePath, eTag, fileBytes.length, thumbnailResp);
180179
} catch (IOException e) {
@@ -185,7 +184,7 @@ public UploadResp upload(String bucketName,
185184
@Override
186185
public void upload(String bucketName, String fileName, String path, InputStream inputStream, String fileType) {
187186
// 构建 S3 存储 文件路径
188-
String filePath = Paths.get(path, fileName).toString();
187+
String filePath = path + fileName;
189188
try {
190189
long available = inputStream.available();
191190
// 构建异步请求体,指定内容长度
@@ -203,7 +202,8 @@ public void upload(String bucketName, String fileName, String path, InputStream
203202
// 写入输入流内容到请求体
204203
requestBody.writeInputStream(inputStream);
205204
CompletedUpload uploadResult = upload.completionFuture().join();
206-
etag = uploadResult.response().eTag();
205+
206+
etag = uploadResult.response().eTag().replace("\"", "");
207207
} catch (IOException e) {
208208
throw new BusinessException("文件上传异常", e);
209209
}
@@ -225,7 +225,7 @@ public ThumbnailResp uploadThumbnail(String bucketName,
225225
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
226226
// 上传文件
227227
this.upload(bucketName, thumbnailFileName, path, inputStream, fileType);
228-
return new ThumbnailResp((long)outputStream.size(), Paths.get(path, thumbnailFileName).toString());
228+
return new ThumbnailResp((long)outputStream.size(), path + thumbnailFileName);
229229
} catch (IOException e) {
230230
throw new BusinessException("缩略图处理异常", e);
231231
}
@@ -326,7 +326,7 @@ private UploadResp buildStorageRecord(String bucketName,
326326
uploadResp.setThumbnailUrl(thumbnailUrl);
327327
uploadResp.setThumbnailSize(thumbnailSize);
328328
uploadResp.seteTag(eTag);
329-
uploadResp.setPath(Paths.get(bucketName, filePath).toString());
329+
uploadResp.setPath(bucketName + filePath);
330330
uploadResp.setBucketName(bucketName);
331331
uploadResp.setCreateTime(LocalDateTime.now());
332332
storageDao.add(uploadResp);

0 commit comments

Comments
 (0)