优化
This commit is contained in:
@@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.infra.api.file.FileApi;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
|
||||
import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClient;
|
||||
import cn.iocoder.yudao.module.infra.framework.file.core.client.s3.S3FileClientConfig;
|
||||
import cn.iocoder.yudao.module.infra.framework.file.core.utils.FileTypeUtils;
|
||||
import cn.iocoder.yudao.module.infra.service.file.FileConfigService;
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
@@ -618,7 +619,14 @@ public class TikUserFileServiceImpl implements TikUserFileService {
|
||||
String presignedUrl = client.presignPutUrl(filePath);
|
||||
String visitUrl = client.presignGetUrl(filePath, null);
|
||||
|
||||
// 5. 构建返回结果
|
||||
// 5. 替换为自定义域名
|
||||
String domain = getFileClientDomain(client.getId());
|
||||
if (StrUtil.isNotBlank(domain)) {
|
||||
presignedUrl = replaceUrlDomain(presignedUrl, domain);
|
||||
visitUrl = replaceUrlDomain(visitUrl, domain);
|
||||
}
|
||||
|
||||
// 6. 构建返回结果
|
||||
return Map.of(
|
||||
"presignedUrl", presignedUrl,
|
||||
"uploadUrl", HttpUtils.removeUrlQuery(visitUrl),
|
||||
@@ -631,6 +639,50 @@ public class TikUserFileServiceImpl implements TikUserFileService {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件客户端的自定义域名
|
||||
* @param configId 配置编号
|
||||
* @return 自定义域名,如果没有配置则返回 null
|
||||
*/
|
||||
private String getFileClientDomain(Long configId) {
|
||||
if (configId == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
var fileConfig = fileConfigService.getFileConfig(configId);
|
||||
if (fileConfig != null && fileConfig.getConfig() instanceof S3FileClientConfig s3Config) {
|
||||
return s3Config.getDomain();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[getFileClientDomain][获取文件配置失败,configId({})]", configId, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换URL的域名部分
|
||||
* @param url 原始URL
|
||||
* @param newDomain 新域名
|
||||
* @return 替换后的URL
|
||||
*/
|
||||
private String replaceUrlDomain(String url, String newDomain) {
|
||||
if (StrUtil.isBlank(url) || StrUtil.isBlank(newDomain)) {
|
||||
return url;
|
||||
}
|
||||
try {
|
||||
// 提取原始URL的协议和主机名
|
||||
int schemeEnd = url.indexOf("://");
|
||||
if (schemeEnd > 0) {
|
||||
int pathStart = url.indexOf("/", schemeEnd + 3);
|
||||
String pathAndQuery = pathStart > 0 ? url.substring(pathStart) : "";
|
||||
return newDomain + pathAndQuery;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[replaceUrlDomain][替换域名失败,url({})]", url, e);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
Reference in New Issue
Block a user