MCPcopy Create free account
hub / github.com/itwanger/toBeBetterJavaer / downloadImage

Function downloadImage

scripts/convert-mdnice-images-to-cdn.js:357–376  ·  view source on GitHub ↗
(url)

Source from the content-addressed store, hash-verified

355}
356
357async 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
378function normalizeContentType(contentType) {
379 if (!contentType) {

Callers 1

uploadAllFunction · 0.85

Calls 1

normalizeContentTypeFunction · 0.85

Tested by

no test coverage detected