fix: 去水印功能先上传图片到S3确保公网可访问
This commit is contained in:
parent
1ba3d7ec83
commit
0b8eb6edea
@ -5,6 +5,8 @@ import { systemSettings, generatedImage } from "@/lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { uploadFile } from "@/lib/storage/service";
|
||||
|
||||
// PicWish API 需要公网可访问的 HTTP/HTTPS URL
|
||||
|
||||
// 获取去水印API配置
|
||||
async function getWatermarkRemoveConfig() {
|
||||
try {
|
||||
@ -86,13 +88,21 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
// 获取请求参数
|
||||
// 获取请求参数 - 只接受URL方式
|
||||
const body = await request.json();
|
||||
const { image_url, image_base64 } = body;
|
||||
const { image_url } = body;
|
||||
|
||||
if (!image_url && !image_base64) {
|
||||
if (!image_url) {
|
||||
return NextResponse.json(
|
||||
{ error: "请提供图片URL或Base64数据" },
|
||||
{ error: "请提供图片URL" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 验证URL格式
|
||||
if (!image_url.startsWith("http://") && !image_url.startsWith("https://")) {
|
||||
return NextResponse.json(
|
||||
{ error: "图片URL格式无效" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
@ -106,31 +116,33 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// 如果传入的是base64,需要先上传到S3获取URL
|
||||
let imageUrl = image_url;
|
||||
if (image_base64 && !image_url) {
|
||||
try {
|
||||
const buffer = Buffer.from(image_base64, "base64");
|
||||
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
||||
const filename = `watermark-temp-${Date.now()}.jpeg`;
|
||||
const uploadResult = await uploadFile(filename, arrayBuffer);
|
||||
imageUrl = uploadResult.url;
|
||||
console.log("[WatermarkRemove] Base64图片已上传到S3:", imageUrl);
|
||||
} catch (uploadError) {
|
||||
console.error("[WatermarkRemove] 上传图片失败:", uploadError);
|
||||
return NextResponse.json(
|
||||
{ error: "上传图片失败" },
|
||||
{ status: 500 }
|
||||
);
|
||||
console.log("[WatermarkRemove] 原始图片URL:", image_url);
|
||||
|
||||
// 下载原图并重新上传到S3,确保获取公网可访问的URL
|
||||
let publicImageUrl = image_url;
|
||||
try {
|
||||
console.log("[WatermarkRemove] 下载原图并上传到S3...");
|
||||
const imageResponse = await fetch(image_url);
|
||||
if (!imageResponse.ok) {
|
||||
throw new Error("下载原图失败");
|
||||
}
|
||||
const imageBuffer = await imageResponse.arrayBuffer();
|
||||
const filename = `watermark-input-${Date.now()}.jpg`;
|
||||
const uploadResult = await uploadFile(filename, imageBuffer);
|
||||
publicImageUrl = uploadResult.url;
|
||||
console.log("[WatermarkRemove] 图片已上传到S3:", publicImageUrl);
|
||||
} catch (downloadError) {
|
||||
console.error("[WatermarkRemove] 下载/上传原图失败:", downloadError);
|
||||
// 如果下载失败,尝试使用原始URL
|
||||
console.log("[WatermarkRemove] 尝试使用原始URL");
|
||||
}
|
||||
|
||||
console.log("[WatermarkRemove] 开始处理图片:", imageUrl);
|
||||
console.log("[WatermarkRemove] 开始处理图片:", publicImageUrl);
|
||||
|
||||
// 创建去水印任务
|
||||
const formData = new FormData();
|
||||
formData.append("sync", "0");
|
||||
formData.append("url", imageUrl);
|
||||
formData.append("url", publicImageUrl);
|
||||
|
||||
const createResponse = await fetch(
|
||||
"https://techsz.aoscdn.com/api/tasks/visual/external/watermark-remove",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user