import { t } from "$lib/i18n.svelte"; /** Studio image slots (ref-upload-spec §3): PNG / JPG / WebP, ≤ 20MB each. */ export const IMAGE_SLOT_ACCEPT = ".png,.jpg,.jpeg,.webp,image/png,image/jpeg,image/webp"; export const IMAGE_SLOT_MAX_BYTES = 20 * 1024 * 1024; const IMAGE_EXTS = [".png", ".jpg", ".jpeg", ".webp"]; /** Reference video slots (HEL-643): MP4 / WebM / MOV, ≤ 500MB (upload cap). */ export const VIDEO_SLOT_ACCEPT = "video/mp4,video/webm,video/quicktime,.mp4,.webm,.mov"; export const VIDEO_SLOT_MAX_BYTES = 500 * 1024 * 1024; const VIDEO_EXTS = [".mp4", ".webm", ".mov"]; /** Reference audio slots (HEL-643): MP3 / WAV / M4A, ≤ 100MB. */ export const AUDIO_SLOT_ACCEPT = "audio/mpeg,audio/wav,audio/mp4,.mp3,.wav,.m4a"; export const AUDIO_SLOT_MAX_BYTES = 100 * 1024 * 1024; const AUDIO_EXTS = [".mp3", ".wav", ".m4a"]; export type MediaSlotKind = "image" | "video" | "audio"; export function mediaSlotAccept(kind: MediaSlotKind): string { if (kind === "video") return VIDEO_SLOT_ACCEPT; if (kind === "audio") return AUDIO_SLOT_ACCEPT; return IMAGE_SLOT_ACCEPT; } /** Why this file cannot fill a media slot of this kind (null = fine). */ export function mediaFileProblem( kind: MediaSlotKind, file: { name: string; size: number }, ): string | null { const dot = file.name.lastIndexOf("."); const ext = dot >= 0 ? file.name.slice(dot).toLowerCase() : ""; const max = kind === "video" ? VIDEO_SLOT_MAX_BYTES : kind === "audio" ? AUDIO_SLOT_MAX_BYTES : IMAGE_SLOT_MAX_BYTES; if (kind === "video" && !VIDEO_EXTS.includes(ext)) return t("studio.errVideoFormat", { name: file.name }); if (kind === "audio" && !AUDIO_EXTS.includes(ext)) return t("studio.errAudioFormat", { name: file.name }); if (kind === "image" && !IMAGE_EXTS.includes(ext)) return t("studio.errImageFormat", { name: file.name }); if (file.size > max) return kind === "image" ? t("studio.errImageSize", { name: file.name }) : t("studio.errMediaSize", { name: file.name }); return null; } /** Why this file cannot fill an image slot (null = fine). */ export function imageFileProblem(file: { name: string; size: number; }): string | null { return mediaFileProblem("image", file); } export function formatBytes(bytes: number): string { if (bytes >= 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`; return `${Math.max(1, Math.round(bytes / 1024))} KB`; } /** Display name of an uploaded path (strips the uuid8- prefix the upload endpoint writes). */ export function uploadBaseName(p: string): string { const raw = p.split(/[/\\]/).pop() ?? p; return /^[0-9a-f]{8}-/.test(raw) ? raw.slice(9) : raw; }