131 lines
3.9 KiB
JavaScript
131 lines
3.9 KiB
JavaScript
import { getOssPresignedUrlApi } from './api.js';
|
||
|
||
/**
|
||
* 从URL提取文件名
|
||
* @param {String} url - URL字符串
|
||
* @returns {String|null} 提取的文件名,失败返回null
|
||
*/
|
||
export function extractFileNameFromUrl(url) {
|
||
if (!url || url.startsWith('/static/')) {
|
||
return null;
|
||
}
|
||
|
||
try {
|
||
let urlPath = url;
|
||
|
||
// 如果是完整URL,提取路径部分
|
||
if (url.includes('://')) {
|
||
// 找到协议后的第一个 / 开始的路径
|
||
const protocolEndIndex = url.indexOf('://');
|
||
const pathStartIndex = url.indexOf('/', protocolEndIndex + 3);
|
||
if (pathStartIndex !== -1) {
|
||
urlPath = url.substring(pathStartIndex);
|
||
}
|
||
}
|
||
|
||
// 提取最后一个 / 之后的内容作为文件名
|
||
const fileName = urlPath.substring(urlPath.lastIndexOf('/') + 1);
|
||
return fileName || null;
|
||
} catch (error) {
|
||
console.error('提取文件名失败:', error);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从cover_url提取文件名(兼容旧函数名)
|
||
* @param {String} coverUrl - 后端返回的cover_url
|
||
* @returns {String|null} 提取的文件名,失败返回null
|
||
*/
|
||
export function extractFileNameFromCoverUrl(coverUrl) {
|
||
return extractFileNameFromUrl(coverUrl);
|
||
}
|
||
|
||
/**
|
||
* 从OSS完整URL中提取对象路径(保留bucket之后的完整路径)
|
||
* 例如:https://bucket.oss-cn-shanghai.aliyuncs.com/asset/13/87/file.jpg -> asset/13/87/file.jpg
|
||
* @param {String} url - OSS完整URL
|
||
* @returns {String|null} 对象路径,失败返回null
|
||
*/
|
||
export function extractOssObjectPath(url) {
|
||
if (!url || url.startsWith('/static/')) return null;
|
||
try {
|
||
if (url.includes('aliyuncs.com/')) {
|
||
return url.substring(url.indexOf('aliyuncs.com/') + 'aliyuncs.com/'.length);
|
||
}
|
||
// 兜底:返回域名后的路径
|
||
const protocolEnd = url.indexOf('://');
|
||
const pathStart = url.indexOf('/', protocolEnd + 3);
|
||
if (pathStart !== -1) {
|
||
return url.substring(pathStart + 1);
|
||
}
|
||
return null;
|
||
} catch (e) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取藏品封面的真实URL
|
||
* @param {String} coverUrl - 后端返回的cover_url
|
||
* @returns {Promise<String>} 真实可访问的URL
|
||
*/
|
||
export async function getAssetCoverRealUrl(coverUrl) {
|
||
// 默认图片路径
|
||
const DEFAULT_IMAGE = '';
|
||
|
||
// 如果是本地静态资源,直接返回
|
||
if (!coverUrl || coverUrl.startsWith('/static/')) {
|
||
return coverUrl || DEFAULT_IMAGE;
|
||
}
|
||
try {
|
||
// 提取完整OSS对象路径(保留 asset/13/87/filename.jpg 这样的前缀)
|
||
const objectPath = extractOssObjectPath(coverUrl);
|
||
if (!objectPath) {
|
||
console.warn('无法提取OSS对象路径,使用默认图片');
|
||
return DEFAULT_IMAGE;
|
||
}
|
||
|
||
// 调用API获取预签名URL(type='asset')
|
||
const res = await getOssPresignedUrlApi(objectPath, 3600, 'asset');
|
||
|
||
if (res.code === 200 && res.data && res.data.url) {
|
||
return res.data.url;
|
||
} else {
|
||
console.error('获取OSS预签名URL失败:', res.message);
|
||
return DEFAULT_IMAGE;
|
||
}
|
||
} catch (error) {
|
||
console.error('获取藏品封面URL失败:', error);
|
||
return DEFAULT_IMAGE;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取好友头像的真实URL
|
||
* @param {String} avatarUrl - 后端返回的avatar_url(完整URL)
|
||
* @returns {Promise<String>} 真实可访问的URL,失败返回空字符串(让Avatar组件使用默认头像)
|
||
*/
|
||
export async function getFriendAvatarRealUrl(avatarUrl) {
|
||
|
||
// 如果是本地静态资源或为空,直接返回
|
||
if (!avatarUrl || avatarUrl.startsWith('/static/')) {
|
||
return avatarUrl || '';
|
||
}
|
||
|
||
try {
|
||
// 直接将完整URL作为filename传递给预签名API
|
||
const res = await getOssPresignedUrlApi(avatarUrl, 3600, 'avatar');
|
||
|
||
if (res.code === 200 && res.data && res.data.url) {
|
||
return res.data.url;
|
||
} else {
|
||
console.error('获取头像预签名URL失败:', res.message);
|
||
return '';
|
||
}
|
||
} catch (error) {
|
||
console.error('获取好友头像URL失败:', error);
|
||
return '';
|
||
}
|
||
}
|