mirror of
https://github.com/Since-3/since3-portal.git
synced 2026-10-12 21:41:16 +00:00
31 lines
1.5 KiB
TypeScript
31 lines
1.5 KiB
TypeScript
import type { NextRequest } from "next/server"
|
||
import { getProfile } from "@/lib/supabase/dal"
|
||
import { buildProject } from "@/modules/templates/export/project"
|
||
import { resolveContent } from "@/modules/templates/lib/content"
|
||
import { getTemplate } from "@/modules/templates/lib/registry"
|
||
import { getVariant } from "@/modules/templates/queries/variants"
|
||
|
||
// Gespeicherter Stand einer Variante als eigenständiges Next.js-Projekt (ZIP) – nur für Admins
|
||
export async function GET(_request: NextRequest, ctx: RouteContext<"/api/templates/[id]/export">) {
|
||
const profile = await getProfile()
|
||
if (profile?.role !== "admin") return Response.json({ error: "Keine Berechtigung." }, { status: 403 })
|
||
|
||
const { id } = await ctx.params
|
||
const variant = await getVariant(id)
|
||
const def = variant ? getTemplate(variant.template_key) : undefined
|
||
if (!variant || !def) return Response.json({ error: "Variante nicht gefunden." }, { status: 404 })
|
||
|
||
try {
|
||
const { fileName, zip } = await buildProject(def, resolveContent(def, variant.content), variant.name)
|
||
return new Response(zip, {
|
||
headers: {
|
||
"Content-Type": "application/zip",
|
||
"Content-Disposition": `attachment; filename="${fileName}"`,
|
||
"Cache-Control": "no-store",
|
||
},
|
||
})
|
||
} catch (error) {
|
||
console.error("Template export failed:", error)
|
||
return Response.json({ error: "Export fehlgeschlagen." }, { status: 500 })
|
||
}
|
||
}
|