diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-12-17 18:46:44 +1100 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-12-17 18:46:44 +1100 |
commit | 3b5be3cc41e4f2befa9cc9621625684512ebe6f2 (patch) | |
tree | 1a5dde192b67e0e7859b00ae381d02648e27eb85 | |
parent | Remove bad banned words implementation (diff) | |
download | server-3b5be3cc41e4f2befa9cc9621625684512ebe6f2.tar.xz |
Remove `sharp` dep
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | src/api/util/utility/EmbedHandlers.ts | 7 | ||||
-rw-r--r-- | src/cdn/routes/external.ts | 103 |
3 files changed, 4 insertions, 107 deletions
diff --git a/package.json b/package.json index e54cafe3..d9feb22c 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,6 @@ "picocolors": "^1.0.0", "probe-image-size": "^7.2.3", "proxy-agent": "^5.0.0", - "sharp": "^0.31.0", "sqlite3": "^5.1.1", "typeorm": "^0.3.10", "typescript-json-schema": "^0.50.1", diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index 374d88fd..a8738a4a 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -17,7 +17,7 @@ export const DEFAULT_FETCH_OPTIONS: any = { }; export const getProxyUrl = (url: URL, width: number, height: number): string => { - const { endpointPublic, resizeWidthMax, resizeHeightMax, imagorServerUrl } = Config.get().cdn; + const { resizeWidthMax, resizeHeightMax, imagorServerUrl } = Config.get().cdn; const secret = Config.get().security.jwtSecret; // maybe shouldn't use this? width = Math.min(width || 500, resizeWidthMax || width); height = Math.min(height || 500, resizeHeightMax || width); @@ -34,8 +34,9 @@ export const getProxyUrl = (url: URL, width: number, height: number): string => return `${imagorServerUrl}/${hash}/${path}`; } - // Fosscord CDN resizer - return `${endpointPublic}/external/resize/${encodeURIComponent(url.href)}?width=${width}&height=${height}`; + // TODO: Imagor documentation + console.log("Imagor has not been set up correctly. docs.fosscord.com/set/up/a/page/about/this"); + return ""; }; const getMeta = ($: cheerio.CheerioAPI, name: string): string | undefined => { diff --git a/src/cdn/routes/external.ts b/src/cdn/routes/external.ts deleted file mode 100644 index 08e6b164..00000000 --- a/src/cdn/routes/external.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { Router, Response, Request } from "express"; -import fetch from "node-fetch"; -import { HTTPError } from "lambert-server"; -import { Snowflake } from "@fosscord/util"; -import { storage } from "../util/Storage"; -import FileType from "file-type"; -import { Config } from "@fosscord/util"; -import sharp from "sharp"; - -// TODO: somehow handle the deletion of images posted to the /external route - -const router = Router(); -const DEFAULT_FETCH_OPTIONS: any = { - redirect: "follow", - follow: 1, - headers: { - "user-agent": - "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)", - }, - size: 1024 * 1024 * 8, - compress: true, - method: "GET", -}; - -router.post("/", async (req: Request, res: Response) => { - if (req.headers.signature !== Config.get().security.requestSignature) - throw new HTTPError("Invalid request signature"); - - if (!req.body) throw new HTTPError("Invalid Body"); - - const { url } = req.body; - if (!url || typeof url !== "string") throw new HTTPError("Invalid url"); - - const id = Snowflake.generate(); - - try { - const response = await fetch(url, DEFAULT_FETCH_OPTIONS); - const buffer = await response.buffer(); - - await storage.set(`/external/${id}`, buffer); - - res.send({ id }); - } catch (error) { - throw new HTTPError("Couldn't fetch website"); - } -}); - -router.get("/:id", async (req: Request, res: Response) => { - const { id } = req.params; - - const file = await storage.get(`/external/${id}`); - if (!file) throw new HTTPError("File not found"); - const result = await FileType.fromBuffer(file); - - res.set("Content-Type", result?.mime); - - return res.send(file); -}); - -// this method is gross lol don't care -// It's also no longer actually used on Slowcord's official server. -// We actually use imagor now -router.get("/resize/:url", async (req: Request, res: Response) => { - const url = decodeURIComponent(req.params.url); - const { width, height } = req.query; - if (!width || !height) throw new HTTPError("Must provide width and height"); - - const { resizeHeightMax, resizeWidthMax } = Config.get().cdn; - const w = Math.min(parseInt(width as string), resizeWidthMax ?? 100); - const h = Math.min(parseInt(height as string), resizeHeightMax ?? 100); - if (w < 1 || h < 1) - throw new HTTPError("Width and height must be greater than 0"); - - let buffer, response; - try { - response = await fetch(url, DEFAULT_FETCH_OPTIONS); - if (response.status !== 200) throw "e"; // lol super gross - buffer = await response.buffer(); - } catch (e) { - throw new HTTPError("Couldn't fetch website"); - } - - if (buffer.length == 0) throw new HTTPError("Website response was empty."); - - if (response.headers.get("content-type")?.indexOf("image") === -1) { - throw new HTTPError("Content type is not image."); - } - - const resizedBuffer = await sharp(buffer) - .resize(parseInt(width as string), parseInt(height as string), { - fit: "inside", - }) - .toBuffer(); - - res.setHeader("Content-Disposition", "attachment"); - res.setHeader( - "Content-Type", - response.headers.get("content-type") ?? "image/png", - ); - return res.end(resizedBuffer); -}); - -export default router; |