small changes

This commit is contained in:
daniel31x13 2024-06-18 12:27:29 -04:00
parent 359d22e61b
commit c68f9d68ad
3 changed files with 99 additions and 133 deletions

View File

@ -5,7 +5,10 @@ import TextInput from "@/components/TextInput";
import unescapeString from "@/lib/client/unescapeString"; import unescapeString from "@/lib/client/unescapeString";
import useCollectionStore from "@/store/collections"; import useCollectionStore from "@/store/collections";
import useLinkStore from "@/store/links"; import useLinkStore from "@/store/links";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; import {
LinkIncludingShortenedCollectionAndTags,
ArchivedFormat,
} from "@/types/global";
import { useSession } from "next-auth/react"; import { useSession } from "next-auth/react";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
@ -110,12 +113,8 @@ export default function UploadFileModal({ onClose }: Props) {
linkType = "pdf"; linkType = "pdf";
} }
if (fileType !== null && linkType !== null) { setSubmitLoader(true);
setSubmitLoader(true); const load = toast.loading(t("creating"));
let response;
const load = toast.loading(t("creating"));
const response = await uploadFile(link, file); const response = await uploadFile(link, file);
@ -150,7 +149,7 @@ export default function UploadFileModal({ onClose }: Props) {
/> />
</label> </label>
<p className="text-xs font-semibold mt-2"> <p className="text-xs font-semibold mt-2">
{t("file_types", { {t("file_types", {
size: process.env.NEXT_PUBLIC_MAX_FILE_SIZE || 30, size: process.env.NEXT_PUBLIC_MAX_FILE_SIZE || 30,
})} })}
</p> </p>

View File

@ -50,18 +50,6 @@ export default async function archiveHandler(link: LinksAndCollectionAndOwner) {
const page = await context.newPage(); 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({ createFolder({
filePath: `archives/preview/${link.collectionId}`, filePath: `archives/preview/${link.collectionId}`,
}); });
@ -168,138 +156,108 @@ export default async function archiveHandler(link: LinksAndCollectionAndOwner) {
return metaTag ? (metaTag as any).content : null; return metaTag ? (metaTag as any).content : null;
}); });
if (ogImageUrl) { if (ogImageUrl) {
console.log("Found og:image URL:", ogImageUrl); console.log("Found og:image URL:", ogImageUrl);
// Download the image // Download the image
const imageResponse = await page.goto(ogImageUrl); const imageResponse = await page.goto(ogImageUrl);
// Check if imageResponse is not null // Check if imageResponse is not null
if (imageResponse && !link.preview?.startsWith("archive")) { if (imageResponse && !link.preview?.startsWith("archive")) {
const buffer = await imageResponse.body(); const buffer = await imageResponse.body();
await generatePreview(buffer, link.collectionId, link.id); 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(); await page.goBack();
} else if (!link.preview?.startsWith("archive")) { } else if (!link.preview?.startsWith("archive")) {
console.log("No og:image found"); console.log("No og:image found");
await page await page
.screenshot({ type: "jpeg", quality: 20 }) .screenshot({ type: "jpeg", quality: 20 })
.then((screenshot) => { .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({ return createFile({
data: screenshot, 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 ( // apply administrator's defined pdf margins or default to 15px
(!link.image?.startsWith("archives") && const margins = {
!link.image?.startsWith("unavailable")) || top: process.env.PDF_MARGIN_TOP || "15px",
(!link.pdf?.startsWith("archives") && bottom: process.env.PDF_MARGIN_BOTTOM || "15px",
!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 if (user.archiveAsPDF && !link.pdf?.startsWith("archive")) {
const linkExists = await prisma.link.findUnique({ processingPromises.push(
where: { id: link.id }, page
}); .pdf({
if (linkExists) { width: "1366px",
const processingPromises = []; height: "1931px",
printBackground: true,
if ( margin: margins,
user.archiveAsScreenshot && })
!link.image?.startsWith("archive") .then((pdf) => {
) {
processingPromises.push(
page.screenshot({ fullPage: true }).then((screenshot) => {
return createFile({ return createFile({
data: screenshot, data: pdf,
filePath: `archives/${linkExists.collectionId}/${link.id}.png`, 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,
},
});
} }
} }
})(), })(),

View File

@ -16,6 +16,10 @@ const removeFiles = async (linkId: number, collectionId: number) => {
await removeFile({ await removeFile({
filePath: `archives/${collectionId}/${linkId}.jpg`, filePath: `archives/${collectionId}/${linkId}.jpg`,
}); });
// HTML
await removeFile({
filePath: `archives/${collectionId}/${linkId}.html`,
});
// Preview // Preview
await removeFile({ await removeFile({
filePath: `archives/preview/${collectionId}/${linkId}.jpeg`, filePath: `archives/preview/${collectionId}/${linkId}.jpeg`,
@ -47,6 +51,11 @@ const moveFiles = async (linkId: number, from: number, to: number) => {
`archives/${to}/${linkId}.jpg` `archives/${to}/${linkId}.jpg`
); );
await moveFile(
`archives/${from}/${linkId}.html`,
`archives/${to}/${linkId}.html`
);
await moveFile( await moveFile(
`archives/preview/${from}/${linkId}.jpeg`, `archives/preview/${from}/${linkId}.jpeg`,
`archives/preview/${to}/${linkId}.jpeg` `archives/preview/${to}/${linkId}.jpeg`