Merge pull request #562 from linkwarden/hotfix/title-fetching

added a new env var + bug fixed
This commit is contained in:
Daniel 2024-04-17 18:03:36 -04:00 committed by GitHub
commit f30c652676
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 8 deletions

View File

@ -21,6 +21,7 @@ ARCHIVE_TAKE_COUNT=
BROWSER_TIMEOUT= BROWSER_TIMEOUT=
IGNORE_UNAUTHORIZED_CA= IGNORE_UNAUTHORIZED_CA=
IGNORE_HTTPS_ERRORS= IGNORE_HTTPS_ERRORS=
IGNORE_URL_SIZE_LIMIT=
# AWS S3 Settings # AWS S3 Settings
SPACES_KEY= SPACES_KEY=

View File

@ -1,17 +1,35 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import https from "https"; import https from "https";
import { SocksProxyAgent } from "socks-proxy-agent";
export default async function validateUrlSize(url: string) { export default async function validateUrlSize(url: string) {
if (process.env.IGNORE_URL_SIZE_LIMIT === "true") return null;
try { try {
const httpsAgent = new https.Agent({ const httpsAgent = new https.Agent({
rejectUnauthorized: rejectUnauthorized:
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true, process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
}); });
const response = await fetch(url, { let fetchOpts = {
method: "HEAD", method: "HEAD",
agent: httpsAgent, agent: httpsAgent,
}); };
if (process.env.PROXY) {
let proxy = new URL(process.env.PROXY);
if (process.env.PROXY_USERNAME) {
proxy.username = process.env.PROXY_USERNAME;
proxy.password = process.env.PROXY_PASSWORD || "";
}
fetchOpts = {
method: "HEAD",
agent: new SocksProxyAgent(proxy.toString()),
};
}
const response = await fetch(url, fetchOpts);
const totalSizeMB = const totalSizeMB =
Number(response.headers.get("content-length")) / Math.pow(1024, 2); Number(response.headers.get("content-length")) / Math.pow(1024, 2);

View File

@ -27,14 +27,25 @@ export default async function getTitle(url: string) {
fetchOpts = { agent: new SocksProxyAgent(proxy.toString()) }; //TODO: add support for http/https proxies fetchOpts = { agent: new SocksProxyAgent(proxy.toString()) }; //TODO: add support for http/https proxies
} }
const response = await fetch(url, fetchOpts); const responsePromise = fetch(url, fetchOpts);
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error("Fetch title timeout"));
}, 10 * 1000); // Stop after 10 seconds
});
const text = await response.text(); const response = await Promise.race([responsePromise, timeoutPromise]);
// regular expression to find the <title> tag if ((response as any)?.status) {
let match = text.match(/<title.*>([^<]*)<\/title>/); const text = await (response as any).text();
if (match) return match[1];
else return ""; // regular expression to find the <title> tag
let match = text.match(/<title.*>([^<]*)<\/title>/);
if (match) return match[1];
else return "";
} else {
return "";
}
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }

View File

@ -13,6 +13,7 @@ declare global {
MAX_LINKS_PER_USER?: string; MAX_LINKS_PER_USER?: string;
ARCHIVE_TAKE_COUNT?: string; ARCHIVE_TAKE_COUNT?: string;
IGNORE_UNAUTHORIZED_CA?: string; IGNORE_UNAUTHORIZED_CA?: string;
IGNORE_URL_SIZE_LIMIT?: string;
SPACES_KEY?: string; SPACES_KEY?: string;
SPACES_SECRET?: string; SPACES_SECRET?: string;