mirror of
https://github.com/spacebarchat/server.git
synced 2024-09-22 02:31:36 +02:00
✨ external assets
This commit is contained in:
parent
614d8f14fa
commit
21e659fce5
@ -2,8 +2,10 @@
|
|||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import cheerio from "cheerio";
|
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
import { HTTPError } from "lambert-server";
|
||||||
|
import { Snowflake } from "@fosscord/server-util";
|
||||||
|
import { storage } from "../util/Storage";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@ -28,52 +30,34 @@ const DEFAULT_FETCH_OPTIONS: any = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
router.post("/", bodyParser.json(), async (req, res) => {
|
router.post("/", bodyParser.json(), async (req, res) => {
|
||||||
if (!req.body) throw new Error("Invalid Body (url missing) \nExample: url:https://discord.com");
|
if (!req.body) throw new HTTPError("Invalid Body");
|
||||||
|
|
||||||
const { url } = req.body;
|
const { url } = req.body;
|
||||||
|
if (!url || typeof url !== "string") throw new HTTPError("Invalid url");
|
||||||
|
|
||||||
const hash = crypto.createHash("md5").update(url).digest("hex");
|
const id = Snowflake.generate();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const request = await fetch(url, DEFAULT_FETCH_OPTIONS);
|
const response = await fetch(ogImage, DEFAULT_FETCH_OPTIONS);
|
||||||
|
const buffer = await response.buffer();
|
||||||
|
|
||||||
const text = await request.text();
|
await storage.set(`/external/${id}`, buffer);
|
||||||
const $ = cheerio.load(text);
|
|
||||||
|
|
||||||
const ogTitle = $('meta[property="og:title"]').attr("content");
|
res.send({ id });
|
||||||
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];
|
|
||||||
|
|
||||||
const ImageResponse = await fetch(ogImage, DEFAULT_FETCH_OPTIONS);
|
|
||||||
const ImageType = ImageResponse.headers.get("content-type");
|
|
||||||
const ImageExtension = ImageType?.split("/")[1];
|
|
||||||
const ImageResponseBuffer = (await ImageResponse.buffer()).toString("base64");
|
|
||||||
const cachedImage = `/external/${ID}/${filename}.${ImageExtension}`;
|
|
||||||
|
|
||||||
await db.data.externals.push({ image: ImageResponseBuffer, id: ID, type: ImageType });
|
|
||||||
|
|
||||||
const new_cache_entry: crawled = { id: ID, ogTitle, ogDescription, cachedImage, ogUrl, ogType };
|
|
||||||
await db.data.crawler.push(new_cache_entry);
|
|
||||||
|
|
||||||
res.send(new_cache_entry);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
throw new HTTPError("Couldn't fetch website");
|
||||||
|
|
||||||
throw new Error("Couldn't fetch website");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/:id/:filename", async (req, res) => {
|
router.get("/:id/", async (req, res) => {
|
||||||
const { id, filename } = req.params;
|
const { id } = req.params;
|
||||||
const { image, type } = await db.data.externals({ id: id }).get();
|
|
||||||
const imageBuffer = Buffer.from(image, "base64");
|
|
||||||
|
|
||||||
res.set("Content-Type", type);
|
const file = await storage.get(`/external/${id}`);
|
||||||
res.send(imageBuffer);
|
if (!file) throw new HTTPError("File not found");
|
||||||
|
const result = await FileType.fromBuffer(file);
|
||||||
|
|
||||||
|
res.set("Content-Type", result?.mime);
|
||||||
|
|
||||||
|
return res.send(file);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
Loading…
Reference in New Issue
Block a user