fix: 去水印支持base64格式图片,先上传S3再调用API
This commit is contained in:
parent
0b8eb6edea
commit
31664cb4fd
@ -88,7 +88,7 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
// 获取请求参数 - 只接受URL方式
|
||||
// 获取请求参数
|
||||
const body = await request.json();
|
||||
const { image_url } = body;
|
||||
|
||||
@ -99,14 +99,6 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// 验证URL格式
|
||||
if (!image_url.startsWith("http://") && !image_url.startsWith("https://")) {
|
||||
return NextResponse.json(
|
||||
{ error: "图片URL格式无效" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 获取API配置
|
||||
const config = await getWatermarkRemoveConfig();
|
||||
if (!config?.apiKey) {
|
||||
@ -116,25 +108,64 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
console.log("[WatermarkRemove] 原始图片URL:", image_url);
|
||||
console.log("[WatermarkRemove] 使用原图URL:", image_url.substring(0, 100) + "...");
|
||||
|
||||
// 下载原图并重新上传到S3,确保获取公网可访问的URL
|
||||
let publicImageUrl = image_url;
|
||||
try {
|
||||
console.log("[WatermarkRemove] 下载原图并上传到S3...");
|
||||
const imageResponse = await fetch(image_url);
|
||||
if (!imageResponse.ok) {
|
||||
throw new Error("下载原图失败");
|
||||
// 处理图片URL,确保获取公网可访问的URL
|
||||
let publicImageUrl: string;
|
||||
|
||||
// 如果是 base64 格式的 data URL,先上传到 S3
|
||||
if (image_url.startsWith("data:")) {
|
||||
console.log("[WatermarkRemove] 检测到base64格式,上传到S3...");
|
||||
try {
|
||||
// 解析 base64 data URL
|
||||
const matches = image_url.match(/^data:image\/(\w+);base64,(.+)$/);
|
||||
if (!matches) {
|
||||
return NextResponse.json(
|
||||
{ error: "无效的图片格式" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const ext = matches[1] === "jpeg" ? "jpg" : matches[1];
|
||||
const base64Data = matches[2];
|
||||
const buffer = Buffer.from(base64Data, "base64");
|
||||
|
||||
// 上传到 S3
|
||||
const filename = `watermark-input-${Date.now()}.${ext}`;
|
||||
const uploadResult = await uploadFile(filename, buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
|
||||
publicImageUrl = uploadResult.url;
|
||||
console.log("[WatermarkRemove] base64图片已上传到S3:", publicImageUrl);
|
||||
} catch (uploadError) {
|
||||
console.error("[WatermarkRemove] base64上传失败:", uploadError);
|
||||
return NextResponse.json(
|
||||
{ error: "图片上传失败" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
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");
|
||||
}
|
||||
// 如果是 HTTP/HTTPS URL,下载后重新上传确保公网可访问
|
||||
else if (image_url.startsWith("http://") || image_url.startsWith("https://")) {
|
||||
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");
|
||||
publicImageUrl = image_url;
|
||||
}
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ error: "图片URL格式无效" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log("[WatermarkRemove] 开始处理图片:", publicImageUrl);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user