(url)
| 355 | } |
| 356 | |
| 357 | async function downloadImage(url) { |
| 358 | const response = await fetch(url, { |
| 359 | redirect: "follow", |
| 360 | headers: { |
| 361 | "User-Agent": "toBeBetterJavaer-image-cdn/1.0", |
| 362 | }, |
| 363 | }); |
| 364 | if (!response.ok) { |
| 365 | throw new Error(`Download failed with HTTP ${response.status}`); |
| 366 | } |
| 367 | const contentType = normalizeContentType(response.headers.get("content-type")); |
| 368 | if (contentType && !contentType.startsWith("image/") && contentType !== "application/octet-stream") { |
| 369 | throw new Error(`Unexpected content type: ${contentType}`); |
| 370 | } |
| 371 | const body = Buffer.from(await response.arrayBuffer()); |
| 372 | if (body.length === 0) { |
| 373 | throw new Error("Downloaded file is empty"); |
| 374 | } |
| 375 | return { body, contentType }; |
| 376 | } |
| 377 | |
| 378 | function normalizeContentType(contentType) { |
| 379 | if (!contentType) { |
no test coverage detected