280 lines
8.9 KiB
TypeScript
280 lines
8.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
import { getSystemSetting } from "@/lib/db/queries";
|
||
import { auth } from "@/app/(auth)/auth";
|
||
import { uploadFile } from "@/lib/storage/service";
|
||
import { db } from "@/lib/db";
|
||
import { generatedImage } from "@/lib/db/schema";
|
||
|
||
/**
|
||
* 压缩图片生成缩略图(使用 sharp)
|
||
*/
|
||
async function createThumbnail(buffer: Buffer, mimeType: string): Promise<Buffer> {
|
||
try {
|
||
const sharp = await import("sharp");
|
||
|
||
// 创建缩略图(宽度 500px,高度按比例,质量 80%)
|
||
const thumbnailBuffer = await sharp.default(buffer)
|
||
.resize(500, null, {
|
||
fit: "inside",
|
||
withoutEnlargement: true,
|
||
})
|
||
.png({ quality: 80 }) // PNG格式保持透明背景
|
||
.toBuffer();
|
||
|
||
console.log(`[RMBG] Thumbnail created, size: ${thumbnailBuffer.length} bytes (original: ${buffer.length} bytes)`);
|
||
return thumbnailBuffer;
|
||
} catch (error) {
|
||
console.error("[RMBG] Failed to create thumbnail:", error);
|
||
// 如果失败,返回原图
|
||
return buffer;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 将 base64 图片上传到 S3,返回缩略图和原图的 URL
|
||
*/
|
||
async function uploadBase64ToS3(base64Data: string, mimeType: string = "image/png"): Promise<{ thumbnail: string; original: string }> {
|
||
try {
|
||
// 解析 base64 数据
|
||
const buffer = Buffer.from(base64Data, "base64");
|
||
|
||
// 生成文件名
|
||
const ext = mimeType.split("/")[1] || "png";
|
||
const timestamp = Date.now();
|
||
const randomStr = Math.random().toString(36).slice(2, 8);
|
||
const thumbnailFilename = `rmbg-thumb-${timestamp}-${randomStr}.${ext}`;
|
||
const originalFilename = `rmbg-orig-${timestamp}-${randomStr}.${ext}`;
|
||
|
||
console.log(`[RMBG] Original size: ${buffer.length} bytes`);
|
||
|
||
// 生成缩略图
|
||
const thumbnailBuffer = await createThumbnail(buffer, mimeType);
|
||
|
||
// 转换为 ArrayBuffer
|
||
const thumbnailArrayBuffer = thumbnailBuffer.buffer.slice(
|
||
thumbnailBuffer.byteOffset,
|
||
thumbnailBuffer.byteOffset + thumbnailBuffer.byteLength
|
||
) as ArrayBuffer;
|
||
const originalArrayBuffer = buffer.buffer.slice(
|
||
buffer.byteOffset,
|
||
buffer.byteOffset + buffer.byteLength
|
||
) as ArrayBuffer;
|
||
|
||
// 并行上传缩略图和原图到 S3
|
||
console.log(`[RMBG] Uploading thumbnail and original to S3...`);
|
||
const [thumbnailResult, originalResult] = await Promise.all([
|
||
uploadFile(thumbnailFilename, thumbnailArrayBuffer),
|
||
uploadFile(originalFilename, originalArrayBuffer),
|
||
]);
|
||
|
||
console.log(`[RMBG] Uploaded to S3:`);
|
||
console.log(` - Thumbnail: ${thumbnailResult.url}`);
|
||
console.log(` - Original: ${originalResult.url}`);
|
||
|
||
return {
|
||
thumbnail: thumbnailResult.url,
|
||
original: originalResult.url,
|
||
};
|
||
} catch (error) {
|
||
console.error("[RMBG] Failed to upload to S3:", error);
|
||
throw new Error(`图片上传到存储桶失败: ${error instanceof Error ? error.message : "未知错误"}。请检查系统设置中的存储配置。`);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 抠图API路由
|
||
* 从数据库读取RMBG API配置,调用外部API进行抠图处理
|
||
* 将处理后的图片上传到S3并保存到数据库
|
||
*/
|
||
export async function POST(request: NextRequest) {
|
||
try {
|
||
// 0. 获取用户会话
|
||
const session = await auth();
|
||
if (!session?.user?.id) {
|
||
return NextResponse.json(
|
||
{ error: "未授权,请先登录" },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
|
||
const userId = session.user.id;
|
||
// 1. 从数据库获取RMBG API配置
|
||
const rmbgConfig = await getSystemSetting({ key: "rmbg_api_config" });
|
||
|
||
if (!rmbgConfig || !rmbgConfig.value) {
|
||
return NextResponse.json(
|
||
{ error: "抠图API配置未设置,请在系统设置中配置" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
const config = rmbgConfig.value as {
|
||
apiUrl?: string;
|
||
apiKey?: string;
|
||
};
|
||
|
||
if (!config.apiUrl) {
|
||
return NextResponse.json(
|
||
{ error: "抠图API地址未配置" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 2. 获取请求体(Base64图片或FormData)
|
||
const contentType = request.headers.get("content-type") || "";
|
||
|
||
let imageBase64: string;
|
||
let prompt = ""; // 用户提示词(可选)
|
||
let chatId: string | null = null; // 聊天ID(可选)
|
||
let messageId: string | null = null; // 消息ID(可选)
|
||
|
||
if (contentType.includes("application/json")) {
|
||
// JSON格式:Base64图片
|
||
const body = await request.json();
|
||
imageBase64 = body.image_base64;
|
||
prompt = body.prompt || "抠图处理";
|
||
chatId = body.chatId || null;
|
||
messageId = body.messageId || null;
|
||
|
||
if (!imageBase64) {
|
||
return NextResponse.json(
|
||
{ error: "缺少image_base64参数" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
} else if (contentType.includes("multipart/form-data")) {
|
||
// FormData格式:文件上传
|
||
const formData = await request.formData();
|
||
const file = formData.get("file") as File;
|
||
|
||
if (!file) {
|
||
return NextResponse.json(
|
||
{ error: "缺少file参数" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 将文件转换为Base64
|
||
const arrayBuffer = await file.arrayBuffer();
|
||
const buffer = Buffer.from(arrayBuffer);
|
||
imageBase64 = buffer.toString("base64");
|
||
prompt = (formData.get("prompt") as string) || "抠图处理";
|
||
chatId = (formData.get("chatId") as string) || null;
|
||
messageId = (formData.get("messageId") as string) || null;
|
||
} else {
|
||
return NextResponse.json(
|
||
{ error: "不支持的Content-Type,请使用application/json或multipart/form-data" },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 3. 调用RMBG API
|
||
const apiUrl = config.apiUrl.endsWith("/")
|
||
? `${config.apiUrl}rmbg/base64`
|
||
: `${config.apiUrl}/rmbg/base64`;
|
||
|
||
const requestBody = {
|
||
image_base64: imageBase64,
|
||
};
|
||
|
||
const headers: HeadersInit = {
|
||
"Content-Type": "application/json",
|
||
};
|
||
|
||
// 如果配置了API Key,添加到请求头(根据RMBG API文档要求使用X-API-Key)
|
||
if (config.apiKey) {
|
||
headers["X-API-Key"] = config.apiKey;
|
||
}
|
||
|
||
const startTime = Date.now();
|
||
const response = await fetch(apiUrl, {
|
||
method: "POST",
|
||
headers,
|
||
body: JSON.stringify(requestBody),
|
||
});
|
||
|
||
const processingTime = (Date.now() - startTime) / 1000;
|
||
|
||
if (!response.ok) {
|
||
const errorText = await response.text();
|
||
console.error("[RMBG API] 请求失败:", {
|
||
status: response.status,
|
||
statusText: response.statusText,
|
||
error: errorText,
|
||
});
|
||
|
||
return NextResponse.json(
|
||
{
|
||
error: "抠图API请求失败",
|
||
detail: errorText || response.statusText,
|
||
},
|
||
{ status: response.status }
|
||
);
|
||
}
|
||
|
||
const result = await response.json();
|
||
|
||
// 4. 处理返回结果
|
||
if (!result.success || !result.image_base64) {
|
||
return NextResponse.json(
|
||
{ error: "抠图处理失败", detail: result.detail || "未知错误" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
|
||
// 5. 上传处理后的图片到S3存储桶
|
||
let thumbnailUrl: string;
|
||
let originalUrl: string;
|
||
|
||
try {
|
||
const uploadResult = await uploadBase64ToS3(result.image_base64, "image/png");
|
||
thumbnailUrl = uploadResult.thumbnail;
|
||
originalUrl = uploadResult.original;
|
||
console.log(`[RMBG] ✅ Images uploaded to S3 successfully`);
|
||
} catch (uploadError) {
|
||
console.error("[RMBG] ❌ Failed to upload to S3:", uploadError);
|
||
return NextResponse.json(
|
||
{
|
||
error: "图片上传到存储桶失败",
|
||
detail: uploadError instanceof Error ? uploadError.message : "未知错误"
|
||
},
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
|
||
// 6. 保存图片记录到数据库
|
||
try {
|
||
await db.insert(generatedImage).values({
|
||
userId,
|
||
chatId: chatId || null,
|
||
messageId: messageId || null,
|
||
thumbnailUrl,
|
||
originalUrl,
|
||
imageSize: "2K", // 默认尺寸
|
||
prompt: prompt || "抠图处理",
|
||
modelId: "rmbg-2.0", // 标记为抠图处理
|
||
});
|
||
console.log(`[RMBG] ✅ Image record saved to database`);
|
||
} catch (dbError) {
|
||
console.error("[RMBG] ❌ Failed to save image record to database:", dbError);
|
||
// 即使数据库保存失败,也返回成功(因为图片已上传到S3)
|
||
}
|
||
|
||
// 7. 返回结果(包含S3 URL和Base64,前端可以选择使用)
|
||
return NextResponse.json({
|
||
success: true,
|
||
image_base64: result.image_base64, // 保留Base64用于前端立即显示
|
||
thumbnail_url: thumbnailUrl, // S3缩略图URL
|
||
original_url: originalUrl, // S3原图URL
|
||
processing_time: result.processing_time || processingTime,
|
||
});
|
||
} catch (error) {
|
||
console.error("[RMBG API] 处理错误:", error);
|
||
return NextResponse.json(
|
||
{ error: "服务器内部错误", detail: error instanceof Error ? error.message : "未知错误" },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|