61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
import { auth } from "@/app/(auth)/auth";
|
||
import { executeLayerSplitJob, LayerSplitServiceError } from "@/lib/ai/layer-split/service";
|
||
|
||
export const runtime = "nodejs";
|
||
export const maxDuration = 300;
|
||
|
||
export async function POST(request: NextRequest) {
|
||
try {
|
||
const session = await auth();
|
||
if (!session?.user?.id) {
|
||
return NextResponse.json({ error: "未授权,请先登录" }, { status: 401 });
|
||
}
|
||
|
||
const contentType = request.headers.get("content-type") || "";
|
||
if (!contentType.includes("application/json")) {
|
||
return NextResponse.json(
|
||
{ error: "不支持的 Content-Type,请使用 application/json" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
const body = await request.json();
|
||
const imageUrl = String(body.imageUrl || body.image_url || "").trim();
|
||
if (!imageUrl) {
|
||
return NextResponse.json({ error: "缺少 imageUrl 参数" }, { status: 400 });
|
||
}
|
||
|
||
const result = await executeLayerSplitJob({
|
||
userId: session.user.id,
|
||
imageUrl,
|
||
prompt: String(body.prompt || "").trim() || undefined,
|
||
chatId: String(body.chatId || "").trim() || null,
|
||
messageId: String(body.messageId || "").trim() || null,
|
||
sourceNodeId: String(body.sourceNodeId || "").trim() || null,
|
||
skipGallerySave: body.skipGallerySave === true,
|
||
});
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
...result,
|
||
});
|
||
} catch (error) {
|
||
if (error instanceof LayerSplitServiceError) {
|
||
return NextResponse.json(
|
||
{
|
||
error: error.message,
|
||
detail: error.detail || error.message,
|
||
},
|
||
{ status: error.status }
|
||
);
|
||
}
|
||
|
||
console.error("[LayerSplit API] 处理错误:", error);
|
||
return NextResponse.json(
|
||
{ error: "服务器内部错误", detail: error instanceof Error ? error.message : "未知错误" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|