同步平台业务模块能力
This commit is contained in:
@@ -30,16 +30,26 @@ public class FileKeyGenerator {
|
||||
return String.format("%s/%s/%s/%s%s", storageType, businessType, date, uuid, extension);
|
||||
}
|
||||
|
||||
private static final String[] COMPOUND_EXTENSIONS = {
|
||||
".tar.gz", ".tar.bz2", ".tar.xz", ".tar.7z", ".tar.zst", ".tar.lz", ".tar.lzma", ".tar.sz", ".tar.lzo"
|
||||
};
|
||||
|
||||
/**
|
||||
* Extract file extension
|
||||
* Extract file extension (including dot), e.g. ".tar.gz" or ".jpg"
|
||||
*
|
||||
* @param fileName File name
|
||||
* @return Extension (including dot), or empty string if no extension
|
||||
*/
|
||||
private static String extractExtension(String fileName) {
|
||||
public static String extractExtension(String fileName) {
|
||||
if (fileName == null || fileName.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
String lowerName = fileName.toLowerCase();
|
||||
for (String ext : COMPOUND_EXTENSIONS) {
|
||||
if (lowerName.endsWith(ext)) {
|
||||
return fileName.substring(fileName.length() - ext.length());
|
||||
}
|
||||
}
|
||||
int dotIndex = fileName.lastIndexOf('.');
|
||||
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
|
||||
return fileName.substring(dotIndex);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.xspaceagi.file.infra.storage;
|
||||
|
||||
import com.xspaceagi.file.domain.storage.FileStorageStrategy;
|
||||
import com.xspaceagi.system.spec.utils.RedisUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
@@ -22,6 +24,7 @@ import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* S3 Protocol Storage Implementation
|
||||
@@ -46,6 +49,12 @@ public class S3FileStorageStrategy implements FileStorageStrategy {
|
||||
@Value("${s3.region:us-east-1}")
|
||||
private String region;
|
||||
|
||||
@Value("${s3.proxy:}")
|
||||
private String proxyBaseUrl;
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
private volatile S3Client s3Client;
|
||||
private volatile S3Presigner s3Presigner;
|
||||
|
||||
@@ -181,7 +190,11 @@ public class S3FileStorageStrategy implements FileStorageStrategy {
|
||||
|
||||
PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(presignRequest);
|
||||
String url = presignedRequest.url().toString();
|
||||
|
||||
if (StringUtils.isNotBlank(proxyBaseUrl) && expireSeconds < 86400 * 30) {
|
||||
String key = UUID.randomUUID().toString().replace("-", "") + FileKeyGenerator.extractExtension(fileKey);
|
||||
redisUtil.set("s3p:" + key, url, expireSeconds);
|
||||
url = proxyBaseUrl + "/" + key;
|
||||
}
|
||||
log.info("S3 presigned URL generated successfully: {}", fileKey);
|
||||
return url;
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user