From 36c4dd7d5e7d3241f4e0f001f100964de4ba568a Mon Sep 17 00:00:00 2001 From: Iacami Gevaerd Date: Thu, 2 Nov 2023 13:05:32 -0300 Subject: [PATCH] twek base image path; handle errors --- app.js | 64 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/app.js b/app.js index 636ce2f..ac05e8b 100644 --- a/app.js +++ b/app.js @@ -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) => { @@ -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;