/*
Fosscord: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Fosscord and Fosscord Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
import { Config, Embed, EmbedType } from "@fosscord/util";
import fetch, { RequestInit } from "node-fetch";
import * as cheerio from "cheerio";
import probe from "probe-image-size";
import crypto from "crypto";
import { yellow } from "picocolors";
export const DEFAULT_FETCH_OPTIONS: RequestInit = {
redirect: "follow",
follow: 1,
headers: {
"user-agent":
"Mozilla/5.0 (compatible; Fosscord/1.0; +https://github.com/fosscord/fosscord)",
},
// size: 1024 * 1024 * 5, // grabbed from config later
compress: true,
method: "GET",
};
let hasWarnedAboutImagor = false;
export const getProxyUrl = (
url: URL,
width: number,
height: number,
): string => {
const { resizeWidthMax, resizeHeightMax, imagorServerUrl } =
Config.get().cdn;
const secret = Config.get().security.requestSignature;
width = Math.min(width || 500, resizeWidthMax || width);
height = Math.min(height || 500, resizeHeightMax || width);
// Imagor
if (imagorServerUrl) {
const path = `${width}x${height}/${url.host}${url.pathname}`;
const hash = crypto
.createHmac("sha1", secret)
.update(path)
.digest("base64")
.replace(/\+/g, "-")
.replace(/\//g, "_");
return `${imagorServerUrl}/${hash}/${path}`;
}
if (!hasWarnedAboutImagor) {
hasWarnedAboutImagor = true;
console.log(
"[Embeds]",
yellow(
"Imagor has not been set up correctly. https://docs.fosscord.com/setup/server/configuration/imagor/",
),
);
}
return url.toString();
};
const getMeta = ($: cheerio.CheerioAPI, name: string): string | undefined => {
let elem = $(`meta[property="${name}"]`);
if (!elem.length) elem = $(`meta[name="${name}"]`);
return elem.attr("content") || elem.text();
};
export const getMetaDescriptions = (text: string) => {
const $ = cheerio.load(text);
return {
title: getMeta($, "og:title") || $("title").first().text(),
provider_name: getMeta($, "og:site_name"),
author: getMeta($, "article:author"),
description: getMeta($, "og:description") || getMeta($, "description"),
image: getMeta($, "og:image") || getMeta($, "twitter:image"),
image_fallback: $(`image`).attr("src"),
video_fallback: $(`video`).attr("src"),
width: parseInt(getMeta($, "og:image:width") || "0"),
height: parseInt(getMeta($, "og:image:height") || "0"),
url: getMeta($, "og:url"),
youtube_embed: getMeta($, "og:video:secure_url"),
};
};
const doFetch = async (url: URL) => {
try {
return await fetch(url, {
...DEFAULT_FETCH_OPTIONS,
size: Config.get().limits.message.maxEmbedDownloadSize,
});
} catch (e) {
return null;
}
};
const genericImageHandler = async (url: URL): Promise