修改Real-ESRGAN备用API为URL方式调用
变更: - 改为先上传原图到S3获取URL - 使用/upscale/url接口而非/upscale/base64 - URL方式传输效率更高,适合大图处理 - Real-ESRGAN API会自动处理并返回处理后的S3 URL
This commit is contained in:
parent
88ac9e2a46
commit
00b167909f
@ -420,39 +420,44 @@ export async function POST(request: NextRequest) {
|
|||||||
|
|
||||||
// ========== 备用API处理路径 (Real-ESRGAN) ==========
|
// ========== 备用API处理路径 (Real-ESRGAN) ==========
|
||||||
if (useBackupApi) {
|
if (useBackupApi) {
|
||||||
console.log("[Enhance API] 使用Real-ESRGAN备用API处理...");
|
console.log("[Enhance API] 使用Real-ESRGAN备用API处理(URL方式)...");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 使用Base64方式调用Real-ESRGAN API
|
// 步骤1: 先将Base64图片上传到S3获取公开可访问的URL
|
||||||
const result = await processWithRealESRGANBase64(imageBase64, {
|
console.log(`[Enhance-RealESRGAN] 📤 步骤1: 上传原图到S3获取URL...`);
|
||||||
|
const tempUploadResult = await uploadBase64ToS3(imageBase64, "image/jpeg");
|
||||||
|
const sourceImageUrl = tempUploadResult.original;
|
||||||
|
console.log(`[Enhance-RealESRGAN] ✅ 原图已上传,URL: ${sourceImageUrl}`);
|
||||||
|
|
||||||
|
// 步骤2: 使用URL方式调用Real-ESRGAN API
|
||||||
|
console.log(`[Enhance-RealESRGAN] 🔄 步骤2: 调用Real-ESRGAN API处理...`);
|
||||||
|
const result = await processWithRealESRGAN(sourceImageUrl, {
|
||||||
backupApiUrl: config.backupApiUrl!,
|
backupApiUrl: config.backupApiUrl!,
|
||||||
backupApiKey: config.backupApiKey!,
|
backupApiKey: config.backupApiKey!,
|
||||||
backupScale: config.backupScale || "4",
|
backupScale: config.backupScale || "4",
|
||||||
backupModel: config.backupModel || "RealESRGAN_x4plus",
|
backupModel: config.backupModel || "RealESRGAN_x4plus",
|
||||||
});
|
});
|
||||||
|
|
||||||
// 上传处理后的图片到S3
|
// 步骤3: Real-ESRGAN API返回的URL就是处理后的图片
|
||||||
|
// 需要重新上传到我们的S3(因为Real-ESRGAN返回的URL可能在另一个S3桶)
|
||||||
|
console.log(`[Enhance-RealESRGAN] 📤 步骤3: 上传处理后的图片到S3...`);
|
||||||
let thumbnailUrl: string;
|
let thumbnailUrl: string;
|
||||||
let originalUrl: string;
|
let originalUrl: string;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`[Enhance-RealESRGAN] 📤 上传处理后的图片到S3...`);
|
|
||||||
const uploadResult = await uploadBase64ToS3(result.imageBase64, "image/png");
|
const uploadResult = await uploadBase64ToS3(result.imageBase64, "image/png");
|
||||||
thumbnailUrl = uploadResult.thumbnail;
|
thumbnailUrl = uploadResult.thumbnail;
|
||||||
originalUrl = uploadResult.original;
|
originalUrl = uploadResult.original;
|
||||||
console.log(`[Enhance-RealESRGAN] ✅ 上传成功: ${originalUrl}`);
|
console.log(`[Enhance-RealESRGAN] ✅ 处理后图片已上传: ${originalUrl}`);
|
||||||
} catch (uploadError) {
|
} catch (uploadError) {
|
||||||
console.error("[Enhance-RealESRGAN] ❌ 上传到S3失败:", uploadError);
|
console.error("[Enhance-RealESRGAN] ❌ 上传到S3失败:", uploadError);
|
||||||
return NextResponse.json(
|
// 如果上传失败,直接使用Real-ESRGAN返回的URL
|
||||||
{
|
console.log(`[Enhance-RealESRGAN] ⚠️ 使用Real-ESRGAN返回的URL作为备选: ${result.imageUrl}`);
|
||||||
error: "图片上传到存储桶失败",
|
thumbnailUrl = result.imageUrl;
|
||||||
detail: uploadError instanceof Error ? uploadError.message : "未知错误"
|
originalUrl = result.imageUrl;
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存到数据库
|
// 步骤4: 保存到数据库
|
||||||
try {
|
try {
|
||||||
const [savedImage] = await db.insert(generatedImage).values({
|
const [savedImage] = await db.insert(generatedImage).values({
|
||||||
userId,
|
userId,
|
||||||
@ -471,13 +476,14 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 返回结果
|
// 返回结果
|
||||||
|
console.log(`[Enhance-RealESRGAN] ✅ 全部完成,总耗时: ${result.processingTime.toFixed(2)}秒`);
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
image_base64: result.imageBase64,
|
image_base64: result.imageBase64,
|
||||||
thumbnail_url: thumbnailUrl,
|
thumbnail_url: thumbnailUrl,
|
||||||
original_url: originalUrl,
|
original_url: originalUrl,
|
||||||
processing_time: result.processingTime,
|
processing_time: result.processingTime,
|
||||||
api_type: "realesrgan",
|
api_type: "realesrgan-url",
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user