22 lines
756 B
TypeScript
22 lines
756 B
TypeScript
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;
|
|
}
|