fix: extend watermark remove polling timeout
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-02-15 23:26:42 +08:00
parent 458b15f889
commit 8dea40d137

View File

@ -28,7 +28,7 @@ async function getWatermarkRemoveConfig() {
// 轮询获取处理结果 // 轮询获取处理结果
async function pollForResult(taskId: string, apiKey: string): Promise<{ success: boolean; imageUrl?: string; error?: string }> { async function pollForResult(taskId: string, apiKey: string): Promise<{ success: boolean; imageUrl?: string; error?: string }> {
const maxAttempts = 30; // 最多轮询30次(30秒) const maxAttempts = 180; // 最多轮询180次(约3分钟)
const pollInterval = 1000; // 每次间隔1秒 const pollInterval = 1000; // 每次间隔1秒
for (let attempt = 0; attempt < maxAttempts; attempt++) { for (let attempt = 0; attempt < maxAttempts; attempt++) {
@ -54,19 +54,25 @@ async function pollForResult(taskId: string, apiKey: string): Promise<{ success:
const result = await response.json(); const result = await response.json();
console.log(`[WatermarkRemove] 轮询结果 (attempt ${attempt + 1}):`, JSON.stringify(result).substring(0, 200)); console.log(`[WatermarkRemove] 轮询结果 (attempt ${attempt + 1}):`, JSON.stringify(result).substring(0, 200));
const state = result.data?.state;
const stateDetail = result.data?.state_detail;
// 检查任务状态 // 检查任务状态
if (result.data?.state === 1) { if (state === 1) {
// 任务完成 - PicWish API 返回 "file" 字段 // 任务完成 - PicWish API 返回 "file" 字段
const imageUrl = result.data?.file || result.data?.image; const imageUrl = result.data?.file || result.data?.image;
if (imageUrl) { if (imageUrl) {
return { success: true, imageUrl }; return { success: true, imageUrl };
} }
return { success: false, error: "处理完成但未返回图片URL" }; return { success: false, error: "处理完成但未返回图片URL" };
} else if (result.data?.state === -1 || result.data?.state < 0) { } else if (typeof state === "number" && state < 0) {
// 任务失败 // 任务失败
return { success: false, error: result.data?.error || "去水印处理失败" }; return { success: false, error: result.data?.error || "去水印处理失败" };
} else if (state === 4 || state === 0) {
// 明确处理中状态,继续轮询
console.log(`[WatermarkRemove] 任务处理中: state=${state}, detail=${stateDetail || "N/A"}`);
} }
// state === 0 表示处理中,继续轮询 // 其他状态默认继续轮询
await new Promise(resolve => setTimeout(resolve, pollInterval)); await new Promise(resolve => setTimeout(resolve, pollInterval));
} catch (error) { } catch (error) {
@ -75,7 +81,7 @@ async function pollForResult(taskId: string, apiKey: string): Promise<{ success:
} }
} }
return { success: false, error: "处理超时,请稍后重试" }; return { success: false, error: "去水印任务处理超时(服务端仍在处理中),请稍后重试" };
} }
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {