Skip to content

Commit

Permalink
twek base image path; handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
enapupe committed Nov 2, 2023
1 parent 47fe23d commit 36c4dd7
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const sharp = require("sharp");
const app = express();
const PORT = 8080;
const HOST = "0.0.0.0";
const BASE_STORAGE_IMAGE_URL = "https://storage.googleapis.com/openbeta-prod/u";
const BASE_STORAGE_IMAGE_URL = "https://storage.googleapis.com";

const getImage = (path) => fetch(path).then((res) => res.arrayBuffer());
const getFormat = (webp, avif) => {
Expand All @@ -16,38 +16,40 @@ const getFormat = (webp, avif) => {
app.get("/healthy", (req, res) => {
res.send("yes");
});
app.get("*", async (req, res) => {
console.log(req.url);
const { searchParams, pathname } = new URL(`http://noop.com${req.url}`);
console.log(searchParams, pathname);

if (!/\.(jpe?g|png|gif|webp)$/i.test(pathname)) {
return res.status(400).send("Disallowed file extension");
app.get("*", async (req, res) => {
try {
const { searchParams, pathname, href } = new URL(
`${BASE_STORAGE_IMAGE_URL}${req.url}`,
);

if (!/\.(jpe?g|png|gif|webp)$/i.test(pathname)) {
return res.status(400).send("Disallowed file extension");
}

const webp = req.headers.accept?.includes("image/webp");
const avif = req.headers.accept?.includes("image/avif");
const quality = Number(searchParams.get("q")) || 90;
const width = Number(searchParams.get("w")) || undefined;
const height = Number(searchParams.get("h")) || undefined;
const format = getFormat(webp, avif);

const i = await getImage(href);
const processedImage = await sharp(i)
.rotate()
.resize({ width, height })
.toFormat(format, { quality });

console.log(pathname, href);

return res
.set("Cache-Control", "public, max-age=15552000")
.set("Vary", "Accept")
.type(`image/${format}`)
.send(await processedImage.toBuffer());
} catch (e) {
res.status(500).send(JSON.stringify(e));
}

const webp = req.headers.accept?.includes("image/webp");
const avif = req.headers.accept?.includes("image/avif");
const quality = Number(searchParams.get("q")) || 90;
const width = Number(searchParams.get("w")) || undefined;
const height = Number(searchParams.get("h")) || undefined;
const format = getFormat(webp, avif);

console.log("webp:", webp);
console.log("avif:", avif);
console.log("quality:", quality);
console.log("width:", width);
console.log("height:", height);

const i = await getImage(`${BASE_STORAGE_IMAGE_URL}${pathname}`);
const processedImage = await sharp(i)
.rotate()
.resize({ width, height })
.toFormat(format, { quality });
return res
.set("Cache-Control", "public, max-age=15552000")
.set("Vary", "Accept")
.type(`image/${format}`)
.send(await processedImage.toBuffer());
});

const port = parseInt(process.env.PORT) || PORT;
Expand Down

0 comments on commit 36c4dd7

Please sign in to comment.