🚧 WIP rewrite

This commit is contained in:
Flam3rboy
2021-05-27 18:54:15 +02:00
parent fdaa277935
commit 8f66556f5a
13 changed files with 609 additions and 306 deletions
+4 -9
View File
@@ -1,6 +1,7 @@
import { Router } from "express";
import multer from "multer";
import Snowflake from "../Snowflake";
import { Snowflake } from "@fosscord/server-util";
import { storage } from "../util/Storage";
const multer_ = multer();
const router = Router();
@@ -11,11 +12,11 @@ type Attachment = {
id: string;
type: string;
};
router.post("/:filename", multer_.single("attachment"), async (req, res) => {
const { buffer, mimetype } = req.file;
const { filename } = req.params;
const { db } = req.cdn;
// storage.set(filename, );
const File: Attachment = {
filename,
@@ -23,14 +24,9 @@ router.post("/:filename", multer_.single("attachment"), async (req, res) => {
id: Snowflake.generate(),
type: mimetype,
};
if (!(await db.data.attachments.push(File))) throw new Error("Error uploading file");
return res.status(201).send({ success: true, message: "attachment uploaded", id: File.id, filename });
});
router.get("/:hash/:filename", async (req, res) => {
const { db } = req.cdn;
const { hash, filename } = req.params;
const File: Attachment = await db.data.attachments({ id: hash, filename: filename }).get();
@@ -41,7 +37,6 @@ router.get("/:hash/:filename", async (req, res) => {
router.delete("/:hash/:filename", async (req, res) => {
const { hash, filename } = req.params;
const { db } = req.cdn;
await db.data.attachments({ id: hash, filename: filename }).delete();
return res.send({ success: true, message: "attachment deleted" });
+8 -14
View File
@@ -2,8 +2,7 @@ import bodyParser from "body-parser";
import { Router } from "express";
import fetch from "node-fetch";
import cheerio from "cheerio";
import btoa from "btoa";
import { URL } from "url";
import crypto from "crypto";
const router = Router();
@@ -30,25 +29,21 @@ const DEFAULT_FETCH_OPTIONS: any = {
router.post("/", bodyParser.json(), async (req, res) => {
if (!req.body) throw new Error("Invalid Body (url missing) \nExample: url:https://discord.com");
const { db } = req.cdn;
const { url } = req.body;
const ID = btoa(url);
const cache = await db.data.crawler({ id: ID }).get();
if (cache) return res.send(cache);
const hash = crypto.createHash("md5").update(url).digest("hex");
try {
const request = await fetch(url, DEFAULT_FETCH_OPTIONS);
const text = await request.text();
const ツ: any = cheerio.load(text);
const $ = cheerio.load(text);
const ogTitle = ツ('meta[property="og:title"]').attr("content");
const ogDescription = ツ('meta[property="og:description"]').attr("content");
const ogImage = ツ('meta[property="og:image"]').attr("content");
const ogUrl = ツ('meta[property="og:url"]').attr("content");
const ogType = ツ('meta[property="og:type"]').attr("content");
const ogTitle = $('meta[property="og:title"]').attr("content");
const ogDescription = $('meta[property="og:description"]').attr("content");
const ogImage = $('meta[property="og:image"]').attr("content");
const ogUrl = $('meta[property="og:url"]').attr("content");
const ogType = $('meta[property="og:type"]').attr("content");
const filename = new URL(url).host.split(".")[0];
@@ -72,7 +67,6 @@ router.post("/", bodyParser.json(), async (req, res) => {
});
router.get("/:id/:filename", async (req, res) => {
const { db } = req.cdn;
const { id, filename } = req.params;
const { image, type } = await db.data.externals({ id: id }).get();
const imageBuffer = Buffer.from(image, "base64");