From c68f9d68ada4ed704b132a6273afb1dd68bcc2b6 Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Tue, 18 Jun 2024 12:27:29 -0400 Subject: [PATCH] small changes --- components/ModalContent/UploadFileModal.tsx | 15 +- lib/api/archiveHandler.ts | 208 ++++++++------------ lib/api/manageLinkFiles.ts | 9 + 3 files changed, 99 insertions(+), 133 deletions(-) diff --git a/components/ModalContent/UploadFileModal.tsx b/components/ModalContent/UploadFileModal.tsx index 469bee6..5630080 100644 --- a/components/ModalContent/UploadFileModal.tsx +++ b/components/ModalContent/UploadFileModal.tsx @@ -5,7 +5,10 @@ import TextInput from "@/components/TextInput"; import unescapeString from "@/lib/client/unescapeString"; import useCollectionStore from "@/store/collections"; import useLinkStore from "@/store/links"; -import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; +import { + LinkIncludingShortenedCollectionAndTags, + ArchivedFormat, +} from "@/types/global"; import { useSession } from "next-auth/react"; import { useRouter } from "next/router"; import toast from "react-hot-toast"; @@ -110,12 +113,8 @@ export default function UploadFileModal({ onClose }: Props) { linkType = "pdf"; } - if (fileType !== null && linkType !== null) { - setSubmitLoader(true); - - let response; - - const load = toast.loading(t("creating")); + setSubmitLoader(true); + const load = toast.loading(t("creating")); const response = await uploadFile(link, file); @@ -150,7 +149,7 @@ export default function UploadFileModal({ onClose }: Props) { />

- {t("file_types", { + {t("file_types", { size: process.env.NEXT_PUBLIC_MAX_FILE_SIZE || 30, })}

diff --git a/lib/api/archiveHandler.ts b/lib/api/archiveHandler.ts index 24212a0..862c6a2 100644 --- a/lib/api/archiveHandler.ts +++ b/lib/api/archiveHandler.ts @@ -50,18 +50,6 @@ export default async function archiveHandler(link: LinksAndCollectionAndOwner) { const page = await context.newPage(); - const timeoutPromise = new Promise((_, reject) => { - setTimeout( - () => - reject( - new Error( - `Browser has been open for more than ${BROWSER_TIMEOUT} minutes.` - ) - ), - BROWSER_TIMEOUT * 60000 - ); - }); - createFolder({ filePath: `archives/preview/${link.collectionId}`, }); @@ -168,138 +156,108 @@ export default async function archiveHandler(link: LinksAndCollectionAndOwner) { return metaTag ? (metaTag as any).content : null; }); - if (ogImageUrl) { - console.log("Found og:image URL:", ogImageUrl); + if (ogImageUrl) { + console.log("Found og:image URL:", ogImageUrl); - // Download the image - const imageResponse = await page.goto(ogImageUrl); + // Download the image + const imageResponse = await page.goto(ogImageUrl); // Check if imageResponse is not null if (imageResponse && !link.preview?.startsWith("archive")) { const buffer = await imageResponse.body(); await generatePreview(buffer, link.collectionId, link.id); - - // Check if buffer is not null - if (buffer) { - // Load the image using Jimp - Jimp.read(buffer, async (err, image) => { - if (image && !err) { - image?.resize(1280, Jimp.AUTO).quality(20); - const processedBuffer = await image?.getBufferAsync( - Jimp.MIME_JPEG - ); - - createFile({ - data: processedBuffer, - filePath: `archives/preview/${link.collectionId}/${link.id}.jpeg`, - }).then(() => { - return prisma.link.update({ - where: { id: link.id }, - data: { - preview: `archives/preview/${link.collectionId}/${link.id}.jpeg`, - }, - }); - }); - } - }).catch((err) => { - console.error("Error processing the image:", err); - }); - } else { - console.log("No image data found."); - } } - await page.goBack(); - } else if (!link.preview?.startsWith("archive")) { - console.log("No og:image found"); - await page - .screenshot({ type: "jpeg", quality: 20 }) - .then((screenshot) => { + await page.goBack(); + } else if (!link.preview?.startsWith("archive")) { + console.log("No og:image found"); + await page + .screenshot({ type: "jpeg", quality: 20 }) + .then((screenshot) => { + return createFile({ + data: screenshot, + filePath: `archives/preview/${link.collectionId}/${link.id}.jpeg`, + }); + }) + .then(() => { + return prisma.link.update({ + where: { id: link.id }, + data: { + preview: `archives/preview/${link.collectionId}/${link.id}.jpeg`, + }, + }); + }); + } + } + + if ( + (!link.image?.startsWith("archives") && + !link.image?.startsWith("unavailable")) || + (!link.pdf?.startsWith("archives") && + !link.pdf?.startsWith("unavailable")) + ) { + // Screenshot/PDF + await page.evaluate( + autoScroll, + Number(process.env.AUTOSCROLL_TIMEOUT) || 30 + ); + + // Check if the user hasn't deleted the link by the time we're done scrolling + const linkExists = await prisma.link.findUnique({ + where: { id: link.id }, + }); + if (linkExists) { + const processingPromises = []; + + if ( + user.archiveAsScreenshot && + !link.image?.startsWith("archive") + ) { + processingPromises.push( + page.screenshot({ fullPage: true }).then((screenshot) => { return createFile({ data: screenshot, - filePath: `archives/preview/${link.collectionId}/${link.id}.jpeg`, + filePath: `archives/${linkExists.collectionId}/${link.id}.png`, }); }) - .then(() => { - return prisma.link.update({ - where: { id: link.id }, - data: { - preview: `archives/preview/${link.collectionId}/${link.id}.jpeg`, - }, - }); - }); + ); } - } - if ( - (!link.image?.startsWith("archives") && - !link.image?.startsWith("unavailable")) || - (!link.pdf?.startsWith("archives") && - !link.pdf?.startsWith("unavailable")) - ) { - // Screenshot/PDF - await page.evaluate( - autoScroll, - Number(process.env.AUTOSCROLL_TIMEOUT) || 30 - ); + // apply administrator's defined pdf margins or default to 15px + const margins = { + top: process.env.PDF_MARGIN_TOP || "15px", + bottom: process.env.PDF_MARGIN_BOTTOM || "15px", + }; - // Check if the user hasn't deleted the link by the time we're done scrolling - const linkExists = await prisma.link.findUnique({ - where: { id: link.id }, - }); - if (linkExists) { - const processingPromises = []; - - if ( - user.archiveAsScreenshot && - !link.image?.startsWith("archive") - ) { - processingPromises.push( - page.screenshot({ fullPage: true }).then((screenshot) => { + if (user.archiveAsPDF && !link.pdf?.startsWith("archive")) { + processingPromises.push( + page + .pdf({ + width: "1366px", + height: "1931px", + printBackground: true, + margin: margins, + }) + .then((pdf) => { return createFile({ - data: screenshot, - filePath: `archives/${linkExists.collectionId}/${link.id}.png`, + data: pdf, + filePath: `archives/${linkExists.collectionId}/${link.id}.pdf`, }); }) - ); - } - - // apply administrator's defined pdf margins or default to 15px - const margins = { - top: process.env.PDF_MARGIN_TOP || "15px", - bottom: process.env.PDF_MARGIN_BOTTOM || "15px", - }; - - if (user.archiveAsPDF && !link.pdf?.startsWith("archive")) { - processingPromises.push( - page - .pdf({ - width: "1366px", - height: "1931px", - printBackground: true, - margin: margins, - }) - .then((pdf) => { - return createFile({ - data: pdf, - filePath: `archives/${linkExists.collectionId}/${link.id}.pdf`, - }); - }) - ); - } - await Promise.allSettled(processingPromises); - await prisma.link.update({ - where: { id: link.id }, - data: { - image: user.archiveAsScreenshot - ? `archives/${linkExists.collectionId}/${link.id}.png` - : undefined, - pdf: user.archiveAsPDF - ? `archives/${linkExists.collectionId}/${link.id}.pdf` - : undefined, - }, - }); + ); } + await Promise.allSettled(processingPromises); + await prisma.link.update({ + where: { id: link.id }, + data: { + image: user.archiveAsScreenshot + ? `archives/${linkExists.collectionId}/${link.id}.png` + : undefined, + pdf: user.archiveAsPDF + ? `archives/${linkExists.collectionId}/${link.id}.pdf` + : undefined, + }, + }); } } })(), diff --git a/lib/api/manageLinkFiles.ts b/lib/api/manageLinkFiles.ts index 7bacdab..7498772 100644 --- a/lib/api/manageLinkFiles.ts +++ b/lib/api/manageLinkFiles.ts @@ -16,6 +16,10 @@ const removeFiles = async (linkId: number, collectionId: number) => { await removeFile({ filePath: `archives/${collectionId}/${linkId}.jpg`, }); + // HTML + await removeFile({ + filePath: `archives/${collectionId}/${linkId}.html`, + }); // Preview await removeFile({ filePath: `archives/preview/${collectionId}/${linkId}.jpeg`, @@ -47,6 +51,11 @@ const moveFiles = async (linkId: number, from: number, to: number) => { `archives/${to}/${linkId}.jpg` ); + await moveFile( + `archives/${from}/${linkId}.html`, + `archives/${to}/${linkId}.html` + ); + await moveFile( `archives/preview/${from}/${linkId}.jpeg`, `archives/preview/${to}/${linkId}.jpeg`