fix: 修复本地上传资源路径解析

This commit is contained in:
Nixevol
2026-07-07 02:23:48 +08:00
parent 98b4b8b079
commit fa98f02458
18 changed files with 186 additions and 48 deletions
+21
View File
@@ -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;
}