fix: timeout remote image downloads

This commit is contained in:
134 2026-07-04 12:02:29 +08:00
parent d8e2d65186
commit ae176cba44

View File

@ -63,6 +63,7 @@ const AOS_MAX_RESOLUTION = 4096;
const AOS_CREATE_TIMEOUT_MS = 120_000; const AOS_CREATE_TIMEOUT_MS = 120_000;
const AOS_POLL_TIMEOUT_MS = 60_000; const AOS_POLL_TIMEOUT_MS = 60_000;
const AOS_DOWNLOAD_TIMEOUT_MS = 60_000; const AOS_DOWNLOAD_TIMEOUT_MS = 60_000;
const IMAGE_URL_FETCH_TIMEOUT_MS = 60_000;
async function createThumbnail(buffer: Buffer, mimeType: string): Promise<Buffer> { async function createThumbnail(buffer: Buffer, mimeType: string): Promise<Buffer> {
try { try {
@ -499,14 +500,16 @@ function getModelId(provider: RmbgProvider) {
export async function fetchImageBufferFromUrl(imageUrl: string): Promise<Buffer> { export async function fetchImageBufferFromUrl(imageUrl: string): Promise<Buffer> {
try { try {
const imageResponse = await fetch(imageUrl); return await withTimeout(async (signal) => {
if (!imageResponse.ok) { const imageResponse = await fetch(imageUrl, { signal });
throw new RmbgServiceError( if (!imageResponse.ok) {
`无法获取图片: ${imageResponse.status} ${imageResponse.statusText}`, throw new RmbgServiceError(
400 `无法获取图片: ${imageResponse.status} ${imageResponse.statusText}`,
); 400
} );
return Buffer.from(await imageResponse.arrayBuffer()); }
return Buffer.from(await imageResponse.arrayBuffer());
}, IMAGE_URL_FETCH_TIMEOUT_MS);
} catch (error) { } catch (error) {
if (error instanceof RmbgServiceError) { if (error instanceof RmbgServiceError) {
throw error; throw error;