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

115 lines
3.1 KiB
TypeScript
Raw Normal View History

import { prisma } from "@/lib/api/db";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
2023-12-06 15:13:11 -06:00
import getTitle from "@/lib/shared/getTitle";
2023-11-19 15:22:27 -06:00
import { UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
2023-07-18 10:59:57 -05:00
import createFolder from "@/lib/api/storage/createFolder";
2023-11-25 02:19:02 -06:00
import validateUrlSize from "../../validateUrlSize";
export default async function postLink(
link: LinkIncludingShortenedCollectionAndTags,
2023-05-27 11:29:39 -05:00
userId: number
) {
2023-07-24 08:39:51 -05:00
try {
2023-12-07 11:29:45 -06:00
new URL(link.url || "");
2023-07-24 08:39:51 -05:00
} catch (error) {
return {
response:
"Please enter a valid Address for the Link. (It should start with http/https)",
status: 400,
};
}
if (!link.collection.name) {
2023-08-21 23:43:34 -05:00
link.collection.name = "Unorganized";
}
link.collection.name = link.collection.name.trim();
2023-05-27 11:29:39 -05:00
if (link.collection.id) {
2023-11-19 15:22:27 -06:00
const collectionIsAccessible = await getPermission({
2023-03-28 02:31:50 -05:00
userId,
collectionId: link.collection.id,
2023-11-19 15:22:27 -06:00
});
2023-03-28 02:31:50 -05:00
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canCreate
);
2023-03-28 02:31:50 -05:00
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
return { response: "Collection is not accessible.", status: 401 };
} else {
link.collection.ownerId = userId;
}
const description =
link.description && link.description !== ""
? link.description
2023-11-25 02:19:02 -06:00
: link.url
? await getTitle(link.url)
: undefined;
const validatedUrl = link.url ? await validateUrlSize(link.url) : undefined;
if (validatedUrl === null)
return { response: "File is too large to be stored.", status: 400 };
const contentType = validatedUrl?.get("content-type");
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";
}
const newLink = await prisma.link.create({
data: {
2023-03-05 15:03:20 -06:00
url: link.url,
2023-07-24 08:39:51 -05:00
name: link.name,
description,
2023-11-25 02:19:02 -06:00
type: linkType,
collection: {
2023-03-28 02:31:50 -05:00
connectOrCreate: {
where: {
name_ownerId: {
ownerId: link.collection.ownerId,
name: link.collection.name,
},
},
create: {
2023-07-23 13:27:12 -05:00
name: link.collection.name.trim(),
2023-03-28 02:31:50 -05:00
ownerId: userId,
},
},
},
tags: {
2023-03-28 02:31:50 -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(),
2023-03-28 02:31:50 -05:00
ownerId: link.collection.ownerId,
},
},
create: {
2023-07-24 08:39:51 -05:00
name: tag.name.trim(),
2023-03-28 02:31:50 -05:00
owner: {
connect: {
2023-03-28 02:31:50 -05:00
id: link.collection.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 };
}