Skip to content

Commit 36c4dd7

Browse files
committed
twek base image path; handle errors
1 parent 47fe23d commit 36c4dd7

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

app.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const sharp = require("sharp");
66
const app = express();
77
const PORT = 8080;
88
const HOST = "0.0.0.0";
9-
const BASE_STORAGE_IMAGE_URL = "https://storage.googleapis.com/openbeta-prod/u";
9+
const BASE_STORAGE_IMAGE_URL = "https://storage.googleapis.com";
1010

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

24-
if (!/\.(jpe?g|png|gif|webp)$/i.test(pathname)) {
25-
return res.status(400).send("Disallowed file extension");
20+
app.get("*", async (req, res) => {
21+
try {
22+
const { searchParams, pathname, href } = new URL(
23+
`${BASE_STORAGE_IMAGE_URL}${req.url}`,
24+
);
25+
26+
if (!/\.(jpe?g|png|gif|webp)$/i.test(pathname)) {
27+
return res.status(400).send("Disallowed file extension");
28+
}
29+
30+
const webp = req.headers.accept?.includes("image/webp");
31+
const avif = req.headers.accept?.includes("image/avif");
32+
const quality = Number(searchParams.get("q")) || 90;
33+
const width = Number(searchParams.get("w")) || undefined;
34+
const height = Number(searchParams.get("h")) || undefined;
35+
const format = getFormat(webp, avif);
36+
37+
const i = await getImage(href);
38+
const processedImage = await sharp(i)
39+
.rotate()
40+
.resize({ width, height })
41+
.toFormat(format, { quality });
42+
43+
console.log(pathname, href);
44+
45+
return res
46+
.set("Cache-Control", "public, max-age=15552000")
47+
.set("Vary", "Accept")
48+
.type(`image/${format}`)
49+
.send(await processedImage.toBuffer());
50+
} catch (e) {
51+
res.status(500).send(JSON.stringify(e));
2652
}
27-
28-
const webp = req.headers.accept?.includes("image/webp");
29-
const avif = req.headers.accept?.includes("image/avif");
30-
const quality = Number(searchParams.get("q")) || 90;
31-
const width = Number(searchParams.get("w")) || undefined;
32-
const height = Number(searchParams.get("h")) || undefined;
33-
const format = getFormat(webp, avif);
34-
35-
console.log("webp:", webp);
36-
console.log("avif:", avif);
37-
console.log("quality:", quality);
38-
console.log("width:", width);
39-
console.log("height:", height);
40-
41-
const i = await getImage(`${BASE_STORAGE_IMAGE_URL}${pathname}`);
42-
const processedImage = await sharp(i)
43-
.rotate()
44-
.resize({ width, height })
45-
.toFormat(format, { quality });
46-
return res
47-
.set("Cache-Control", "public, max-age=15552000")
48-
.set("Vary", "Accept")
49-
.type(`image/${format}`)
50-
.send(await processedImage.toBuffer());
5153
});
5254

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

0 commit comments

Comments
 (0)