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

131 lines
3.4 KiB
TypeScript
Raw Normal View History

import { prisma } from "@/lib/api/db";
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";
2024-09-14 15:00:19 -05:00
import {
PostLinkSchema,
PostLinkSchemaType,
} from "@/lib/shared/schemaValidation";
2024-10-26 12:44:52 -05:00
import { hasPassedLimit } from "../../verifyCapacity";
2023-12-19 16:20:09 -06:00
export default async function postLink(
2024-09-14 15:00:19 -05:00
body: PostLinkSchemaType,
2023-05-27 11:29:39 -05:00
userId: number
) {
2024-09-14 15:00:19 -05:00
const dataValidation = PostLinkSchema.safeParse(body);
if (!dataValidation.success) {
return {
response: `Error: ${
dataValidation.error.issues[0].message
} [${dataValidation.error.issues[0].path.join(", ")}]`,
status: 400,
};
2023-07-24 08:39:51 -05:00
}
2024-09-14 15:00:19 -05:00
const link = dataValidation.data;
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-10-26 12:44:52 -05:00
const hasTooManyLinks = await hasPassedLimit(userId, 1);
2024-02-19 13:37:07 -06:00
2024-10-26 12:44:52 -05:00
if (hasTooManyLinks) {
2024-02-19 13:37:07 -06:00
return {
2024-10-26 12:44:52 -05:00
response: `Your subscription have reached the maximum number of links allowed.`,
2024-02-19 13:37:07 -06:00
status: 400,
};
2024-10-26 12:44:52 -05:00
}
2024-02-19 13:37:07 -06:00
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";
2024-11-12 22:12:20 -06:00
if (!link.url) linkType = link.type || "url";
2023-11-25 02:19:02 -06:00
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,
2024-10-26 12:44:52 -05:00
createdBy: {
connect: {
id: userId,
},
},
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 };
}