Use imagor for image resizing

This commit is contained in:
Madeline 2022-10-02 15:28:03 +11:00
parent d341fe44d7
commit 5bbe2666d0
2 changed files with 17 additions and 3 deletions

View File

@ -2,7 +2,7 @@ import { Config, Embed, EmbedType } from "@fosscord/util";
import fetch, { Response } from "node-fetch"; import fetch, { Response } from "node-fetch";
import * as cheerio from "cheerio"; import * as cheerio from "cheerio";
import probe from "probe-image-size"; import probe from "probe-image-size";
import imageSize from "image-size"; import crypto from "crypto";
export const DEFAULT_FETCH_OPTIONS: any = { export const DEFAULT_FETCH_OPTIONS: any = {
redirect: "follow", redirect: "follow",
@ -18,16 +18,28 @@ export const DEFAULT_FETCH_OPTIONS: any = {
export const getProxyUrl = (url: URL, width: number, height: number) => { export const getProxyUrl = (url: URL, width: number, height: number) => {
const { endpointPublic, resizeWidthMax, resizeHeightMax } = Config.get().cdn; const { endpointPublic, resizeWidthMax, resizeHeightMax } = Config.get().cdn;
const secret = Config.get().security.jwtSecret; // maybe shouldn't use this?
width = Math.min(width || 500, resizeWidthMax || width); width = Math.min(width || 500, resizeWidthMax || width);
height = Math.min(height || 500, resizeHeightMax || width); height = Math.min(height || 500, resizeHeightMax || width);
return `${endpointPublic}/external/resize/${encodeURIComponent(url.href)}?width=${width}&height=${height}`;
let path = `${width}x${height}/${url.host}${url.pathname}`
const hash = crypto.createHmac('sha1', secret)
.update(path)
.digest('base64')
.replace(/\+/g, '-').replace(/\//g, '_');
// TODO: make configurable
return `https://media.understars.dev/${hash}/${path}`;
// return `${endpointPublic}/external/resize/${encodeURIComponent(url.href)}?width=${width}&height=${height}`;
}; };
const getMeta = ($: cheerio.CheerioAPI, name: string): string | undefined => { const getMeta = ($: cheerio.CheerioAPI, name: string): string | undefined => {
let elem = $(`meta[property="${name}"]`); let elem = $(`meta[property="${name}"]`);
if (!elem.length) elem = $(`meta[name="${name}"]`); if (!elem.length) elem = $(`meta[name="${name}"]`);
return elem.attr("content") || elem.text(); return elem.attr("content") || elem.text();
} };
export const getMetaDescriptions = (text: string) => { export const getMetaDescriptions = (text: string) => {
const $ = cheerio.load(text); const $ = cheerio.load(text);

View File

@ -58,6 +58,8 @@ router.get("/:id", async (req: Request, res: Response) => {
}); });
// this method is gross lol don't care // this method is gross lol don't care
// It's also no longer actually used on Slowcord's official server.
// We actually use imagor now
router.get("/resize/:url", async (req: Request, res: Response) => { router.get("/resize/:url", async (req: Request, res: Response) => {
const url = decodeURIComponent(req.params.url); const url = decodeURIComponent(req.params.url);
const { width, height } = req.query; const { width, height } = req.query;