fix: 修复本地上传资源路径解析
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
export function resolveMediaUrl(url?: string | null): string {
|
||||
const value = String(url || '').trim();
|
||||
if (!value) return '';
|
||||
if (/^\d{4}\/\d{2}\/\d{2}\/[^?#]+$/i.test(value)) {
|
||||
return `/uploads/${value}`;
|
||||
}
|
||||
if (/^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?\/uploads\//i.test(value)) {
|
||||
return value.replace(/^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?/i, '');
|
||||
}
|
||||
if (typeof window !== 'undefined' && /^https?:\/\//i.test(value)) {
|
||||
try {
|
||||
const parsed = new URL(value);
|
||||
if (parsed.origin === window.location.origin && parsed.pathname.startsWith('/uploads/')) {
|
||||
return `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
||||
}
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user