42 lines
1.7 KiB
TypeScript
42 lines
1.7 KiB
TypeScript
import { auth } from "@/app/(auth)/auth";
|
|
import { after, NextResponse } from "next/server";
|
|
import { canvasTaskRequestSchema } from "@/lib/video-canvas/task-contract";
|
|
import {
|
|
createCanvasGroupTasks,
|
|
ownsTaskProject,
|
|
} from "@/lib/video-canvas/task-store";
|
|
import { runCanvasGenerationQueue } from "@/lib/video-canvas/task-worker";
|
|
import { preflightCanvasProductionRequests } from "@/lib/video-canvas/production-services";
|
|
export const runtime = "nodejs",
|
|
maxDuration = 900;
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const session = await auth();
|
|
if (!session?.user?.id)
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
const text = await request.text();
|
|
if (Buffer.byteLength(text) > 1048576)
|
|
return NextResponse.json({ error: "编组数据过大" }, { status: 413 });
|
|
const parsed = canvasTaskRequestSchema
|
|
.array()
|
|
.min(1)
|
|
.max(40)
|
|
.safeParse(JSON.parse(text));
|
|
if (!parsed.success)
|
|
return NextResponse.json({ error: "编组参数无效" }, { status: 400 });
|
|
const projectId = parsed.data[0].projectId,
|
|
userId = session.user.id;
|
|
if (!(await ownsTaskProject(userId, projectId)))
|
|
return NextResponse.json({ error: "画布不存在" }, { status: 404 });
|
|
const tasks = await createCanvasGroupTasks(userId, parsed.data,
|
|
() => preflightCanvasProductionRequests(parsed.data));
|
|
after(() => runCanvasGenerationQueue(userId, projectId));
|
|
return NextResponse.json({ success: true, tasks }, { status: 202 });
|
|
} catch (e) {
|
|
return NextResponse.json(
|
|
{ error: e instanceof Error ? e.message : "编组执行失败", code: (e as any)?.code },
|
|
{ status: Number((e as any)?.status) || 400 }
|
|
);
|
|
}
|
|
}
|