feat: 优化
This commit is contained in:
@@ -116,8 +116,8 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
|
||||
|
||||
@Override
|
||||
public String presignGetUrl(String url, Integer expirationSeconds) {
|
||||
// 1. 将 url 转换为 path
|
||||
String path = StrUtil.removePrefix(url, config.getDomain() + "/");
|
||||
// 1. 将 url 转换为 path(支持多种URL格式)
|
||||
String path = extractPathFromUrl(url);
|
||||
path = HttpUtils.removeUrlQuery(path);
|
||||
String decodedPath = URLUtil.decode(path, StandardCharsets.UTF_8);
|
||||
|
||||
@@ -138,6 +138,37 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
|
||||
return signedUrl.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从URL中提取相对路径
|
||||
* 支持多种URL格式:
|
||||
* 1. CDN域名:https://oss.muyetools.cn/path/to/file
|
||||
* 2. OSS原始域名:https://bucket.oss-region.aliyuncs.com/path/to/file
|
||||
* 3. 相对路径:path/to/file
|
||||
*/
|
||||
private String extractPathFromUrl(String url) {
|
||||
if (StrUtil.isEmpty(url)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// 1. 尝试移除配置的域名前缀
|
||||
String path = StrUtil.removePrefix(url, config.getDomain() + "/");
|
||||
if (!StrUtil.equals(url, path)) {
|
||||
return path;
|
||||
}
|
||||
|
||||
// 2. 如果不是以配置域名开头,检查是否是完整URL
|
||||
if (url.contains("://")) {
|
||||
// 提取域名后的路径
|
||||
int pathStart = url.indexOf("/", url.indexOf("://") + 3);
|
||||
if (pathStart > 0) {
|
||||
return url.substring(pathStart + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 已经是相对路径,直接返回
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于 bucket + endpoint 构建访问的 Domain 地址
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user