优化
This commit is contained in:
@@ -7,7 +7,7 @@ import { ref, reactive } from 'vue'
|
|||||||
import { message } from 'ant-design-vue'
|
import { message } from 'ant-design-vue'
|
||||||
import { MaterialService } from '@/api/material'
|
import { MaterialService } from '@/api/material'
|
||||||
import { useUserStore } from '@/stores/user'
|
import { useUserStore } from '@/stores/user'
|
||||||
import { OSS_ORIGINAL, isDev } from '@gold/config/api'
|
import { OSS_ORIGINAL, OSS_DOMAIN, isDev } from '@gold/config/api'
|
||||||
|
|
||||||
// GB转字节常量
|
// GB转字节常量
|
||||||
const GB_TO_BYTES = 1073741824
|
const GB_TO_BYTES = 1073741824
|
||||||
@@ -109,10 +109,10 @@ export function useUpload() {
|
|||||||
|
|
||||||
// 发起PUT请求
|
// 发起PUT请求
|
||||||
// 开发环境:使用 /oss 代理避免CORS问题
|
// 开发环境:使用 /oss 代理避免CORS问题
|
||||||
// 生产环境:直接使用OSS原始域名(需要OSS配置CORS)
|
// 生产环境:使用自定义域名(签名基于路径,域名可替换)
|
||||||
const uploadUrl = isDev()
|
const uploadUrl = isDev()
|
||||||
? presignedData.presignedUrl.replace(OSS_ORIGINAL, '/oss')
|
? presignedData.presignedUrl.replace(OSS_ORIGINAL, '/oss')
|
||||||
: presignedData.presignedUrl
|
: presignedData.presignedUrl.replace(OSS_ORIGINAL, OSS_DOMAIN)
|
||||||
xhr.open('PUT', uploadUrl)
|
xhr.open('PUT', uploadUrl)
|
||||||
if (presignedData.headers && presignedData.headers['Content-Type']) {
|
if (presignedData.headers && presignedData.headers['Content-Type']) {
|
||||||
xhr.setRequestHeader('Content-Type', presignedData.headers['Content-Type'])
|
xhr.setRequestHeader('Content-Type', presignedData.headers['Content-Type'])
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ export const API_BASE = {
|
|||||||
*/
|
*/
|
||||||
export const OSS_ORIGINAL = 'https://muye-ai-chat.oss-cn-hangzhou.aliyuncs.com'
|
export const OSS_ORIGINAL = 'https://muye-ai-chat.oss-cn-hangzhou.aliyuncs.com'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OSS 自定义域名(生产环境使用)
|
||||||
|
*/
|
||||||
|
export const OSS_DOMAIN = 'https://oss.muyetools.cn'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断是否为开发环境
|
* 判断是否为开发环境
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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.dataobject.file.FileDO;
|
||||||
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
|
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.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.framework.file.core.utils.FileTypeUtils;
|
||||||
import cn.iocoder.yudao.module.infra.service.file.FileConfigService;
|
import cn.iocoder.yudao.module.infra.service.file.FileConfigService;
|
||||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||||
@@ -618,7 +619,14 @@ public class TikUserFileServiceImpl implements TikUserFileService {
|
|||||||
String presignedUrl = client.presignPutUrl(filePath);
|
String presignedUrl = client.presignPutUrl(filePath);
|
||||||
String visitUrl = client.presignGetUrl(filePath, null);
|
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(
|
return Map.of(
|
||||||
"presignedUrl", presignedUrl,
|
"presignedUrl", presignedUrl,
|
||||||
"uploadUrl", HttpUtils.removeUrlQuery(visitUrl),
|
"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
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|||||||
Reference in New Issue
Block a user