el.xwx.moe/lib/api/controllers/links/postLink.ts

128 lines
3.4 KiB
TypeScript
Raw Normal View History

import { prisma } from "@/lib/api/db";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
2024-06-29 16:18:38 -05:00
import fetchTitleAndHeaders from "@/lib/shared/fetchTitleAndHeaders";
2023-07-18 10:59:57 -05:00
import createFolder from "@/lib/api/storage/createFolder";
2024-06-28 08:14:09 -05:00
import setLinkCollection from "../../setLinkCollection";
2023-12-19 16:20:09 -06:00
const MAX_LINKS_PER_USER = Number(process.env.MAX_LINKS_PER_USER) || 30000;
export default async function postLink(
link: LinkIncludingShortenedCollectionAndTags,
2023-05-27 11:29:39 -05:00
userId: number
) {
2024-03-20 08:56:14 -05:00
if (link.url || link.type === "url") {
try {
new URL(link.url || "");
} catch (error) {
return {
response:
"Please enter a valid Address for the Link. (It should start with http/https)",
status: 400,
};
}
2023-07-24 08:39:51 -05:00
}
2024-06-28 08:14:09 -05:00
const linkCollection = await setLinkCollection(link, userId);
2024-06-28 08:14:09 -05:00
if (!linkCollection)
return { response: "Collection is not accessible.", status: 400 };
2024-03-04 23:24:30 -06:00
const user = await prisma.user.findUnique({
where: {
id: userId,
},
});
2024-03-05 08:03:04 -06:00
if (user?.preventDuplicateLinks) {
const url = link.url?.trim().replace(/\/+$/, ""); // trim and remove trailing slashes from the URL
const hasWwwPrefix = url?.includes(`://www.`);
const urlWithoutWww = hasWwwPrefix ? url?.replace(`://www.`, "://") : url;
const urlWithWww = hasWwwPrefix ? url : url?.replace("://", `://www.`);
2024-03-05 08:03:04 -06:00
const existingLink = await prisma.link.findFirst({
where: {
OR: [{ url: urlWithWww }, { url: urlWithoutWww }],
2024-03-05 08:03:04 -06:00
collection: {
ownerId: userId,
},
2024-03-04 23:24:30 -06:00
},
2024-03-05 08:03:04 -06:00
});
2024-03-04 23:24:30 -06:00
2024-03-05 08:03:04 -06:00
if (existingLink)
return {
response: "Link already exists",
status: 409,
};
2024-03-04 23:24:30 -06:00
}
2024-02-19 13:37:07 -06:00
const numberOfLinksTheUserHas = await prisma.link.count({
where: {
collection: {
2024-06-28 08:14:09 -05:00
ownerId: linkCollection.ownerId,
2024-02-19 13:37:07 -06:00
},
},
});
2024-06-27 21:23:47 -05:00
if (numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
2024-02-19 13:37:07 -06:00
return {
2024-06-28 08:14:09 -05:00
response: `Each collection owner can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
2024-02-19 13:37:07 -06:00
status: 400,
};
2024-06-29 16:18:38 -05:00
const { title, headers } = await fetchTitleAndHeaders(link.url || "");
const name =
link.name && link.name !== "" ? link.name : link.url ? title : "";
2023-11-25 02:19:02 -06:00
2024-06-29 16:18:38 -05:00
const contentType = headers?.get("content-type");
2023-11-25 02:19:02 -06:00
let linkType = "url";
let imageExtension = "png";
if (!link.url) linkType = link.type;
else if (contentType === "application/pdf") linkType = "pdf";
else if (contentType?.startsWith("image")) {
linkType = "image";
if (contentType === "image/jpeg") imageExtension = "jpeg";
else if (contentType === "image/png") imageExtension = "png";
}
2024-07-25 15:50:38 -05:00
if (!link.tags) link.tags = [];
const newLink = await prisma.link.create({
data: {
2024-08-15 09:30:44 -05:00
url: link.url?.trim() || null,
name,
description: link.description,
2023-11-25 02:19:02 -06:00
type: linkType,
collection: {
2024-02-19 13:37:07 -06:00
connect: {
2024-06-28 08:14:09 -05:00
id: linkCollection.id,
},
},
tags: {
2024-07-25 15:50:38 -05:00
connectOrCreate: link.tags?.map((tag) => ({
where: {
2023-03-28 02:31:50 -05:00
name_ownerId: {
2023-07-24 08:39:51 -05:00
name: tag.name.trim(),
2024-06-28 08:14:09 -05:00
ownerId: linkCollection.ownerId,
},
},
create: {
2023-07-24 08:39:51 -05:00
name: tag.name.trim(),
2023-03-28 02:31:50 -05:00
owner: {
connect: {
2024-06-28 08:14:09 -05:00
id: linkCollection.ownerId,
},
},
},
})),
},
},
2023-03-10 13:25:33 -06:00
include: { tags: true, collection: true },
});
2023-07-18 10:59:57 -05:00
createFolder({ filePath: `archives/${newLink.collectionId}` });
2023-06-13 09:30:52 -05:00
return { response: newLink, status: 200 };
}