2023-10-22 23:28:39 -05:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2023-10-28 04:57:53 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-11-06 07:25:57 -06:00
|
|
|
import verifyUser from "@/lib/api/verifyUser";
|
2023-11-25 02:27:34 -06:00
|
|
|
import isValidUrl from "@/lib/shared/isValidUrl";
|
2023-12-19 10:50:43 -06:00
|
|
|
import { Collection, Link } from "@prisma/client";
|
2024-04-08 18:35:06 -05:00
|
|
|
import { removeFiles } from "@/lib/api/manageLinkFiles";
|
2023-10-22 23:28:39 -05:00
|
|
|
|
2023-10-31 14:44:58 -05:00
|
|
|
const RE_ARCHIVE_LIMIT = Number(process.env.RE_ARCHIVE_LIMIT) || 5;
|
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
2023-11-06 07:25:57 -06:00
|
|
|
const user = await verifyUser({ req, res });
|
|
|
|
if (!user) return;
|
2023-10-22 23:28:39 -05:00
|
|
|
|
2023-10-28 04:57:53 -05:00
|
|
|
const link = await prisma.link.findUnique({
|
|
|
|
where: {
|
|
|
|
id: Number(req.query.id),
|
|
|
|
},
|
2023-12-17 16:28:42 -06:00
|
|
|
include: { collection: { include: { owner: true } } },
|
2023-10-28 04:57:53 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!link)
|
|
|
|
return res.status(404).json({
|
|
|
|
response: "Link not found.",
|
2023-10-22 23:28:39 -05:00
|
|
|
});
|
2023-10-28 04:57:53 -05:00
|
|
|
|
2023-11-02 13:59:31 -05:00
|
|
|
if (link.collection.ownerId !== user.id)
|
2023-10-28 04:57:53 -05:00
|
|
|
return res.status(401).json({
|
|
|
|
response: "Permission denied.",
|
|
|
|
});
|
|
|
|
|
|
|
|
if (req.method === "PUT") {
|
2024-07-18 15:29:59 -05:00
|
|
|
if (process.env.NEXT_PUBLIC_DEMO === "true")
|
2024-07-16 19:33:33 -05:00
|
|
|
return res.status(400).json({
|
|
|
|
response:
|
|
|
|
"This action is disabled because this is a read-only demo of Linkwarden.",
|
|
|
|
});
|
|
|
|
|
2023-10-31 14:44:58 -05:00
|
|
|
if (
|
|
|
|
link?.lastPreserved &&
|
|
|
|
getTimezoneDifferenceInMinutes(new Date(), link?.lastPreserved) <
|
|
|
|
RE_ARCHIVE_LIMIT
|
|
|
|
)
|
|
|
|
return res.status(400).json({
|
|
|
|
response: `This link is currently being saved or has already been preserved. Please retry in ${
|
|
|
|
RE_ARCHIVE_LIMIT -
|
|
|
|
Math.floor(
|
|
|
|
getTimezoneDifferenceInMinutes(new Date(), link?.lastPreserved)
|
|
|
|
)
|
|
|
|
} minutes or create a new one.`,
|
|
|
|
});
|
|
|
|
|
2023-12-19 10:50:43 -06:00
|
|
|
if (!link.url || !isValidUrl(link.url))
|
2023-11-25 02:27:34 -06:00
|
|
|
return res.status(200).json({
|
2023-12-19 10:50:43 -06:00
|
|
|
response: "Invalid URL.",
|
2023-11-25 02:27:34 -06:00
|
|
|
});
|
2023-12-19 10:50:43 -06:00
|
|
|
|
|
|
|
await deleteArchivedFiles(link);
|
2023-11-25 02:27:34 -06:00
|
|
|
|
2023-10-28 04:57:53 -05:00
|
|
|
return res.status(200).json({
|
|
|
|
response: "Link is being archived.",
|
2023-10-22 23:28:39 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 14:44:58 -05:00
|
|
|
|
|
|
|
const getTimezoneDifferenceInMinutes = (future: Date, past: Date) => {
|
|
|
|
const date1 = new Date(future);
|
|
|
|
const date2 = new Date(past);
|
|
|
|
|
|
|
|
const diffInMilliseconds = Math.abs(date1.getTime() - date2.getTime());
|
|
|
|
|
|
|
|
const diffInMinutes = diffInMilliseconds / (1000 * 60);
|
|
|
|
|
|
|
|
return diffInMinutes;
|
|
|
|
};
|
2023-12-19 10:50:43 -06:00
|
|
|
|
|
|
|
const deleteArchivedFiles = async (link: Link & { collection: Collection }) => {
|
|
|
|
await prisma.link.update({
|
|
|
|
where: {
|
|
|
|
id: link.id,
|
|
|
|
},
|
|
|
|
data: {
|
2023-12-22 12:13:43 -06:00
|
|
|
image: null,
|
|
|
|
pdf: null,
|
|
|
|
readable: null,
|
2024-06-27 20:58:07 -05:00
|
|
|
monolith: null,
|
2023-12-23 11:11:47 -06:00
|
|
|
preview: null,
|
2023-12-19 10:50:43 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-04-08 18:35:06 -05:00
|
|
|
await removeFiles(link.id, link.collection.id);
|
2023-12-19 10:50:43 -06:00
|
|
|
};
|