avoid regex overflow in watermark base64 uploads
This commit is contained in:
parent
e300153a01
commit
c9808d4f5b
@ -319,16 +319,24 @@ export async function POST(request: NextRequest) {
|
||||
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) {
|
||||
// 避免对超长 base64 使用正则,直接按 data URL 头部拆分
|
||||
const commaIndex = image_url.indexOf(",");
|
||||
if (commaIndex <= 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "无效的图片格式" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const ext = matches[1] === "jpeg" ? "jpg" : matches[1];
|
||||
const base64Data = matches[2];
|
||||
const header = image_url.slice(0, commaIndex);
|
||||
const base64Data = image_url.slice(commaIndex + 1);
|
||||
const headerMatch = header.match(/^data:image\/([a-zA-Z0-9.+-]+);base64$/i);
|
||||
if (!headerMatch || !base64Data) {
|
||||
return NextResponse.json(
|
||||
{ error: "无效的图片格式" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const ext = headerMatch[1].toLowerCase() === "jpeg" ? "jpg" : headerMatch[1].toLowerCase();
|
||||
const buffer = Buffer.from(base64Data, "base64");
|
||||
|
||||
// 上传到 S3
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user