import { NextResponse } from 'next/server'; import path from 'path'; import { writeFile } from 'fs/promises'; import { randomUUID } from 'crypto'; import { UPLOADS_DIR } from '@/lib/db'; export const dynamic = 'force-dynamic'; const ALLOWED_TYPES: Record = { 'image/jpeg': 'jpg', 'image/png': 'png', 'image/gif': 'gif', 'image/webp': 'webp', }; const MAX_SIZE_BYTES = 10 * 1024 * 1024; // 10 MB export async function POST(request: Request) { const formData = await request.formData(); const file = formData.get('file'); if (!file || !(file instanceof File)) { return NextResponse.json({ error: 'No file provided' }, { status: 400 }); } if (!ALLOWED_TYPES[file.type]) { return NextResponse.json({ error: 'Invalid file type' }, { status: 400 }); } if (file.size > MAX_SIZE_BYTES) { return NextResponse.json({ error: 'File too large (max 10 MB)' }, { status: 400 }); } const ext = ALLOWED_TYPES[file.type]; const filename = `${randomUUID()}.${ext}`; const buffer = Buffer.from(await file.arrayBuffer()); await writeFile(path.join(UPLOADS_DIR, filename), buffer); return NextResponse.json({ filename }); }