feat: 优化

This commit is contained in:
2026-03-02 01:28:46 +08:00
parent b2e5bb85f4
commit ce3d529f80
11 changed files with 88 additions and 33 deletions

View File

@@ -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 地址
*