33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { listTemplates, saveTemplate } from '@/lib/mockupTemplates'
|
|
|
|
export async function GET() {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session) return Response.json({ error: 'Non authentifié' }, { status: 401 })
|
|
const templates = await listTemplates()
|
|
return Response.json(templates)
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session) return Response.json({ error: 'Non authentifié' }, { status: 401 })
|
|
|
|
const form = await req.formData()
|
|
const name = form.get('name') as string
|
|
const family = form.get('family') as string
|
|
const placement = (form.get('placement') as string) || undefined
|
|
const description = (form.get('description') as string) ?? ''
|
|
const imageFile = form.get('image') as File
|
|
|
|
if (!name || !family || !imageFile) {
|
|
return Response.json({ error: 'name, family et image requis' }, { status: 400 })
|
|
}
|
|
|
|
const buffer = Buffer.from(await imageFile.arrayBuffer())
|
|
const ext = imageFile.name.split('.').pop() ?? 'jpg'
|
|
const template = await saveTemplate({ name, family, placement, description, filename: '' }, buffer, ext)
|
|
return Response.json(template)
|
|
}
|