| import { NextRequest, NextResponse } from "next/server"; |
| import { setProgress } from "@/lib/progress"; |
| import { renderProject } from "@/lib/render-pipeline"; |
| import { loadProject, saveProject } from "@/lib/storage"; |
|
|
| export const runtime = "nodejs"; |
| export const maxDuration = 300; |
|
|
| export async function POST(req: NextRequest) { |
| try { |
| const body = await req.json(); |
| const id = body.id as string; |
|
|
| if (!id) { |
| return NextResponse.json({ error: "Missing project id" }, { status: 400 }); |
| } |
|
|
| const project = await loadProject(id); |
| if (!project) { |
| return NextResponse.json({ error: "Project not found" }, { status: 404 }); |
| } |
|
|
| if (body.captions) project.captions = body.captions; |
| if (body.style) project.style = body.style; |
| if (body.layout) project.layout = body.layout; |
| if (body.animation) project.animation = body.animation; |
|
|
| if (!project.captions.length) { |
| return NextResponse.json({ error: "No captions to render" }, { status: 400 }); |
| } |
|
|
| if (project.status === "rendering") { |
| return NextResponse.json({ message: "Render already in progress" }); |
| } |
|
|
| project.status = "rendering"; |
| await saveProject(project); |
|
|
| setProgress(id, { |
| percent: 0, |
| stage: "extracting", |
| message: "Starting render...", |
| }); |
|
|
| void renderProject(project).catch((error) => { |
| console.error("Render error:", error); |
| setProgress(id, { |
| percent: 0, |
| stage: "error", |
| message: error instanceof Error ? error.message : "Render failed", |
| }); |
| }); |
|
|
| return NextResponse.json({ message: "Render started", id }); |
| } catch (error) { |
| return NextResponse.json( |
| { error: error instanceof Error ? error.message : "Render failed" }, |
| { status: 500 }, |
| ); |
| } |
| } |
|
|