77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
import { auth } from "@/app/(auth)/auth";
|
|
import {
|
|
BoxCutoutServiceError,
|
|
executeBoxCutoutJob,
|
|
} from "@/lib/ai/box-cutout/service";
|
|
import { RmbgServiceError } from "@/lib/ai/rmbg/service";
|
|
|
|
export const runtime = "nodejs";
|
|
export const maxDuration = 60;
|
|
|
|
type BoxCutoutRequest = {
|
|
imageUrl?: string;
|
|
cropRect?: {
|
|
x?: number;
|
|
y?: number;
|
|
width?: number;
|
|
height?: number;
|
|
};
|
|
paddingRatio?: number;
|
|
prompt?: string;
|
|
chatId?: string | null;
|
|
messageId?: string | null;
|
|
skipGallerySave?: boolean;
|
|
sourceNodeId?: string;
|
|
};
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ success: false, error: "未授权,请先登录" }, { status: 401 });
|
|
}
|
|
|
|
const body = (await request.json()) as BoxCutoutRequest;
|
|
const rawImageUrl = String(body.imageUrl || "").trim();
|
|
if (!rawImageUrl) {
|
|
return NextResponse.json({ success: false, error: "缺少 imageUrl 参数" }, { status: 400 });
|
|
}
|
|
|
|
const resolvedImageUrl = new URL(rawImageUrl, request.url).toString();
|
|
const result = await executeBoxCutoutJob({
|
|
userId: session.user.id,
|
|
imageUrl: resolvedImageUrl,
|
|
cropRect: body.cropRect || {},
|
|
paddingRatio: body.paddingRatio,
|
|
prompt: body.prompt || "框选抠图",
|
|
chatId: body.chatId || null,
|
|
messageId: body.messageId || null,
|
|
skipGallerySave: body.skipGallerySave === true,
|
|
sourceNodeId: body.sourceNodeId,
|
|
});
|
|
|
|
return NextResponse.json(result);
|
|
} catch (error) {
|
|
if (error instanceof BoxCutoutServiceError || error instanceof RmbgServiceError) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: error.detail || error.message,
|
|
},
|
|
{ status: error.status },
|
|
);
|
|
}
|
|
|
|
console.error("[BoxCutout] 处理失败:", error);
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: error instanceof Error ? error.message : "框选抠图失败",
|
|
},
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|