2023-02-24 11:32:28 -06:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
import { Session } from "next-auth";
|
2023-03-05 15:03:20 -06:00
|
|
|
import { LinkAndTags, NewLink } from "@/types/global";
|
2023-03-08 15:31:24 -06:00
|
|
|
import { existsSync, mkdirSync } from "fs";
|
|
|
|
import getTitle from "../../getTitle";
|
|
|
|
import archive from "../../archive";
|
|
|
|
import { Link } from "@prisma/client";
|
|
|
|
import AES from "crypto-js/aes";
|
2023-02-24 11:32:28 -06:00
|
|
|
|
|
|
|
export default async function (
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse,
|
|
|
|
session: Session
|
|
|
|
) {
|
|
|
|
if (!session?.user?.email) {
|
|
|
|
return res.status(401).json({ response: "You must be logged in." });
|
|
|
|
}
|
|
|
|
|
|
|
|
const email: string = session.user.email;
|
2023-03-05 15:03:20 -06:00
|
|
|
const link: NewLink = req?.body;
|
2023-02-24 11:32:28 -06:00
|
|
|
|
|
|
|
if (!link.name) {
|
|
|
|
return res
|
|
|
|
.status(401)
|
|
|
|
.json({ response: "Please enter a valid name for the link." });
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
if (link.collection.isNew) {
|
|
|
|
const collectionId = link.collection.id as string;
|
2023-02-24 11:32:28 -06:00
|
|
|
|
|
|
|
const findCollection = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
collections: {
|
|
|
|
where: {
|
|
|
|
name: collectionId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const checkIfCollectionExists = findCollection?.collections[0];
|
|
|
|
|
|
|
|
if (checkIfCollectionExists) {
|
|
|
|
return res.status(400).json({ response: "Collection already exists." });
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const newCollection = await prisma.collection.create({
|
2023-02-24 11:32:28 -06:00
|
|
|
data: {
|
|
|
|
owner: {
|
|
|
|
connect: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: collectionId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const collectionPath = `data/archives/${newCollection.id}`;
|
|
|
|
if (!existsSync(collectionPath))
|
|
|
|
mkdirSync(collectionPath, { recursive: true });
|
|
|
|
|
|
|
|
link.collection.id = newCollection.id;
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const collectionId = link.collection.id as number;
|
|
|
|
|
|
|
|
const title = await getTitle(link.url);
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const newLink: Link = await prisma.link.create({
|
2023-02-24 11:32:28 -06:00
|
|
|
data: {
|
|
|
|
name: link.name,
|
2023-03-05 15:03:20 -06:00
|
|
|
url: link.url,
|
2023-02-24 11:32:28 -06:00
|
|
|
collection: {
|
|
|
|
connect: {
|
|
|
|
id: collectionId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
connectOrCreate: link.tags.map((name) => ({
|
|
|
|
where: {
|
|
|
|
name_collectionId: {
|
|
|
|
name,
|
|
|
|
collectionId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
create: {
|
|
|
|
name,
|
|
|
|
collections: {
|
|
|
|
connect: {
|
|
|
|
id: collectionId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
},
|
2023-03-08 15:31:24 -06:00
|
|
|
title,
|
|
|
|
isFavorites: false,
|
|
|
|
screenshotPath: "",
|
|
|
|
pdfPath: "",
|
2023-02-24 11:32:28 -06:00
|
|
|
},
|
2023-03-08 15:31:24 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
const AES_SECRET = process.env.AES_SECRET as string;
|
|
|
|
|
|
|
|
const screenShotHashedPath = AES.encrypt(
|
|
|
|
`data/archives/${newLink.collectionId}/${newLink.id}.png`,
|
|
|
|
AES_SECRET
|
|
|
|
).toString();
|
|
|
|
|
|
|
|
const pdfHashedPath = AES.encrypt(
|
|
|
|
`data/archives/${newLink.collectionId}/${newLink.id}.pdf`,
|
|
|
|
AES_SECRET
|
|
|
|
).toString();
|
|
|
|
|
|
|
|
const updatedLink: LinkAndTags = await prisma.link.update({
|
|
|
|
where: { id: newLink.id },
|
|
|
|
data: { screenshotPath: screenShotHashedPath, pdfPath: pdfHashedPath },
|
2023-03-05 15:03:20 -06:00
|
|
|
include: { tags: true },
|
2023-02-24 11:32:28 -06:00
|
|
|
});
|
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
archive(updatedLink.url, updatedLink.collectionId, updatedLink.id);
|
|
|
|
|
2023-02-24 11:32:28 -06:00
|
|
|
return res.status(200).json({
|
2023-03-08 15:31:24 -06:00
|
|
|
response: updatedLink,
|
2023-02-24 11:32:28 -06:00
|
|
|
});
|
|
|
|
}
|