Your Name 864b6d41e3
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: upgrade agent executor and canvas sync
2026-03-07 15:58:03 +08:00

67 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 },
);
}
}