Skip to content

Commit

Permalink
Improve image delivery endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Jul 16, 2024
1 parent e56f92e commit da9d8b5
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,29 @@ export async function launch(trie, libp2p) {
}

const size = 250;
const cfUrl = preview.cfTransform(data.safeAvatar, size);
const response = await fetch(cfUrl);
let url;
if (
data.safeAvatar.includes("imagedelivery.net") ||
data.safeAvatar.includes("imgur.com")
) {
url = data.safeAvatar;
} else {
url = preview.cfTransform(data.safeAvatar, size);
}
const response = await fetch(url);
if (!response.ok) {
return reply.status(404).type("text/plain").send("Not Found");
}

try {
const message = await response.clone().text();
if (message.startsWith("ERROR")) {
return reply.status(404).type("text/plain").send("Not Found");
}
} catch (err) {
return reply.status(500).type("text/plain").send("Internal Server Error");
}

reply.header("Content-Type", response.headers.get("Content-Type"));
reply.header(
"Cache-Control",
Expand Down

0 comments on commit da9d8b5

Please sign in to comment.