Run Prettier (tabs -> spaces???)

This commit is contained in:
TomatoCake 2024-06-22 20:43:54 +02:00
parent 37bd06e142
commit e90f8e88c0

View File

@ -58,7 +58,8 @@ export async function ImageProxy(req: Request, res: Response) {
.replace(/\//g, "_"); .replace(/\//g, "_");
try { try {
if (!crypto.timingSafeEqual(Buffer.from(hash), Buffer.from(path[0]))) throw new Error("Invalid signature"); if (!crypto.timingSafeEqual(Buffer.from(hash), Buffer.from(path[0])))
throw new Error("Invalid signature");
} catch { } catch {
console.log("Invalid signature, expected " + hash + " got " + path[0]); console.log("Invalid signature, expected " + hash + " got " + path[0]);
res.status(403).send("Invalid signature"); res.status(403).send("Invalid signature");
@ -80,18 +81,30 @@ export async function ImageProxy(req: Request, res: Response) {
if (!request) return; if (!request) return;
if (request.status !== 200) { if (request.status !== 200) {
res.status(request.status).send("Origin failed to respond: " + request.status + " " + request.statusText); res.status(request.status).send(
"Origin failed to respond: " +
request.status +
" " +
request.statusText,
);
return; return;
} }
if (!request.headers.get("Content-Type") || !request.headers.get("Content-Length")) { if (
res.status(500).send("Origin did not provide a Content-Type or Content-Length header"); !request.headers.get("Content-Type") ||
!request.headers.get("Content-Length")
) {
res.status(500).send(
"Origin did not provide a Content-Type or Content-Length header",
);
return; return;
} }
// @ts-expect-error TS doesn't believe that the header cannot be null (it's checked for falsiness above) // @ts-expect-error TS doesn't believe that the header cannot be null (it's checked for falsiness above)
if (parseInt(request.headers.get("Content-Length")) > 1024 * 1024 * 10) { if (parseInt(request.headers.get("Content-Length")) > 1024 * 1024 * 10) {
res.status(500).send("Origin provided a Content-Length header that is too large"); res.status(500).send(
"Origin provided a Content-Length header that is too large",
);
return; return;
} }
@ -115,7 +128,11 @@ export async function ImageProxy(req: Request, res: Response) {
Jimp = await import("jimp"); Jimp = await import("jimp");
} catch { } catch {
Jimp = false; Jimp = false;
console.log(`[ImageProxy] ${yellow("Neither \"sharp\" or \"jimp\" NPM packages are installed, image resizing will be disabled")}`); console.log(
`[ImageProxy] ${yellow(
'Neither "sharp" or "jimp" NPM packages are installed, image resizing will be disabled',
)}`,
);
} }
} }
@ -123,7 +140,8 @@ export async function ImageProxy(req: Request, res: Response) {
const buffer = Buffer.from(arrayBuffer); const buffer = Buffer.from(arrayBuffer);
if (sharp && sharpSupported.has(contentType)) { if (sharp && sharpSupported.has(contentType)) {
resultBuffer = await sharp.default(buffer) resultBuffer = await sharp
.default(buffer)
// Sharp doesn't support "scaleToFit" // Sharp doesn't support "scaleToFit"
.resize(width) .resize(width)
.toBuffer(); .toBuffer();
@ -131,13 +149,15 @@ export async function ImageProxy(req: Request, res: Response) {
resultBuffer = await Jimp.read(buffer).then((image) => { resultBuffer = await Jimp.read(buffer).then((image) => {
contentType = image.getMIME(); contentType = image.getMIME();
// @ts-expect-error Jimp is defined at this point // @ts-expect-error Jimp is defined at this point
return image.scaleToFit(width, height).getBufferAsync(Jimp.AUTO); return image
.scaleToFit(width, height)
.getBufferAsync(Jimp.AUTO);
}); });
} }
} }
res.header("Content-Type", contentType); res.header("Content-Type", contentType);
res.setHeader("Cache-Control", "public, max-age=" + (1000 * 60 * 60 * 24)); res.setHeader("Cache-Control", "public, max-age=" + 1000 * 60 * 60 * 24);
res.send(resultBuffer); res.send(resultBuffer);
} }