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_POLL_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> {
try {
@ -499,14 +500,16 @@ function getModelId(provider: RmbgProvider) {
export async function fetchImageBufferFromUrl(imageUrl: string): Promise<Buffer> {
try {
const imageResponse = await fetch(imageUrl);
if (!imageResponse.ok) {
throw new RmbgServiceError(
`无法获取图片: ${imageResponse.status} ${imageResponse.statusText}`,
400
);
}
return Buffer.from(await imageResponse.arrayBuffer());
return await withTimeout(async (signal) => {
const imageResponse = await fetch(imageUrl, { signal });
if (!imageResponse.ok) {
throw new RmbgServiceError(
`无法获取图片: ${imageResponse.status} ${imageResponse.statusText}`,
400
);
}
return Buffer.from(await imageResponse.arrayBuffer());
}, IMAGE_URL_FETCH_TIMEOUT_MS);
} catch (error) {
if (error instanceof RmbgServiceError) {
throw error;