File size: 1,796 Bytes
ef4c36f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 },
    );
  }
}