summary refs log tree commit diff
path: root/cdn/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-30 12:15:07 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-30 12:15:07 +0200
commit1eecd409af29dd1ec730f0a09821250075c07160 (patch)
tree9972e8b7ef5db4b632a8a2c56a60b8cb6f8a8405 /cdn/src
parent:construction: typeorm (diff)
parentadded unittests for filestorage [cdn] (diff)
downloadserver-1eecd409af29dd1ec730f0a09821250075c07160.tar.xz
Merge branch 'typeorm' of https://github.com/fosscord/fosscord-api into typeorm
Diffstat (limited to 'cdn/src')
-rw-r--r--cdn/src/routes/external.ts27
-rw-r--r--cdn/src/util/FileStorage.ts2
2 files changed, 12 insertions, 17 deletions
diff --git a/cdn/src/routes/external.ts b/cdn/src/routes/external.ts

index 625b6bbd..10bb0f7d 100644 --- a/cdn/src/routes/external.ts +++ b/cdn/src/routes/external.ts
@@ -1,45 +1,38 @@ -// @ts-nocheck -import bodyParser from "body-parser"; import { Router, Response, Request } from "express"; import fetch from "node-fetch"; -import crypto from "crypto"; 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"; -const router = Router(); - -type crawled = { - id: string; - ogTitle: string; - ogType: string; - ogDescription: string; - ogUrl: string; - cachedImage: string; -}; +// 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; Discordbot/2.0; +https://discordapp.com)", + "user-agent": "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)", }, size: 1024 * 1024 * 8, compress: true, method: "GET", }; -router.post("/", bodyParser.json(), async (req: Request, res: Response) => { +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(ogImage, DEFAULT_FETCH_OPTIONS); + const response = await fetch(url, DEFAULT_FETCH_OPTIONS); const buffer = await response.buffer(); await storage.set(`/external/${id}`, buffer); @@ -50,7 +43,7 @@ router.post("/", bodyParser.json(), async (req: Request, res: Response) => { } }); -router.get("/:id/", async (req: Request, res: Response) => { +router.get("/:id", async (req: Request, res: Response) => { const { id } = req.params; const file = await storage.get(`/external/${id}`); diff --git a/cdn/src/util/FileStorage.ts b/cdn/src/util/FileStorage.ts
index 6e74788f..fae6eb1a 100644 --- a/cdn/src/util/FileStorage.ts +++ b/cdn/src/util/FileStorage.ts
@@ -6,6 +6,8 @@ import "missing-native-js-functions"; import { Readable } from "stream"; import ExifTransformer = require("exif-be-gone"); +// TODO: split stored files into separate folders named after cloned route + function getPath(path: string) { // STORAGE_LOCATION has a default value in start.ts const root = process.env.STORAGE_LOCATION || "../";