improve link refresh logic + many changes and improvements

This commit is contained in:
daniel31x13
2023-12-19 11:50:43 -05:00
parent 71b99bb25c
commit b65787358f
29 changed files with 297 additions and 1628 deletions
+29 -8
View File
@@ -1,8 +1,9 @@
import type { NextApiRequest, NextApiResponse } from "next";
import archiveHandler from "@/lib/api/archiveHandler";
import { prisma } from "@/lib/api/db";
import verifyUser from "@/lib/api/verifyUser";
import isValidUrl from "@/lib/shared/isValidUrl";
import removeFile from "@/lib/api/storage/removeFile";
import { Collection, Link } from "@prisma/client";
const RE_ARCHIVE_LIMIT = Number(process.env.RE_ARCHIVE_LIMIT) || 5;
@@ -42,20 +43,17 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) {
} minutes or create a new one.`,
});
if (link.url && isValidUrl(link.url)) {
archiveHandler(link);
if (!link.url || !isValidUrl(link.url))
return res.status(200).json({
response: "Link is not a webpage to be archived.",
response: "Invalid URL.",
});
}
await deleteArchivedFiles(link);
return res.status(200).json({
response: "Link is being archived.",
});
}
// TODO - Later?
// else if (req.method === "DELETE") {}
}
const getTimezoneDifferenceInMinutes = (future: Date, past: Date) => {
@@ -68,3 +66,26 @@ const getTimezoneDifferenceInMinutes = (future: Date, past: Date) => {
return diffInMinutes;
};
const deleteArchivedFiles = async (link: Link & { collection: Collection }) => {
await prisma.link.update({
where: {
id: link.id,
},
data: {
screenshotPath: null,
pdfPath: null,
readabilityPath: null,
},
});
await removeFile({
filePath: `archives/${link.collection.id}/${link.id}.pdf`,
});
await removeFile({
filePath: `archives/${link.collection.id}/${link.id}.png`,
});
await removeFile({
filePath: `archives/${link.collection.id}/${link.id}_readability.json`,
});
};