feat: proxy archiver and pdf margin settings
This commit is contained in:
parent
047e156cfb
commit
ae561ff227
|
@ -1,4 +1,4 @@
|
|||
import { chromium, devices } from "playwright";
|
||||
import { LaunchOptions, chromium, devices } from "playwright";
|
||||
import { prisma } from "./db";
|
||||
import createFile from "./storage/createFile";
|
||||
import sendToWayback from "./sendToWayback";
|
||||
|
@ -20,7 +20,18 @@ type LinksAndCollectionAndOwner = Link & {
|
|||
const BROWSER_TIMEOUT = Number(process.env.BROWSER_TIMEOUT) || 5;
|
||||
|
||||
export default async function archiveHandler(link: LinksAndCollectionAndOwner) {
|
||||
const browser = await chromium.launch();
|
||||
// allow user to configure a proxy
|
||||
let browserOptions: LaunchOptions = {};
|
||||
if (process.env.ARCHIVER_PROXY) {
|
||||
browserOptions.proxy = {
|
||||
server: process.env.ARCHIVER_PROXY,
|
||||
bypass: process.env.ARCHIVER_PROXY_BYPASS,
|
||||
username: process.env.ARCHIVER_PROXY_USERNAME,
|
||||
password: process.env.ARCHIVER_PROXY_PASSWORD,
|
||||
}
|
||||
}
|
||||
|
||||
const browser = await chromium.launch(browserOptions);
|
||||
const context = await browser.newContext(devices["Desktop Chrome"]);
|
||||
const page = await context.newPage();
|
||||
|
||||
|
@ -238,6 +249,15 @@ export default async function archiveHandler(link: LinksAndCollectionAndOwner) {
|
|||
})
|
||||
);
|
||||
}
|
||||
|
||||
// apply administrator's defined pdf margins or default to 15px
|
||||
const margins = { top: "15px", bottom: "15px" };
|
||||
if (process.env.ARCHIVER_PDF_MARGIN_TOP) {
|
||||
margins.top = process.env.ARCHIVER_PDF_MARGIN_TOP;
|
||||
} else if (process.env.ARCHIVER_PDF_MARGIN_BOTTOM) {
|
||||
margins.bottom = process.env.ARCHIVER_PDF_MARGIN_BOTTOM;
|
||||
}
|
||||
|
||||
if (user.archiveAsPDF && !link.pdf?.startsWith("archive")) {
|
||||
processingPromises.push(
|
||||
page
|
||||
|
@ -245,7 +265,7 @@ export default async function archiveHandler(link: LinksAndCollectionAndOwner) {
|
|||
width: "1366px",
|
||||
height: "1931px",
|
||||
printBackground: true,
|
||||
margin: { top: "15px", bottom: "15px" },
|
||||
margin: margins,
|
||||
})
|
||||
.then((pdf) => {
|
||||
return createFile({
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import fetch from "node-fetch";
|
||||
import https from "https";
|
||||
import { SocksProxyAgent } from "socks-proxy-agent";
|
||||
|
||||
export default async function getTitle(url: string) {
|
||||
try {
|
||||
const httpsAgent = new https.Agent({
|
||||
|
@ -7,9 +9,26 @@ export default async function getTitle(url: string) {
|
|||
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
|
||||
});
|
||||
|
||||
const response = await fetch(url, {
|
||||
// fetchOpts allows a proxy to be defined
|
||||
let fetchOpts = {
|
||||
agent: httpsAgent,
|
||||
});
|
||||
};
|
||||
|
||||
if (process.env.ARCHIVER_PROXY) {
|
||||
// parse proxy url
|
||||
let proxy = new URL(process.env.ARCHIVER_PROXY)
|
||||
// if authentication set, apply to proxy URL
|
||||
if (process.env.ARCHIVER_PROXY_USERNAME) {
|
||||
proxy.username = process.env.ARCHIVER_PROXY_USERNAME;
|
||||
proxy.password = process.env.ARCHIVER_PROXY_PASSWORD || "";
|
||||
}
|
||||
|
||||
// add socks5 proxy to fetchOpts
|
||||
fetchOpts = { agent: new SocksProxyAgent(proxy.toString()) }; //TODO: add support for http/https proxies
|
||||
}
|
||||
|
||||
const response = await fetch(url, fetchOpts);
|
||||
|
||||
const text = await response.text();
|
||||
|
||||
// regular expression to find the <title> tag
|
||||
|
|
|
@ -396,6 +396,16 @@ declare global {
|
|||
ZOOM_CUSTOM_NAME?: string;
|
||||
ZOOM_CLIENT_ID?: string;
|
||||
ZOOM_CLIENT_SECRET?: string;
|
||||
|
||||
// Proxy settings
|
||||
ARCHIVER_PROXY?: string;
|
||||
ARCHIVER_PROXY_USERNAME?: string;
|
||||
ARCHIVER_PROXY_PASSWORD?: string;
|
||||
ARCHIVER_PROXY_BYPASS?: string;
|
||||
|
||||
// PDF archive settings
|
||||
ARCHIVER_PDF_MARGIN_TOP?: string;
|
||||
ARCHIVER_PDF_MARGIN_BOTTOM?: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ŝarĝante…
Reference in New Issue