MBO-Tech-IT-Webseite/modules/06-website-cms/files/app/api/admin/hero/favicon/route.ts

50 lines
1.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { requireAdmin } from '@/lib/admin-auth'
import { createServiceClient } from '@/lib/supabase'
const BUCKET = 'site-assets'
export async function POST(req: NextRequest) {
const check = await requireAdmin()
if (check instanceof NextResponse) return check
const db = createServiceClient()
const formData = await req.formData()
const file = formData.get('file') as File
if (!file) return NextResponse.json({ error: 'file erforderlich' }, { status: 400 })
const ext = file.name.split('.').pop()?.toLowerCase() ?? 'png'
const path = `favicon-${Date.now()}.${ext}`
const { data: existing } = await db.from('hero_content').select('favicon_path').limit(1).single()
if (existing?.favicon_path) {
await db.storage.from(BUCKET).remove([existing.favicon_path])
}
const { error: uploadError } = await db.storage.from(BUCKET).upload(path, file, { contentType: file.type, upsert: true })
if (uploadError) return NextResponse.json({ error: uploadError.message }, { status: 500 })
const { data: row } = await db.from('hero_content').select('id').limit(1).single()
const { error } = row
? await db.from('hero_content').update({ favicon_path: path }).eq('id', row.id)
: await db.from('hero_content').insert({ favicon_path: path })
if (error) return NextResponse.json({ error: error.message }, { status: 500 })
return NextResponse.json({ path })
}
export async function DELETE() {
const check = await requireAdmin()
if (check instanceof NextResponse) return check
const db = createServiceClient()
const { data: existing } = await db.from('hero_content').select('id, favicon_path').limit(1).single()
if (existing?.favicon_path) {
await db.storage.from(BUCKET).remove([existing.favicon_path])
}
if (existing?.id) {
await db.from('hero_content').update({ favicon_path: null }).eq('id', existing.id)
}
return NextResponse.json({ success: true })
}