Files
InsanityLab-web/src/lib/upload-rules.ts
T
2026-07-05 17:18:30 +02:00

17 lines
724 B
TypeScript

import { extname } from 'node:path';
export const ALLOWED: Record<string, string> = {
'.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.webp': 'image/webp', '.gif': 'image/gif',
};
export const MAX_BYTES = 5 * 1024 * 1024;
export function validateUploadFile(file: unknown):
| { ok: true; file: File; ext: string }
| { ok: false; error: string } {
if (!(file instanceof File)) return { ok: false, error: 'Nessun file.' };
const ext = extname(file.name).toLowerCase();
if (!ALLOWED[ext]) return { ok: false, error: 'Formato non consentito (jpg, png, webp, gif).' };
if (file.size > MAX_BYTES) return { ok: false, error: 'File troppo grande (max 5 MB).' };
return { ok: true, file, ext };
}