67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
import { auth } from "@/app/(auth)/auth";
|
||
import {
|
||
executeResizeJob,
|
||
ResizeServiceError,
|
||
} from "@/lib/ai/resize/service";
|
||
|
||
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 imageBase64 = String(body.image_base64 || "").trim();
|
||
const canvasWidth = Number(body.canvasWidth);
|
||
const canvasHeight = Number(body.canvasHeight);
|
||
|
||
if (!imageBase64) {
|
||
return NextResponse.json({ error: "缺少image_base64参数" }, { status: 400 });
|
||
}
|
||
|
||
const result = await executeResizeJob({
|
||
userId: session.user.id,
|
||
imageBuffer: Buffer.from(imageBase64, "base64"),
|
||
prompt: body.prompt || "尺寸调整处理",
|
||
chatId: body.chatId || null,
|
||
messageId: body.messageId || null,
|
||
canvasWidth,
|
||
canvasHeight,
|
||
mimeType: body.mimeType || "image/png",
|
||
transparentBackground: body.transparentBackground === true,
|
||
backgroundColor: body.backgroundColor || undefined,
|
||
});
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
...result,
|
||
});
|
||
} catch (error) {
|
||
if (error instanceof ResizeServiceError) {
|
||
return NextResponse.json(
|
||
{
|
||
error: error.message,
|
||
detail: error.detail || error.message,
|
||
},
|
||
{ status: error.status },
|
||
);
|
||
}
|
||
|
||
console.error("[Resize API] 处理错误:", error);
|
||
return NextResponse.json(
|
||
{ error: "服务器内部错误", detail: error instanceof Error ? error.message : "未知错误" },
|
||
{ status: 500 },
|
||
);
|
||
}
|
||
}
|