2022-06-19 00:25:21 +00:00
|
|
|
import * as cheerio from "cheerio";
|
|
|
|
import got from "got";
|
|
|
|
import {URL} from "url";
|
|
|
|
import mime from "mime-types";
|
|
|
|
|
|
|
|
import log from "../../log";
|
|
|
|
import Config from "../../config";
|
2023-01-29 15:58:33 +00:00
|
|
|
import {findLinksWithSchema} from "../../../shared/linkify";
|
2022-06-19 00:25:21 +00:00
|
|
|
import storage from "../storage";
|
|
|
|
import Client from "../../client";
|
|
|
|
import Chan from "../../models/chan";
|
|
|
|
import Msg from "../../models/msg";
|
|
|
|
|
|
|
|
type FetchRequest = {
|
|
|
|
data: Buffer;
|
|
|
|
type: string;
|
|
|
|
size: number;
|
|
|
|
};
|
|
|
|
const currentFetchPromises = new Map<string, Promise<FetchRequest>>();
|
2019-04-15 16:19:50 +00:00
|
|
|
const imageTypeRegex = /^image\/.+/;
|
2017-12-14 11:14:45 +00:00
|
|
|
const mediaTypeRegex = /^(audio|video)\/.+/;
|
2022-04-09 00:19:08 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export type LinkPreview = {
|
|
|
|
type: string;
|
|
|
|
head: string;
|
|
|
|
body: string;
|
|
|
|
thumb: string;
|
|
|
|
size: number;
|
|
|
|
link: string; // Send original matched link to the client
|
|
|
|
shown?: boolean | null;
|
|
|
|
error?: string;
|
|
|
|
message?: string;
|
|
|
|
|
|
|
|
media?: string;
|
|
|
|
mediaType?: string;
|
|
|
|
maxSize?: number;
|
|
|
|
thumbActualUrl?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function (client: Client, chan: Chan, msg: Msg, cleanText: string) {
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!Config.values.prefetch) {
|
2016-12-09 20:46:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
msg.previews = findLinksWithSchema(cleanText).reduce((cleanLinks: LinkPreview[], link) => {
|
2018-04-27 13:27:26 +00:00
|
|
|
const url = normalizeURL(link.link);
|
2016-12-09 20:46:53 +00:00
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
// If the URL is invalid and cannot be normalized, don't fetch it
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!url) {
|
2018-04-27 13:27:26 +00:00
|
|
|
return cleanLinks;
|
|
|
|
}
|
2016-12-09 20:46:53 +00:00
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
// If there are too many urls in this message, only fetch first X valid links
|
|
|
|
if (cleanLinks.length > 4) {
|
|
|
|
return cleanLinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not fetch duplicate links twice
|
|
|
|
if (cleanLinks.some((l) => l.link === link.link)) {
|
|
|
|
return cleanLinks;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const preview: LinkPreview = {
|
2018-04-27 13:27:26 +00:00
|
|
|
type: "loading",
|
|
|
|
head: "",
|
|
|
|
body: "",
|
|
|
|
thumb: "",
|
2019-08-09 20:20:08 +00:00
|
|
|
size: -1,
|
2018-04-27 13:27:26 +00:00
|
|
|
link: link.link, // Send original matched link to the client
|
2019-12-23 10:26:57 +00:00
|
|
|
shown: null,
|
2018-04-27 13:27:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cleanLinks.push(preview);
|
|
|
|
|
|
|
|
fetch(url, {
|
2018-03-23 14:50:52 +00:00
|
|
|
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
2022-06-19 00:25:21 +00:00
|
|
|
language: client.config.browser?.language || "",
|
2019-07-17 09:33:59 +00:00
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
parse(msg, chan, preview, res, client);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
preview.type = "error";
|
|
|
|
preview.error = "message";
|
|
|
|
preview.message = err.message;
|
2019-12-13 09:43:13 +00:00
|
|
|
emitPreview(client, chan, msg, preview);
|
2019-07-17 09:33:59 +00:00
|
|
|
});
|
2018-04-27 13:27:26 +00:00
|
|
|
|
|
|
|
return cleanLinks;
|
|
|
|
}, []);
|
2022-06-19 00:25:21 +00:00
|
|
|
}
|
2014-09-27 19:17:05 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function parseHtml(preview, res, client: Client) {
|
|
|
|
// TODO:
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
return new Promise((resolve: (preview: FetchRequest | null) => void) => {
|
2018-01-11 11:33:36 +00:00
|
|
|
const $ = cheerio.load(res.data);
|
2017-08-13 09:58:27 +00:00
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
return parseHtmlMedia($, preview, client)
|
2017-12-14 11:14:45 +00:00
|
|
|
.then((newRes) => resolve(newRes))
|
|
|
|
.catch(() => {
|
|
|
|
preview.type = "link";
|
|
|
|
preview.head =
|
2019-07-17 09:33:59 +00:00
|
|
|
$('meta[property="og:title"]').attr("content") ||
|
2020-03-21 20:55:36 +00:00
|
|
|
$("head > title, title").first().text() ||
|
2019-07-17 09:33:59 +00:00
|
|
|
"";
|
2017-12-14 11:14:45 +00:00
|
|
|
preview.body =
|
2019-07-17 09:33:59 +00:00
|
|
|
$('meta[property="og:description"]').attr("content") ||
|
|
|
|
$('meta[name="description"]').attr("content") ||
|
|
|
|
"";
|
2017-12-14 11:14:45 +00:00
|
|
|
|
2019-12-19 11:40:32 +00:00
|
|
|
if (preview.head.length) {
|
|
|
|
preview.head = preview.head.substr(0, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preview.body.length) {
|
|
|
|
preview.body = preview.body.substr(0, 300);
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!Config.values.prefetchStorage && Config.values.disableMediaPreview) {
|
2020-07-12 02:04:04 +00:00
|
|
|
resolve(res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let thumb =
|
|
|
|
$('meta[property="og:image"]').attr("content") ||
|
|
|
|
$('meta[name="twitter:image:src"]').attr("content") ||
|
|
|
|
$('link[rel="image_src"]').attr("href") ||
|
|
|
|
"";
|
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
// Make sure thumbnail is a valid and absolute url
|
2019-12-13 09:43:13 +00:00
|
|
|
if (thumb.length) {
|
|
|
|
thumb = normalizeURL(thumb, preview.link) || "";
|
2017-06-22 19:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
// Verify that thumbnail pic exists and is under allowed size
|
2019-12-13 09:43:13 +00:00
|
|
|
if (thumb.length) {
|
2022-06-19 00:25:21 +00:00
|
|
|
fetch(thumb, {language: client.config.browser?.language || ""})
|
2019-07-17 09:33:59 +00:00
|
|
|
.then((resThumb) => {
|
|
|
|
if (
|
2019-12-13 09:43:13 +00:00
|
|
|
resThumb !== null &&
|
|
|
|
imageTypeRegex.test(resThumb.type) &&
|
2022-05-01 19:12:39 +00:00
|
|
|
resThumb.size <= Config.values.prefetchMaxImageSize * 1024
|
2019-07-17 09:33:59 +00:00
|
|
|
) {
|
2019-12-13 09:43:13 +00:00
|
|
|
preview.thumbActualUrl = thumb;
|
2019-07-17 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(resThumb);
|
|
|
|
})
|
2019-12-13 09:43:13 +00:00
|
|
|
.catch(() => resolve(null));
|
2017-12-14 11:14:45 +00:00
|
|
|
} else {
|
|
|
|
resolve(res);
|
|
|
|
}
|
2017-06-22 19:32:13 +00:00
|
|
|
});
|
2017-12-14 11:14:45 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-22 19:32:13 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
// TODO: type $
|
|
|
|
function parseHtmlMedia($: any, preview, client: Client): Promise<FetchRequest> {
|
2017-12-14 11:14:45 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.disableMediaPreview) {
|
2020-07-12 02:04:04 +00:00
|
|
|
reject();
|
2020-08-21 07:18:41 +00:00
|
|
|
return;
|
2020-07-12 02:04:04 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
let foundMedia = false;
|
2020-08-21 07:16:54 +00:00
|
|
|
const openGraphType = $('meta[property="og:type"]').attr("content");
|
|
|
|
|
|
|
|
// Certain news websites may include video and audio tags,
|
|
|
|
// despite actually being an article (as indicated by og:type).
|
|
|
|
// If there is og:type tag, we will only select video or audio if it matches
|
|
|
|
if (
|
|
|
|
openGraphType &&
|
|
|
|
!openGraphType.startsWith("video") &&
|
|
|
|
!openGraphType.startsWith("music")
|
|
|
|
) {
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-14 11:14:45 +00:00
|
|
|
|
|
|
|
["video", "audio"].forEach((type) => {
|
|
|
|
if (foundMedia) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
$(`meta[property="og:${type}:type"]`).each(function (this: cheerio.Element, i: number) {
|
2017-12-14 11:14:45 +00:00
|
|
|
const mimeType = $(this).attr("content");
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!mimeType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
if (mediaTypeRegex.test(mimeType)) {
|
|
|
|
// If we match a clean video or audio tag, parse that as a preview instead
|
2018-04-27 13:27:26 +00:00
|
|
|
let mediaUrl = $($(`meta[property="og:${type}"]`).get(i)).attr("content");
|
2017-12-14 11:14:45 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!mediaUrl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
// Make sure media is a valid url
|
2018-04-27 13:27:26 +00:00
|
|
|
mediaUrl = normalizeURL(mediaUrl, preview.link, true);
|
|
|
|
|
|
|
|
// Make sure media is a valid url
|
|
|
|
if (!mediaUrl) {
|
2017-12-14 11:14:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foundMedia = true;
|
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
fetch(mediaUrl, {
|
2019-07-17 09:33:59 +00:00
|
|
|
accept:
|
|
|
|
type === "video"
|
|
|
|
? "video/webm,video/ogg,video/*;q=0.9,application/ogg;q=0.7,audio/*;q=0.6,*/*;q=0.5"
|
|
|
|
: "audio/webm, audio/ogg, audio/wav, audio/*;q=0.9, application/ogg;q=0.7, video/*;q=0.6; */*;q=0.5",
|
2022-06-19 00:25:21 +00:00
|
|
|
language: client.config.browser?.language || "",
|
2019-07-17 09:33:59 +00:00
|
|
|
})
|
|
|
|
.then((resMedia) => {
|
|
|
|
if (resMedia === null || !mediaTypeRegex.test(resMedia.type)) {
|
|
|
|
return reject();
|
|
|
|
}
|
2017-12-14 11:14:45 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
preview.type = type;
|
|
|
|
preview.media = mediaUrl;
|
|
|
|
preview.mediaType = resMedia.type;
|
2017-12-14 11:14:45 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
resolve(resMedia);
|
|
|
|
})
|
|
|
|
.catch(reject);
|
2017-12-14 11:14:45 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!foundMedia) {
|
|
|
|
reject();
|
2017-06-22 19:32:13 +00:00
|
|
|
}
|
2017-12-14 11:14:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function parse(msg: Msg, chan: Chan, preview: LinkPreview, res: FetchRequest, client: Client) {
|
|
|
|
let promise: Promise<FetchRequest | null> | null = null;
|
2017-06-22 19:32:13 +00:00
|
|
|
|
2019-08-09 20:20:08 +00:00
|
|
|
preview.size = res.size;
|
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
switch (res.type) {
|
2019-07-17 09:33:59 +00:00
|
|
|
case "text/html":
|
2019-08-09 20:20:08 +00:00
|
|
|
preview.size = -1;
|
2019-07-17 09:33:59 +00:00
|
|
|
promise = parseHtml(preview, res, client);
|
|
|
|
break;
|
|
|
|
|
2019-12-19 11:40:32 +00:00
|
|
|
case "text/plain":
|
|
|
|
preview.type = "link";
|
|
|
|
preview.body = res.data.toString().substr(0, 300);
|
|
|
|
break;
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
case "image/png":
|
|
|
|
case "image/gif":
|
|
|
|
case "image/jpg":
|
|
|
|
case "image/jpeg":
|
2021-05-08 08:10:45 +00:00
|
|
|
case "image/jxl":
|
2019-07-17 09:33:59 +00:00
|
|
|
case "image/webp":
|
2020-08-23 09:51:52 +00:00
|
|
|
case "image/avif":
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!Config.values.prefetchStorage && Config.values.disableMediaPreview) {
|
2020-07-12 02:04:04 +00:00
|
|
|
return removePreview(msg, preview);
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (res.size > Config.values.prefetchMaxImageSize * 1024) {
|
2019-07-17 09:33:59 +00:00
|
|
|
preview.type = "error";
|
|
|
|
preview.error = "image-too-big";
|
2022-05-01 19:12:39 +00:00
|
|
|
preview.maxSize = Config.values.prefetchMaxImageSize * 1024;
|
2019-07-17 09:33:59 +00:00
|
|
|
} else {
|
|
|
|
preview.type = "image";
|
2019-12-13 09:43:13 +00:00
|
|
|
preview.thumbActualUrl = preview.link;
|
2019-07-17 09:33:59 +00:00
|
|
|
}
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2017-12-06 22:27:35 +00:00
|
|
|
break;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
case "audio/midi":
|
|
|
|
case "audio/mpeg":
|
|
|
|
case "audio/mpeg3":
|
|
|
|
case "audio/ogg":
|
|
|
|
case "audio/wav":
|
2020-02-26 08:07:40 +00:00
|
|
|
case "audio/x-wav":
|
2019-07-17 09:33:59 +00:00
|
|
|
case "audio/x-mid":
|
|
|
|
case "audio/x-midi":
|
|
|
|
case "audio/x-mpeg":
|
|
|
|
case "audio/x-mpeg-3":
|
2021-05-06 00:02:23 +00:00
|
|
|
case "audio/flac":
|
2022-02-12 01:42:59 +00:00
|
|
|
case "audio/x-flac":
|
|
|
|
case "audio/mp4":
|
2021-05-06 00:02:23 +00:00
|
|
|
case "audio/x-m4a":
|
2019-07-17 09:33:59 +00:00
|
|
|
if (!preview.link.startsWith("https://")) {
|
|
|
|
break;
|
|
|
|
}
|
2017-12-09 23:25:01 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.disableMediaPreview) {
|
2020-07-12 02:04:04 +00:00
|
|
|
return removePreview(msg, preview);
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
preview.type = "audio";
|
|
|
|
preview.media = preview.link;
|
|
|
|
preview.mediaType = res.type;
|
2017-12-09 23:25:01 +00:00
|
|
|
|
|
|
|
break;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
case "video/webm":
|
|
|
|
case "video/ogg":
|
|
|
|
case "video/mp4":
|
|
|
|
if (!preview.link.startsWith("https://")) {
|
|
|
|
break;
|
|
|
|
}
|
2017-12-09 23:25:01 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.disableMediaPreview) {
|
2020-07-12 02:04:04 +00:00
|
|
|
return removePreview(msg, preview);
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
preview.type = "video";
|
|
|
|
preview.media = preview.link;
|
|
|
|
preview.mediaType = res.type;
|
2017-12-06 22:27:35 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return removePreview(msg, preview);
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
2014-09-27 23:47:04 +00:00
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
if (!promise) {
|
2018-07-10 11:57:11 +00:00
|
|
|
return handlePreview(client, chan, msg, preview, res);
|
2017-12-14 11:14:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
void promise.then((newRes) => handlePreview(client, chan, msg, preview, newRes));
|
2017-07-06 15:33:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function handlePreview(client: Client, chan: Chan, msg: Msg, preview: LinkPreview, res) {
|
2019-12-13 09:43:13 +00:00
|
|
|
const thumb = preview.thumbActualUrl || "";
|
|
|
|
delete preview.thumbActualUrl;
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!thumb.length || !Config.values.prefetchStorage) {
|
2019-12-13 09:43:13 +00:00
|
|
|
preview.thumb = thumb;
|
2018-07-10 11:57:11 +00:00
|
|
|
return emitPreview(client, chan, msg, preview);
|
2017-07-06 15:33:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 10:46:51 +00:00
|
|
|
// Get the correct file extension for the provided content-type
|
|
|
|
// This is done to prevent user-input being stored in the file name (extension)
|
|
|
|
const extension = mime.extension(res.type);
|
|
|
|
|
|
|
|
if (!extension) {
|
|
|
|
// For link previews, drop the thumbnail
|
|
|
|
// For other types, do not display preview at all
|
|
|
|
if (preview.type !== "link") {
|
2018-06-03 09:25:01 +00:00
|
|
|
return removePreview(msg, preview);
|
2017-12-30 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
return emitPreview(client, chan, msg, preview);
|
2017-12-30 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.store(res.data, extension, (uri) => {
|
2017-08-13 09:58:27 +00:00
|
|
|
preview.thumb = uri;
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
emitPreview(client, chan, msg, preview);
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
2017-06-26 06:27:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function emitPreview(client: Client, chan: Chan, msg: Msg, preview: LinkPreview) {
|
2017-06-26 06:27:51 +00:00
|
|
|
// If there is no title but there is preview or description, set title
|
|
|
|
// otherwise bail out and show no preview
|
2017-06-26 09:01:55 +00:00
|
|
|
if (!preview.head.length && preview.type === "link") {
|
|
|
|
if (preview.thumb.length || preview.body.length) {
|
|
|
|
preview.head = "Untitled page";
|
2017-06-26 06:27:51 +00:00
|
|
|
} else {
|
2018-06-03 09:25:01 +00:00
|
|
|
return removePreview(msg, preview);
|
2017-06-26 06:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
client.emit("msg:preview", {
|
|
|
|
id: msg.id,
|
|
|
|
chan: chan.id,
|
|
|
|
preview: preview,
|
|
|
|
});
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function removePreview(msg: Msg, preview: LinkPreview) {
|
2018-06-03 09:25:01 +00:00
|
|
|
// If a preview fails to load, remove the link from msg object
|
|
|
|
// So that client doesn't attempt to display an preview on page reload
|
|
|
|
const index = msg.previews.indexOf(preview);
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
msg.previews.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function getRequestHeaders(headers: Record<string, string>) {
|
2018-03-23 14:50:52 +00:00
|
|
|
const formattedHeaders = {
|
2019-12-17 20:35:15 +00:00
|
|
|
// Certain websites like Amazon only add <meta> tags to known bots,
|
|
|
|
// lets pretend to be them to get the metadata
|
2019-07-17 09:33:59 +00:00
|
|
|
"User-Agent":
|
2023-10-09 10:28:14 +00:00
|
|
|
"Mozilla/5.0 (compatible; Hard Lounge IRC Client; COLD HARD CHATS; +https://git.supernets.org/supernets/hardlounge)" +
|
2019-12-17 20:35:15 +00:00
|
|
|
" facebookexternalhit/1.1 Twitterbot/1.0",
|
2019-07-17 09:33:59 +00:00
|
|
|
Accept: headers.accept || "*/*",
|
2018-08-08 00:09:45 +00:00
|
|
|
"X-Purpose": "preview",
|
2017-12-28 13:34:49 +00:00
|
|
|
};
|
|
|
|
|
2018-03-23 14:50:52 +00:00
|
|
|
if (headers.language) {
|
|
|
|
formattedHeaders["Accept-Language"] = headers.language;
|
2017-12-28 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 14:50:52 +00:00
|
|
|
return formattedHeaders;
|
2017-12-28 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function fetch(uri: string, headers: Record<string, string>) {
|
2018-06-10 17:32:52 +00:00
|
|
|
// Stringify the object otherwise the objects won't compute to the same value
|
|
|
|
const cacheKey = JSON.stringify([uri, headers]);
|
|
|
|
let promise = currentFetchPromises.get(cacheKey);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-06-10 17:32:52 +00:00
|
|
|
if (promise) {
|
|
|
|
return promise;
|
2015-01-04 02:58:12 +00:00
|
|
|
}
|
2017-11-10 20:44:14 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
const prefetchTimeout = Config.values.prefetchTimeout;
|
2022-04-09 00:19:08 +00:00
|
|
|
|
|
|
|
if (!prefetchTimeout) {
|
2022-04-09 19:40:38 +00:00
|
|
|
log.warn(
|
2023-10-09 10:28:14 +00:00
|
|
|
"prefetchTimeout is missing from your Hard Lounge configuration, defaulting to 5000 ms"
|
2022-04-09 19:40:38 +00:00
|
|
|
);
|
2022-04-09 00:19:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
promise = new Promise<FetchRequest>((resolve, reject) => {
|
2019-04-15 16:19:50 +00:00
|
|
|
let buffer = Buffer.from("");
|
2019-12-07 21:21:42 +00:00
|
|
|
let contentLength = 0;
|
2022-06-19 00:25:21 +00:00
|
|
|
let contentType: string | undefined;
|
2022-05-01 19:12:39 +00:00
|
|
|
let limit = Config.values.prefetchMaxImageSize * 1024;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
try {
|
2019-12-07 21:21:42 +00:00
|
|
|
const gotStream = got.stream(uri, {
|
|
|
|
retry: 0,
|
2022-04-09 00:19:08 +00:00
|
|
|
timeout: prefetchTimeout || 5000, // milliseconds
|
2019-07-17 09:33:59 +00:00
|
|
|
headers: getRequestHeaders(headers),
|
2023-06-25 08:44:14 +00:00
|
|
|
localAddress: Config.values.bind,
|
2019-12-07 21:21:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
gotStream
|
2020-03-21 20:55:36 +00:00
|
|
|
.on("response", function (res) {
|
2019-12-07 21:21:42 +00:00
|
|
|
contentLength = parseInt(res.headers["content-length"], 10) || 0;
|
|
|
|
contentType = res.headers["content-type"];
|
2019-04-15 16:19:50 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (contentType && imageTypeRegex.test(contentType)) {
|
2019-04-15 16:19:50 +00:00
|
|
|
// response is an image
|
|
|
|
// if Content-Length header reports a size exceeding the prefetch limit, abort fetch
|
2021-11-23 00:59:33 +00:00
|
|
|
// and if file is not to be stored we don't need to download further either
|
2022-05-01 19:12:39 +00:00
|
|
|
if (contentLength > limit || !Config.values.prefetchStorage) {
|
2019-12-07 21:21:42 +00:00
|
|
|
gotStream.destroy();
|
2019-04-15 16:19:50 +00:00
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
} else if (contentType && mediaTypeRegex.test(contentType)) {
|
2019-04-15 16:19:50 +00:00
|
|
|
// We don't need to download the file any further after we received content-type header
|
2019-12-07 21:21:42 +00:00
|
|
|
gotStream.destroy();
|
2019-04-15 16:19:50 +00:00
|
|
|
} else {
|
2020-12-30 10:52:12 +00:00
|
|
|
// if not image, limit download to the max search size, since we need only meta tags
|
|
|
|
// twitter.com sends opengraph meta tags within ~20kb of data for individual tweets, the default is set to 50.
|
|
|
|
// for sites like Youtube the og tags are in the first 300K and hence this is configurable by the admin
|
|
|
|
limit =
|
2022-05-01 19:12:39 +00:00
|
|
|
"prefetchMaxSearchSize" in Config.values
|
|
|
|
? Config.values.prefetchMaxSearchSize * 1024
|
2020-12-30 10:52:12 +00:00
|
|
|
: // set to the previous size if config option is unset
|
|
|
|
50 * 1024;
|
2018-06-10 17:32:52 +00:00
|
|
|
}
|
2019-04-15 16:19:50 +00:00
|
|
|
})
|
|
|
|
.on("error", (e) => reject(e))
|
|
|
|
.on("data", (data) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
buffer = Buffer.concat(
|
|
|
|
[buffer, data],
|
|
|
|
buffer.length + (data as Array<any>).length
|
|
|
|
);
|
2019-04-15 16:19:50 +00:00
|
|
|
|
|
|
|
if (buffer.length >= limit) {
|
2019-12-07 21:21:42 +00:00
|
|
|
gotStream.destroy();
|
2019-04-15 16:19:50 +00:00
|
|
|
}
|
|
|
|
})
|
2019-12-07 21:21:42 +00:00
|
|
|
.on("end", () => gotStream.destroy())
|
|
|
|
.on("close", () => {
|
2019-04-15 16:19:50 +00:00
|
|
|
let type = "";
|
2017-06-21 05:51:14 +00:00
|
|
|
|
2019-12-07 21:21:42 +00:00
|
|
|
// If we downloaded more data then specified in Content-Length, use real data size
|
|
|
|
const size = contentLength > buffer.length ? contentLength : buffer.length;
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2019-12-07 21:21:42 +00:00
|
|
|
if (contentType) {
|
2022-06-19 00:25:21 +00:00
|
|
|
type = contentType.split(/ *; */).shift() || "";
|
2019-04-15 16:19:50 +00:00
|
|
|
}
|
2018-06-10 17:32:52 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
resolve({data: buffer, type, size});
|
|
|
|
});
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
2019-04-15 16:19:50 +00:00
|
|
|
return reject(e);
|
|
|
|
}
|
2018-06-10 17:32:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const removeCache = () => currentFetchPromises.delete(cacheKey);
|
|
|
|
|
|
|
|
promise.then(removeCache).catch(removeCache);
|
|
|
|
|
|
|
|
currentFetchPromises.set(cacheKey, promise);
|
|
|
|
|
|
|
|
return promise;
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
2016-03-25 09:45:39 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function normalizeURL(link: string, baseLink?: string, disallowHttp = false) {
|
2018-04-27 11:11:54 +00:00
|
|
|
try {
|
2018-04-27 13:27:26 +00:00
|
|
|
const url = new URL(link, baseLink);
|
2018-04-27 11:11:54 +00:00
|
|
|
|
|
|
|
// Only fetch http and https links
|
2018-04-27 13:27:26 +00:00
|
|
|
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
2022-06-19 00:25:21 +00:00
|
|
|
return undefined;
|
2018-04-27 13:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (disallowHttp && url.protocol === "http:") {
|
2022-06-19 00:25:21 +00:00
|
|
|
return undefined;
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not fetch links without hostname or ones that contain authorization
|
2018-04-27 13:27:26 +00:00
|
|
|
if (!url.hostname || url.username || url.password) {
|
2022-06-19 00:25:21 +00:00
|
|
|
return undefined;
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|
2018-04-27 13:27:26 +00:00
|
|
|
|
|
|
|
// Drop hash from the url, if any
|
|
|
|
url.hash = "";
|
|
|
|
|
|
|
|
return url.toString();
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
2018-04-27 13:27:26 +00:00
|
|
|
// if an exception was thrown, the url is not valid
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
return undefined;
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|