mirror of
https://github.com/moku-project/Moku.git
synced 2026-06-13 09:19:56 -05:00
Implement phase 1
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import {fetchAuthenticated, getAuthMode, getServerBase} from '$lib/core/auth';
|
||||
|
||||
function isAbsoluteUrl(value: string): boolean {
|
||||
return /^https?:\/\//.test(value);
|
||||
}
|
||||
|
||||
export function resolveImageUrl(path: string | null | undefined): string | undefined {
|
||||
if (!path) return undefined;
|
||||
if (isAbsoluteUrl(path)) return path;
|
||||
|
||||
const normalizedBase = getServerBase().replace(/\/$/, '');
|
||||
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
|
||||
return `${normalizedBase}${normalizedPath}`;
|
||||
}
|
||||
|
||||
export async function loadImageObjectUrl(path: string, signal?: AbortSignal): Promise<string> {
|
||||
const resolved = resolveImageUrl(path);
|
||||
if (!resolved) throw new Error('Image URL is missing');
|
||||
|
||||
if (getAuthMode() === 'NONE') {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
const response = await fetchAuthenticated(resolved, {}, signal);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load image: ${response.status}`);
|
||||
}
|
||||
|
||||
return URL.createObjectURL(await response.blob());
|
||||
}
|
||||
Reference in New Issue
Block a user