adjustible archive formats + finalized settings page
This commit is contained in:
+56
-39
@@ -1,55 +1,72 @@
|
||||
import { Page, chromium, devices } from "playwright";
|
||||
import { chromium, devices } from "playwright";
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import createFile from "@/lib/api/storage/createFile";
|
||||
import sendToWayback from "./sendToWayback";
|
||||
|
||||
export default async function archive(linkId: number, url: string) {
|
||||
const browser = await chromium.launch();
|
||||
const context = await browser.newContext(devices["Desktop Chrome"]);
|
||||
const page = await context.newPage();
|
||||
export default async function archive(
|
||||
linkId: number,
|
||||
url: string,
|
||||
userId: number
|
||||
) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
|
||||
sendToWayback(url);
|
||||
if (user?.archiveAsWaybackMachine) sendToWayback(url);
|
||||
|
||||
try {
|
||||
await page.goto(url, { waitUntil: "domcontentloaded" });
|
||||
if (user?.archiveAsPDF || user?.archiveAsScreenshot) {
|
||||
const browser = await chromium.launch();
|
||||
const context = await browser.newContext(devices["Desktop Chrome"]);
|
||||
const page = await context.newPage();
|
||||
|
||||
await page.evaluate(
|
||||
autoScroll,
|
||||
Number(process.env.AUTOSCROLL_TIMEOUT) || 30
|
||||
);
|
||||
try {
|
||||
await page.goto(url, { waitUntil: "domcontentloaded" });
|
||||
|
||||
const linkExists = await prisma.link.findUnique({
|
||||
where: {
|
||||
id: linkId,
|
||||
},
|
||||
});
|
||||
await page.evaluate(
|
||||
autoScroll,
|
||||
Number(process.env.AUTOSCROLL_TIMEOUT) || 30
|
||||
);
|
||||
|
||||
if (linkExists) {
|
||||
const pdf = await page.pdf({
|
||||
width: "1366px",
|
||||
height: "1931px",
|
||||
printBackground: true,
|
||||
margin: { top: "15px", bottom: "15px" },
|
||||
});
|
||||
const screenshot = await page.screenshot({
|
||||
fullPage: true,
|
||||
const linkExists = await prisma.link.findUnique({
|
||||
where: {
|
||||
id: linkId,
|
||||
},
|
||||
});
|
||||
|
||||
createFile({
|
||||
data: screenshot,
|
||||
filePath: `archives/${linkExists.collectionId}/${linkId}.png`,
|
||||
});
|
||||
if (linkExists) {
|
||||
if (user.archiveAsScreenshot) {
|
||||
const screenshot = await page.screenshot({
|
||||
fullPage: true,
|
||||
});
|
||||
|
||||
createFile({
|
||||
data: pdf,
|
||||
filePath: `archives/${linkExists.collectionId}/${linkId}.pdf`,
|
||||
});
|
||||
createFile({
|
||||
data: screenshot,
|
||||
filePath: `archives/${linkExists.collectionId}/${linkId}.png`,
|
||||
});
|
||||
}
|
||||
|
||||
if (user.archiveAsPDF) {
|
||||
const pdf = await page.pdf({
|
||||
width: "1366px",
|
||||
height: "1931px",
|
||||
printBackground: true,
|
||||
margin: { top: "15px", bottom: "15px" },
|
||||
});
|
||||
|
||||
createFile({
|
||||
data: pdf,
|
||||
filePath: `archives/${linkExists.collectionId}/${linkId}.pdf`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await browser.close();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
await browser.close();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ export default async function postLink(
|
||||
|
||||
createFolder({ filePath: `archives/${newLink.collectionId}` });
|
||||
|
||||
archive(newLink.id, newLink.url);
|
||||
archive(newLink.id, newLink.url, userId);
|
||||
|
||||
return { response: newLink, status: 200 };
|
||||
}
|
||||
|
||||
@@ -108,6 +108,9 @@ export default async function updateUser(
|
||||
username: user.username.toLowerCase(),
|
||||
email: user.email?.toLowerCase(),
|
||||
isPrivate: user.isPrivate,
|
||||
archiveAsScreenshot: user.archiveAsScreenshot,
|
||||
archiveAsPDF: user.archiveAsPDF,
|
||||
archiveAsWaybackMachine: user.archiveAsWaybackMachine,
|
||||
password:
|
||||
user.newPassword && user.newPassword !== ""
|
||||
? newHashedPassword
|
||||
|
||||
Reference in New Issue
Block a user