1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

fixed missing imports and programming issues [cdn]

This commit is contained in:
xnacly 2021-08-29 17:29:05 +02:00
parent aa340d0a3b
commit 498475c501

View File

@ -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";
// TODO: somehow handle the deletion of images posted to the /external route
const router = Router();
type crawled = {
id: string;
ogTitle: string;
ogType: string;
ogDescription: string;
ogUrl: string;
cachedImage: string;
};
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}`);