diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-12 21:09:29 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-12 21:09:29 +0200 |
commit | d50073fc41fc7d69d3dab9a2858158320983329b (patch) | |
tree | c4507ebe83019292807f5a4cf10fc8a522334e2a /cdn | |
parent | Merge pull request #348 from AlTech98/fix-cdn-upload (diff) | |
download | server-d50073fc41fc7d69d3dab9a2858158320983329b.tar.xz |
:art: remove long relatives paths -> short module paths
Diffstat (limited to 'cdn')
-rw-r--r-- | cdn/package.json | 2 | ||||
-rw-r--r-- | cdn/scripts/tsconfig-paths-bootstrap.js | 10 | ||||
-rw-r--r-- | cdn/src/routes/attachments.ts | 120 | ||||
-rw-r--r-- | cdn/src/routes/avatars.ts | 70 | ||||
-rw-r--r-- | cdn/src/routes/external.ts | 5 | ||||
-rw-r--r-- | cdn/tsconfig.json | 7 |
6 files changed, 129 insertions, 85 deletions
diff --git a/cdn/package.json b/cdn/package.json index c2bdbd79..7c59381b 100644 --- a/cdn/package.json +++ b/cdn/package.json @@ -7,7 +7,7 @@ "scripts": { "test": "npm run build && jest --coverage ./tests", "build": "npx tsc -b .", - "start": "npm run build && node dist/start.js" + "start": "npm run build && node -r ./scripts/tsconfig-paths-bootstrap.js dist/start.js" }, "repository": { "type": "git", diff --git a/cdn/scripts/tsconfig-paths-bootstrap.js b/cdn/scripts/tsconfig-paths-bootstrap.js new file mode 100644 index 00000000..50602b02 --- /dev/null +++ b/cdn/scripts/tsconfig-paths-bootstrap.js @@ -0,0 +1,10 @@ +const tsConfigPaths = require("tsconfig-paths"); +const path = require("path"); + +const cleanup = tsConfigPaths.register({ + baseUrl: path.join(__dirname, ".."), + paths: { + "@fosscord/cdn": ["dist/index.js"], + "@fosscord/cdn/*": ["dist/*"], + }, +}); diff --git a/cdn/src/routes/attachments.ts b/cdn/src/routes/attachments.ts index 59845c94..7c55998b 100644 --- a/cdn/src/routes/attachments.ts +++ b/cdn/src/routes/attachments.ts @@ -1,73 +1,87 @@ import { Router, Response, Request } from "express"; import { Config, Snowflake } from "@fosscord/util"; -import { storage } from "../util/Storage"; +import { storage } from "@fosscord/cdn/util/Storage"; import FileType from "file-type"; import { HTTPError } from "lambert-server"; -import { multer } from "../util/multer"; +import { multer } from "@fosscord/cdn/util/multer"; import imageSize from "image-size"; const router = Router(); -router.post("/:channel_id", multer.single("file"), async (req: Request, res: Response) => { - if (req.headers.signature !== Config.get().security.requestSignature) - throw new HTTPError("Invalid request signature"); - if (!req.file) throw new HTTPError("file missing"); - - const { buffer, mimetype, size, originalname, fieldname } = req.file; - const { channel_id } = req.params; - const filename = originalname.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, ""); - const id = Snowflake.generate(); - const path = `attachments/${channel_id}/${id}/${filename}`; - - const endpoint = Config.get()?.cdn.endpoint || "http://localhost:3003"; - - await storage.set(path, buffer); - var width; - var height; - if (mimetype.includes("image")) { - const dimensions = imageSize(buffer); - if (dimensions) { - width = dimensions.width; - height = dimensions.height; +router.post( + "/:channel_id", + multer.single("file"), + async (req: Request, res: Response) => { + if (req.headers.signature !== Config.get().security.requestSignature) + throw new HTTPError("Invalid request signature"); + if (!req.file) throw new HTTPError("file missing"); + + const { buffer, mimetype, size, originalname, fieldname } = req.file; + const { channel_id } = req.params; + const filename = originalname + .replaceAll(" ", "_") + .replace(/[^a-zA-Z0-9._]+/g, ""); + const id = Snowflake.generate(); + const path = `attachments/${channel_id}/${id}/${filename}`; + + const endpoint = Config.get()?.cdn.endpoint || "http://localhost:3003"; + + await storage.set(path, buffer); + var width; + var height; + if (mimetype.includes("image")) { + const dimensions = imageSize(buffer); + if (dimensions) { + width = dimensions.width; + height = dimensions.height; + } } - } - - const file = { - id, - content_type: mimetype, - filename: filename, - size, - url: `${endpoint}/${path}`, - width, - height, - }; - return res.json(file); -}); + const file = { + id, + content_type: mimetype, + filename: filename, + size, + url: `${endpoint}/${path}`, + width, + height, + }; + + return res.json(file); + } +); -router.get("/:channel_id/:id/:filename", async (req: Request, res: Response) => { - const { channel_id, id, filename } = req.params; +router.get( + "/:channel_id/:id/:filename", + async (req: Request, res: Response) => { + const { channel_id, id, filename } = req.params; - const file = await storage.get(`attachments/${channel_id}/${id}/${filename}`); - if (!file) throw new HTTPError("File not found"); - const type = await FileType.fromBuffer(file); + const file = await storage.get( + `attachments/${channel_id}/${id}/${filename}` + ); + if (!file) throw new HTTPError("File not found"); + const type = await FileType.fromBuffer(file); - res.set("Content-Type", type?.mime); - res.set("Cache-Control", "public, max-age=31536000"); + res.set("Content-Type", type?.mime); + res.set("Cache-Control", "public, max-age=31536000"); - return res.send(file); -}); + return res.send(file); + } +); -router.delete("/:channel_id/:id/:filename", async (req: Request, res: Response) => { - if (req.headers.signature !== Config.get().security.requestSignature) - throw new HTTPError("Invalid request signature"); +router.delete( + "/:channel_id/:id/:filename", + async (req: Request, res: Response) => { + if (req.headers.signature !== Config.get().security.requestSignature) + throw new HTTPError("Invalid request signature"); - const { channel_id, id, filename } = req.params; - const path = `attachments/${channel_id}/${id}/${filename}`; + const { channel_id, id, filename } = req.params; + const path = `attachments/${channel_id}/${id}/${filename}`; - await storage.delete(path); + await storage.delete(path); - return res.send({ success: true }); -}); + return res.send({ success: true }); + } +); export default router; diff --git a/cdn/src/routes/avatars.ts b/cdn/src/routes/avatars.ts index 03388afc..3d745f90 100644 --- a/cdn/src/routes/avatars.ts +++ b/cdn/src/routes/avatars.ts @@ -1,9 +1,9 @@ import { Router, Response, Request } from "express"; import { Config, Snowflake } from "@fosscord/util"; -import { storage } from "../util/Storage"; +import { storage } from "@fosscord/cdn/util/Storage"; import FileType from "file-type"; import { HTTPError } from "lambert-server"; -import { multer } from "../util/multer"; +import { multer } from "@fosscord/cdn/util/multer"; import crypto from "crypto"; // TODO: check premium and animated pfp are allowed in the config @@ -12,36 +12,50 @@ import crypto from "crypto"; // TODO: delete old icons const ANIMATED_MIME_TYPES = ["image/apng", "image/gif", "image/gifv"]; -const STATIC_MIME_TYPES = ["image/png", "image/jpeg", "image/webp", "image/svg+xml", "image/svg"]; +const STATIC_MIME_TYPES = [ + "image/png", + "image/jpeg", + "image/webp", + "image/svg+xml", + "image/svg", +]; const ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES]; const router = Router(); -router.post("/:user_id", multer.single("file"), async (req: Request, res: Response) => { - if (req.headers.signature !== Config.get().security.requestSignature) - throw new HTTPError("Invalid request signature"); - if (!req.file) throw new HTTPError("Missing file"); - const { buffer, mimetype, size, originalname, fieldname } = req.file; - const { user_id } = req.params; - - var hash = crypto.createHash("md5").update(Snowflake.generate()).digest("hex"); - - const type = await FileType.fromBuffer(buffer); - if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type"); - if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash - - const path = `avatars/${user_id}/${hash}`; - const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; - - await storage.set(path, buffer); - - return res.json({ - id: hash, - content_type: type.mime, - size, - url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`, - }); -}); +router.post( + "/:user_id", + multer.single("file"), + async (req: Request, res: Response) => { + if (req.headers.signature !== Config.get().security.requestSignature) + throw new HTTPError("Invalid request signature"); + if (!req.file) throw new HTTPError("Missing file"); + const { buffer, mimetype, size, originalname, fieldname } = req.file; + const { user_id } = req.params; + + var hash = crypto + .createHash("md5") + .update(Snowflake.generate()) + .digest("hex"); + + const type = await FileType.fromBuffer(buffer); + if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) + throw new HTTPError("Invalid file type"); + if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash + + const path = `avatars/${user_id}/${hash}`; + const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; + + await storage.set(path, buffer); + + return res.json({ + id: hash, + content_type: type.mime, + size, + url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`, + }); + } +); router.get("/:user_id/:hash", async (req: Request, res: Response) => { var { user_id, hash } = req.params; diff --git a/cdn/src/routes/external.ts b/cdn/src/routes/external.ts index 10bb0f7d..e7783ec3 100644 --- a/cdn/src/routes/external.ts +++ b/cdn/src/routes/external.ts @@ -2,7 +2,7 @@ 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 { storage } from "@fosscord/cdn/util/Storage"; import FileType from "file-type"; import { Config } from "@fosscord/util"; @@ -13,7 +13,8 @@ const DEFAULT_FETCH_OPTIONS: any = { redirect: "follow", follow: 1, headers: { - "user-agent": "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)", + "user-agent": + "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)", }, size: 1024 * 1024 * 8, compress: true, diff --git a/cdn/tsconfig.json b/cdn/tsconfig.json index 08e39435..2daac1ad 100644 --- a/cdn/tsconfig.json +++ b/cdn/tsconfig.json @@ -65,6 +65,11 @@ /* Advanced Options */ "skipLibCheck": true /* Skip type checking of declaration files. */, - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, + "baseUrl": ".", + "paths": { + "@fosscord/cdn/": ["src/index.ts"], + "@fosscord/cdn/*": ["src/*"] + } } } |