115 lines
3.6 KiB
TypeScript
115 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
|
||
import { auth } from "@/app/(auth)/auth";
|
||
import {
|
||
executeRmbgJob,
|
||
fetchImageBufferFromUrl,
|
||
RmbgServiceError,
|
||
} from "@/lib/ai/rmbg/service";
|
||
|
||
export const runtime = "nodejs";
|
||
export const maxDuration = 60;
|
||
|
||
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") || "";
|
||
|
||
let imageBuffer: Buffer;
|
||
let imageBase64: string | undefined;
|
||
let prompt = "抠图处理";
|
||
let chatId: string | null = null;
|
||
let messageId: string | null = null;
|
||
let skipGallerySave = false;
|
||
let sourceImageUrl: string | null = null;
|
||
|
||
if (contentType.includes("application/json")) {
|
||
const body = await request.json();
|
||
prompt = body.prompt || "抠图处理";
|
||
chatId = body.chatId || null;
|
||
messageId = body.messageId || null;
|
||
skipGallerySave = body.skipGallerySave === true || body.skip_gallery_save === 1;
|
||
|
||
const imageUrl = body.imageUrl || body.image_url;
|
||
const explicitSourceImageUrl = body.sourceImageUrl || body.source_image_url;
|
||
|
||
if (imageUrl) {
|
||
sourceImageUrl = String(imageUrl);
|
||
imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl);
|
||
} else if (body.image_base64) {
|
||
imageBase64 = String(body.image_base64);
|
||
imageBuffer = Buffer.from(imageBase64, "base64");
|
||
if (explicitSourceImageUrl) {
|
||
sourceImageUrl = String(explicitSourceImageUrl);
|
||
}
|
||
} else {
|
||
return NextResponse.json(
|
||
{ error: "缺少 imageUrl 或 image_base64 参数" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
} else if (contentType.includes("multipart/form-data")) {
|
||
const formData = await request.formData();
|
||
const file = formData.get("file") as File | null;
|
||
|
||
if (!file) {
|
||
return NextResponse.json({ error: "缺少file参数" }, { status: 400 });
|
||
}
|
||
|
||
const arrayBuffer = await file.arrayBuffer();
|
||
imageBuffer = Buffer.from(arrayBuffer);
|
||
imageBase64 = imageBuffer.toString("base64");
|
||
prompt = (formData.get("prompt") as string) || "抠图处理";
|
||
chatId = (formData.get("chatId") as string) || null;
|
||
messageId = (formData.get("messageId") as string) || null;
|
||
skipGallerySave =
|
||
formData.get("skipGallerySave") === "true" || formData.get("skip_gallery_save") === "1";
|
||
sourceImageUrl =
|
||
(formData.get("sourceImageUrl") as string) ||
|
||
(formData.get("source_image_url") as string) ||
|
||
null;
|
||
} else {
|
||
return NextResponse.json(
|
||
{ error: "不支持的Content-Type,请使用application/json或multipart/form-data" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
const result = await executeRmbgJob({
|
||
userId: session.user.id,
|
||
imageBuffer,
|
||
imageBase64,
|
||
prompt,
|
||
chatId,
|
||
messageId,
|
||
skipGallerySave,
|
||
sourceImageUrl,
|
||
});
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
...result,
|
||
});
|
||
} catch (error) {
|
||
if (error instanceof RmbgServiceError) {
|
||
return NextResponse.json(
|
||
{
|
||
error: error.message,
|
||
detail: error.detail || error.message,
|
||
},
|
||
{ status: error.status }
|
||
);
|
||
}
|
||
|
||
console.error("[RMBG API] 处理错误:", error);
|
||
return NextResponse.json(
|
||
{ error: "服务器内部错误", detail: error instanceof Error ? error.message : "未知错误" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|