mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +08:00
refactor(storage): 修复对象存储路径拼接问题
This commit is contained in:
@@ -71,16 +71,29 @@ public class StorageUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认路径地址 格式 2024/03/10/
|
* 本地存储默认路径地址 格式
|
||||||
|
* <p>mac/linux : 2024/03/10/</>
|
||||||
|
* <p>windows : 2024\03\10\</>
|
||||||
*
|
*
|
||||||
* @return {@link String }
|
* @return {@link String }
|
||||||
*/
|
*/
|
||||||
public static String defaultPath() {
|
public static String localDefaultPath() {
|
||||||
LocalDate today = LocalDate.now();
|
LocalDate today = LocalDate.now();
|
||||||
return Paths.get(String.valueOf(today.getYear()), String.valueOf(today.getMonthValue()), String.valueOf(today
|
return Paths.get(String.valueOf(today.getYear()), String.valueOf(today.getMonthValue()), String.valueOf(today
|
||||||
.getDayOfMonth())) + StringConstants.SLASH;
|
.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 前缀。
|
* 根据 endpoint 判断是否带有 http 或 https,如果没有则加上 http 前缀。
|
||||||
*
|
*
|
||||||
|
@@ -125,7 +125,7 @@ public class LocalStorageStrategy implements StorageStrategy<LocalClient> {
|
|||||||
String formatFileName = StorageUtils.formatFileName(fileName);
|
String formatFileName = StorageUtils.formatFileName(fileName);
|
||||||
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
|
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
|
||||||
if (StrUtil.isEmpty(path)) {
|
if (StrUtil.isEmpty(path)) {
|
||||||
path = StorageUtils.defaultPath();
|
path = StorageUtils.localDefaultPath();
|
||||||
}
|
}
|
||||||
// 判断文件夹是否存在 不存在则创建
|
// 判断文件夹是否存在 不存在则创建
|
||||||
Path folderPath = Paths.get(bucketName, path);
|
Path folderPath = Paths.get(bucketName, path);
|
||||||
|
@@ -54,7 +54,6 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
@@ -157,7 +156,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
|
|||||||
String formatFileName = StorageUtils.formatFileName(fileName);
|
String formatFileName = StorageUtils.formatFileName(fileName);
|
||||||
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
|
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
|
||||||
if (StrUtil.isEmpty(path)) {
|
if (StrUtil.isEmpty(path)) {
|
||||||
path = StorageUtils.defaultPath();
|
path = StorageUtils.ossDefaultPath();
|
||||||
}
|
}
|
||||||
ThumbnailResp thumbnailResp = null;
|
ThumbnailResp thumbnailResp = null;
|
||||||
//判断是否需要上传缩略图 前置条件 文件必须为图片
|
//判断是否需要上传缩略图 前置条件 文件必须为图片
|
||||||
@@ -174,7 +173,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
|
|||||||
}
|
}
|
||||||
String eTag = etag;
|
String eTag = etag;
|
||||||
// 构建 上传后的文件路径地址 格式 xxx/xxx/xxx.jpg
|
// 构建 上传后的文件路径地址 格式 xxx/xxx/xxx.jpg
|
||||||
String filePath = Paths.get(path, formatFileName).toString();
|
String filePath = path + formatFileName;
|
||||||
// 构建 文件上传记录 并返回
|
// 构建 文件上传记录 并返回
|
||||||
return buildStorageRecord(bucketName, fileName, filePath, eTag, fileBytes.length, thumbnailResp);
|
return buildStorageRecord(bucketName, fileName, filePath, eTag, fileBytes.length, thumbnailResp);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -185,7 +184,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
|
|||||||
@Override
|
@Override
|
||||||
public void upload(String bucketName, String fileName, String path, InputStream inputStream, String fileType) {
|
public void upload(String bucketName, String fileName, String path, InputStream inputStream, String fileType) {
|
||||||
// 构建 S3 存储 文件路径
|
// 构建 S3 存储 文件路径
|
||||||
String filePath = Paths.get(path, fileName).toString();
|
String filePath = path + fileName;
|
||||||
try {
|
try {
|
||||||
long available = inputStream.available();
|
long available = inputStream.available();
|
||||||
// 构建异步请求体,指定内容长度
|
// 构建异步请求体,指定内容长度
|
||||||
@@ -203,7 +202,8 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
|
|||||||
// 写入输入流内容到请求体
|
// 写入输入流内容到请求体
|
||||||
requestBody.writeInputStream(inputStream);
|
requestBody.writeInputStream(inputStream);
|
||||||
CompletedUpload uploadResult = upload.completionFuture().join();
|
CompletedUpload uploadResult = upload.completionFuture().join();
|
||||||
etag = uploadResult.response().eTag();
|
|
||||||
|
etag = uploadResult.response().eTag().replace("\"", "");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new BusinessException("文件上传异常", e);
|
throw new BusinessException("文件上传异常", e);
|
||||||
}
|
}
|
||||||
@@ -225,7 +225,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
|
|||||||
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||||
// 上传文件
|
// 上传文件
|
||||||
this.upload(bucketName, thumbnailFileName, path, inputStream, fileType);
|
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) {
|
} catch (IOException e) {
|
||||||
throw new BusinessException("缩略图处理异常", e);
|
throw new BusinessException("缩略图处理异常", e);
|
||||||
}
|
}
|
||||||
@@ -326,7 +326,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
|
|||||||
uploadResp.setThumbnailUrl(thumbnailUrl);
|
uploadResp.setThumbnailUrl(thumbnailUrl);
|
||||||
uploadResp.setThumbnailSize(thumbnailSize);
|
uploadResp.setThumbnailSize(thumbnailSize);
|
||||||
uploadResp.seteTag(eTag);
|
uploadResp.seteTag(eTag);
|
||||||
uploadResp.setPath(Paths.get(bucketName, filePath).toString());
|
uploadResp.setPath(bucketName + filePath);
|
||||||
uploadResp.setBucketName(bucketName);
|
uploadResp.setBucketName(bucketName);
|
||||||
uploadResp.setCreateTime(LocalDateTime.now());
|
uploadResp.setCreateTime(LocalDateTime.now());
|
||||||
storageDao.add(uploadResp);
|
storageDao.add(uploadResp);
|
||||||
|
Reference in New Issue
Block a user