Run Prettier (tabs -> spaces???)

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

View File

@ -1,19 +1,19 @@
/* /*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors Copyright (C) 2023 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Config } from "@spacebar/util"; import { Config } from "@spacebar/util";
@ -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);
} }