import { NextRequest, NextResponse } from 'next/server' import { requireAdmin } from '@/lib/admin-auth' import { createServiceClient } from '@/lib/supabase' const BUCKET = 'ueber-uns-bilder' const ALLOWED = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'] const MAX_SIZE = 8 * 1024 * 1024 export async function POST(req: NextRequest) { const check = await requireAdmin() if (check instanceof NextResponse) return check const formData = await req.formData() const file = formData.get('file') as File | null if (!file) return NextResponse.json({ error: 'Keine Datei.' }, { status: 400 }) if (!ALLOWED.includes(file.type)) return NextResponse.json({ error: 'Nur JPG, PNG oder WebP.' }, { status: 400 }) if (file.size > MAX_SIZE) return NextResponse.json({ error: 'Maximal 8 MB.' }, { status: 400 }) const db = createServiceClient() const ext = file.name.split('.').pop() ?? 'jpg' const path = `ueber-uns/${Date.now()}.${ext}` const { error } = await db.storage.from(BUCKET).upload(path, await file.arrayBuffer(), { contentType: file.type, upsert: true }) if (error) return NextResponse.json({ error: error.message }, { status: 500 }) const base = (process.env.SUPABASE_INTERNAL_URL ?? process.env.NEXT_PUBLIC_SUPABASE_URL!).replace(/\/$/, '') return NextResponse.json({ url: `${base}/storage/v1/object/public/${BUCKET}/${path}` }) }