cleaner code
This commit is contained in:
parent
39422e54df
commit
8a901ba0e9
|
@ -1,6 +1,6 @@
|
||||||
import { prisma } from "@/lib/api/db";
|
import { prisma } from "@/lib/api/db";
|
||||||
import getPermission from "@/lib/api/getPermission";
|
import getPermission from "@/lib/api/getPermission";
|
||||||
import { Collection, UsersAndCollections } from "@prisma/client";
|
import { UsersAndCollections } from "@prisma/client";
|
||||||
import removeFolder from "@/lib/api/storage/removeFolder";
|
import removeFolder from "@/lib/api/storage/removeFolder";
|
||||||
|
|
||||||
export default async function deleteCollection(
|
export default async function deleteCollection(
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
import { prisma } from "@/lib/api/db";
|
import { prisma } from "@/lib/api/db";
|
||||||
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
||||||
import getTitle from "@/lib/shared/getTitle";
|
import getTitle from "@/lib/shared/getTitle";
|
||||||
import { UsersAndCollections } from "@prisma/client";
|
|
||||||
import getPermission from "@/lib/api/getPermission";
|
|
||||||
import createFolder from "@/lib/api/storage/createFolder";
|
import createFolder from "@/lib/api/storage/createFolder";
|
||||||
import validateUrlSize from "../../validateUrlSize";
|
import validateUrlSize from "../../validateUrlSize";
|
||||||
|
import setLinkCollection from "../../setLinkCollection";
|
||||||
|
|
||||||
const MAX_LINKS_PER_USER = Number(process.env.MAX_LINKS_PER_USER) || 30000;
|
const MAX_LINKS_PER_USER = Number(process.env.MAX_LINKS_PER_USER) || 30000;
|
||||||
|
|
||||||
|
@ -24,93 +23,10 @@ export default async function postLink(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!link.collection.id && link.collection.name) {
|
const linkCollection = await setLinkCollection(link, userId);
|
||||||
link.collection.name = link.collection.name.trim();
|
|
||||||
|
|
||||||
// find the collection with the name and the user's id
|
if (!linkCollection)
|
||||||
const findCollection = await prisma.collection.findFirst({
|
return { response: "Collection is not accessible.", status: 400 };
|
||||||
where: {
|
|
||||||
name: link.collection.name,
|
|
||||||
ownerId: userId,
|
|
||||||
parentId: link.collection.parentId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (findCollection) {
|
|
||||||
const collectionIsAccessible = await getPermission({
|
|
||||||
userId,
|
|
||||||
collectionId: findCollection.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
const memberHasAccess = collectionIsAccessible?.members.some(
|
|
||||||
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
|
||||||
return { response: "Collection is not accessible.", status: 401 };
|
|
||||||
|
|
||||||
link.collection.id = findCollection.id;
|
|
||||||
link.collection.ownerId = findCollection.ownerId;
|
|
||||||
} else {
|
|
||||||
const collection = await prisma.collection.create({
|
|
||||||
data: {
|
|
||||||
name: link.collection.name,
|
|
||||||
ownerId: userId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
link.collection.id = collection.id;
|
|
||||||
|
|
||||||
await prisma.user.update({
|
|
||||||
where: {
|
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
collectionOrder: {
|
|
||||||
push: link.collection.id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else if (link.collection.id) {
|
|
||||||
const collectionIsAccessible = await getPermission({
|
|
||||||
userId,
|
|
||||||
collectionId: link.collection.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
const memberHasAccess = collectionIsAccessible?.members.some(
|
|
||||||
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
|
||||||
return { response: "Collection is not accessible.", status: 401 };
|
|
||||||
} else if (!link.collection.id) {
|
|
||||||
link.collection.name = "Unorganized";
|
|
||||||
link.collection.parentId = null;
|
|
||||||
|
|
||||||
// find the collection with the name "Unorganized" and the user's id
|
|
||||||
const unorganizedCollection = await prisma.collection.findFirst({
|
|
||||||
where: {
|
|
||||||
name: "Unorganized",
|
|
||||||
ownerId: userId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
link.collection.id = unorganizedCollection?.id;
|
|
||||||
|
|
||||||
await prisma.user.update({
|
|
||||||
where: {
|
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
collectionOrder: {
|
|
||||||
push: link.collection.id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return { response: "Uncaught error.", status: 500 };
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
where: {
|
where: {
|
||||||
|
@ -124,8 +40,6 @@ export default async function postLink(
|
||||||
const urlWithoutWww = hasWwwPrefix ? url?.replace(`://www.`, "://") : url;
|
const urlWithoutWww = hasWwwPrefix ? url?.replace(`://www.`, "://") : url;
|
||||||
const urlWithWww = hasWwwPrefix ? url : url?.replace("://", `://www.`);
|
const urlWithWww = hasWwwPrefix ? url : url?.replace("://", `://www.`);
|
||||||
|
|
||||||
console.log(url, urlWithoutWww, urlWithWww);
|
|
||||||
|
|
||||||
const existingLink = await prisma.link.findFirst({
|
const existingLink = await prisma.link.findFirst({
|
||||||
where: {
|
where: {
|
||||||
OR: [{ url: urlWithWww }, { url: urlWithoutWww }],
|
OR: [{ url: urlWithWww }, { url: urlWithoutWww }],
|
||||||
|
@ -135,8 +49,6 @@ export default async function postLink(
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(url, urlWithoutWww, urlWithWww, "DONE!");
|
|
||||||
|
|
||||||
if (existingLink)
|
if (existingLink)
|
||||||
return {
|
return {
|
||||||
response: "Link already exists",
|
response: "Link already exists",
|
||||||
|
@ -147,19 +59,17 @@ export default async function postLink(
|
||||||
const numberOfLinksTheUserHas = await prisma.link.count({
|
const numberOfLinksTheUserHas = await prisma.link.count({
|
||||||
where: {
|
where: {
|
||||||
collection: {
|
collection: {
|
||||||
ownerId: userId,
|
ownerId: linkCollection.ownerId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
if (numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
||||||
return {
|
return {
|
||||||
response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
response: `Each collection owner can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
||||||
status: 400,
|
status: 400,
|
||||||
};
|
};
|
||||||
|
|
||||||
link.collection.name = link.collection.name.trim();
|
|
||||||
|
|
||||||
const title =
|
const title =
|
||||||
!(link.name && link.name !== "") && link.url
|
!(link.name && link.name !== "") && link.url
|
||||||
? await getTitle(link.url)
|
? await getTitle(link.url)
|
||||||
|
@ -190,7 +100,7 @@ export default async function postLink(
|
||||||
type: linkType,
|
type: linkType,
|
||||||
collection: {
|
collection: {
|
||||||
connect: {
|
connect: {
|
||||||
id: link.collection.id,
|
id: linkCollection.id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
tags: {
|
tags: {
|
||||||
|
@ -198,14 +108,14 @@ export default async function postLink(
|
||||||
where: {
|
where: {
|
||||||
name_ownerId: {
|
name_ownerId: {
|
||||||
name: tag.name.trim(),
|
name: tag.name.trim(),
|
||||||
ownerId: link.collection.ownerId,
|
ownerId: linkCollection.ownerId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
create: {
|
create: {
|
||||||
name: tag.name.trim(),
|
name: tag.name.trim(),
|
||||||
owner: {
|
owner: {
|
||||||
connect: {
|
connect: {
|
||||||
id: link.collection.ownerId,
|
id: linkCollection.ownerId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -31,7 +31,7 @@ export default async function importFromHTMLFile(
|
||||||
|
|
||||||
if (totalImports + numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
if (totalImports + numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
||||||
return {
|
return {
|
||||||
response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
response: `Each collection owner can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
||||||
status: 400,
|
status: 400,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ export default async function importFromLinkwarden(
|
||||||
|
|
||||||
if (totalImports + numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
if (totalImports + numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
||||||
return {
|
return {
|
||||||
response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
response: `Each collection owner can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
||||||
status: 400,
|
status: 400,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ export default async function importFromWallabag(
|
||||||
|
|
||||||
if (totalImports + numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
if (totalImports + numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
||||||
return {
|
return {
|
||||||
response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
response: `Each collection owner can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
||||||
status: 400,
|
status: 400,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,12 @@ import { prisma } from "@/lib/api/db";
|
||||||
type Props = {
|
type Props = {
|
||||||
userId: number;
|
userId: number;
|
||||||
collectionId?: number;
|
collectionId?: number;
|
||||||
collectionName?: string;
|
|
||||||
linkId?: number;
|
linkId?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function getPermission({
|
export default async function getPermission({
|
||||||
userId,
|
userId,
|
||||||
collectionId,
|
collectionId,
|
||||||
collectionName,
|
|
||||||
linkId,
|
linkId,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
if (linkId) {
|
if (linkId) {
|
||||||
|
@ -26,11 +24,10 @@ export default async function getPermission({
|
||||||
});
|
});
|
||||||
|
|
||||||
return check;
|
return check;
|
||||||
} else if (collectionId || collectionName) {
|
} else if (collectionId) {
|
||||||
const check = await prisma.collection.findFirst({
|
const check = await prisma.collection.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: collectionId || undefined,
|
id: collectionId,
|
||||||
name: collectionName || undefined,
|
|
||||||
OR: [{ ownerId: userId }, { members: { some: { userId } } }],
|
OR: [{ ownerId: userId }, { members: { some: { userId } } }],
|
||||||
},
|
},
|
||||||
include: { members: true },
|
include: { members: true },
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
||||||
|
import { prisma } from "./db";
|
||||||
|
import getPermission from "./getPermission";
|
||||||
|
import { UsersAndCollections } from "@prisma/client";
|
||||||
|
|
||||||
|
const setLinkCollection = async (
|
||||||
|
link: LinkIncludingShortenedCollectionAndTags,
|
||||||
|
userId: number
|
||||||
|
) => {
|
||||||
|
if (link?.collection?.id && typeof link?.collection?.id === "number") {
|
||||||
|
const existingCollection = await prisma.collection.findUnique({
|
||||||
|
where: {
|
||||||
|
id: link.collection.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!existingCollection) return null;
|
||||||
|
|
||||||
|
const collectionIsAccessible = await getPermission({
|
||||||
|
userId,
|
||||||
|
collectionId: existingCollection.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
||||||
|
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return existingCollection;
|
||||||
|
} else if (link?.collection?.name) {
|
||||||
|
if (link.collection.name === "Unorganized") {
|
||||||
|
const firstTopLevelUnorganizedCollection =
|
||||||
|
await prisma.collection.findFirst({
|
||||||
|
where: {
|
||||||
|
name: "Unorganized",
|
||||||
|
ownerId: userId,
|
||||||
|
parentId: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (firstTopLevelUnorganizedCollection)
|
||||||
|
return firstTopLevelUnorganizedCollection;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newCollection = await prisma.collection.create({
|
||||||
|
data: {
|
||||||
|
name: link.collection.name.trim(),
|
||||||
|
ownerId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.user.update({
|
||||||
|
where: {
|
||||||
|
id: userId,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
collectionOrder: {
|
||||||
|
push: newCollection.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return newCollection;
|
||||||
|
} else {
|
||||||
|
const firstTopLevelUnorganizedCollection =
|
||||||
|
await prisma.collection.findFirst({
|
||||||
|
where: {
|
||||||
|
name: "Unorganized",
|
||||||
|
ownerId: userId,
|
||||||
|
parentId: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (firstTopLevelUnorganizedCollection)
|
||||||
|
return firstTopLevelUnorganizedCollection;
|
||||||
|
else
|
||||||
|
return await prisma.collection.create({
|
||||||
|
data: {
|
||||||
|
name: "Unorganized",
|
||||||
|
ownerId: userId,
|
||||||
|
parentId: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default setLinkCollection;
|
|
@ -85,12 +85,15 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||||
linkId,
|
linkId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const memberHasAccess = collectionPermissions?.members.some(
|
if (!collectionPermissions)
|
||||||
|
return { response: "Collection is not accessible.", status: 400 };
|
||||||
|
|
||||||
|
const memberHasAccess = collectionPermissions.members.some(
|
||||||
(e: UsersAndCollections) => e.userId === user.id && e.canCreate
|
(e: UsersAndCollections) => e.userId === user.id && e.canCreate
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!(collectionPermissions?.ownerId === user.id || memberHasAccess))
|
if (!(collectionPermissions.ownerId === user.id || memberHasAccess))
|
||||||
return { response: "Collection is not accessible.", status: 401 };
|
return { response: "Collection is not accessible.", status: 400 };
|
||||||
|
|
||||||
// await uploadHandler(linkId, )
|
// await uploadHandler(linkId, )
|
||||||
|
|
||||||
|
@ -106,18 +109,18 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
if (numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
if (numberOfLinksTheUserHas > MAX_LINKS_PER_USER)
|
||||||
return {
|
return {
|
||||||
response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
response: `Each collection owner can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
||||||
status: 400,
|
status: 400,
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAX_UPLOAD_SIZE = Number(
|
const NEXT_PUBLIC_MAX_FILE_BUFFER = Number(
|
||||||
process.env.NEXT_PUBLIC_MAX_FILE_BUFFER || 10
|
process.env.NEXT_PUBLIC_MAX_FILE_BUFFER || 10
|
||||||
);
|
);
|
||||||
|
|
||||||
const form = formidable({
|
const form = formidable({
|
||||||
maxFields: 1,
|
maxFields: 1,
|
||||||
maxFiles: 1,
|
maxFiles: 1,
|
||||||
maxFileSize: MAX_UPLOAD_SIZE * 1048576,
|
maxFileSize: NEXT_PUBLIC_MAX_FILE_BUFFER * 1048576,
|
||||||
});
|
});
|
||||||
|
|
||||||
form.parse(req, async (err, fields, files) => {
|
form.parse(req, async (err, fields, files) => {
|
||||||
|
@ -136,7 +139,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||||
) {
|
) {
|
||||||
// Handle parsing error
|
// Handle parsing error
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
response: `Sorry, we couldn't process your file. Please ensure it's a PDF, PNG, or JPG format and doesn't exceed ${MAX_UPLOAD_SIZE}MB.`,
|
response: `Sorry, we couldn't process your file. Please ensure it's a PDF, PNG, or JPG format and doesn't exceed ${NEXT_PUBLIC_MAX_FILE_BUFFER}MB.`,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const fileBuffer = fs.readFileSync(files.file[0].filepath);
|
const fileBuffer = fs.readFileSync(files.file[0].filepath);
|
||||||
|
@ -146,7 +149,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (linkStillExists && files.file[0].mimetype?.includes("image")) {
|
if (linkStillExists && files.file[0].mimetype?.includes("image")) {
|
||||||
const collectionId = collectionPermissions?.id as number;
|
const collectionId = collectionPermissions.id as number;
|
||||||
createFolder({
|
createFolder({
|
||||||
filePath: `archives/preview/${collectionId}`,
|
filePath: `archives/preview/${collectionId}`,
|
||||||
});
|
});
|
||||||
|
@ -156,9 +159,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
if (linkStillExists) {
|
if (linkStillExists) {
|
||||||
await createFile({
|
await createFile({
|
||||||
filePath: `archives/${collectionPermissions?.id}/${
|
filePath: `archives/${collectionPermissions.id}/${linkId + suffix}`,
|
||||||
linkId + suffix
|
|
||||||
}`,
|
|
||||||
data: fileBuffer,
|
data: fileBuffer,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -169,10 +170,10 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||||
? "unavailable"
|
? "unavailable"
|
||||||
: undefined,
|
: undefined,
|
||||||
image: files.file[0].mimetype?.includes("image")
|
image: files.file[0].mimetype?.includes("image")
|
||||||
? `archives/${collectionPermissions?.id}/${linkId + suffix}`
|
? `archives/${collectionPermissions.id}/${linkId + suffix}`
|
||||||
: null,
|
: null,
|
||||||
pdf: files.file[0].mimetype?.includes("pdf")
|
pdf: files.file[0].mimetype?.includes("pdf")
|
||||||
? `archives/${collectionPermissions?.id}/${linkId + suffix}`
|
? `archives/${collectionPermissions.id}/${linkId + suffix}`
|
||||||
: null,
|
: null,
|
||||||
lastPreserved: new Date().toISOString(),
|
lastPreserved: new Date().toISOString(),
|
||||||
},
|
},
|
||||||
|
|
Ŝarĝante…
Reference in New Issue