Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: move logic to process-document

Marc Seitza4e3bf15f489
diff --git a/lib/api/documents/process-document.ts b/lib/api/documents/process-document.ts
index 89c025d9..a0371c8d 100644
--- a/lib/api/documents/process-document.ts
+++ b/lib/api/documents/process-document.ts
@@ -1,3 +1,4 @@
+import { get } from "@vercel/edge-config";
import { parsePageId } from "notion-utils";
import { DocumentData } from "@/lib/documents/create-document";
@@ -12,7 +13,7 @@ import {
} from "@/lib/trigger/convert-files";
import { processVideo } from "@/lib/trigger/optimize-video-files";
import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";
-import { getExtension } from "@/lib/utils";
+import { getExtension, log } from "@/lib/utils";
import { conversionQueue } from "@/lib/utils/trigger-utils";
import { sendDocumentCreatedWebhook } from "@/lib/webhook/triggers/document-created";
import { sendLinkCreatedWebhook } from "@/lib/webhook/triggers/link-created";
@@ -79,6 +80,22 @@ export const processDocument = async ({
if (type === "link") {
try {
new URL(key);
+
+ const keywords = await get("keywords");
+ if (Array.isArray(keywords) && keywords.length > 0) {
+ const matchedKeyword = keywords.find(
+ (keyword) => typeof keyword === "string" && key.includes(keyword),
+ );
+
+ if (matchedKeyword) {
+ log({
+ message: `Link document creation blocked: ${matchedKeyword} \n\n \`Metadata: {teamId: ${teamId}, url: ${key}}\``,
+ type: "error",
+ mention: true,
+ });
+ throw new Error("This URL is not allowed");
+ }
+ }
} catch (error) {
throw new Error("Invalid URL format for link document.");
}
diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.ts
index 01a0744c..232b7f5c 100644
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@ -3,7 +3,6 @@ import { NextApiRequest, NextApiResponse } from "next";
import { isTeamPausedById } from "@/ee/features/billing/cancellation/lib/is-team-paused";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { Prisma } from "@prisma/client";
-import { get } from "@vercel/edge-config";
import { getServerSession } from "next-auth/next";
import { hashToken } from "@/lib/api/auth/token";
@@ -348,29 +347,6 @@ export default async function handle(
});
}
- // Check if URL contains blocked keywords (for link type documents)
- if (fileType === "link") {
- const keywords = await get("keywords");
- if (Array.isArray(keywords) && keywords.length > 0) {
- const matchedKeyword = keywords.find(
- (keyword) =>
- typeof keyword === "string" && fileUrl.includes(keyword),
- );
-
- if (matchedKeyword) {
- log({
- message: `Link document creation blocked: ${matchedKeyword} \n\n \`Metadata: {teamId: ${teamId}, url: ${fileUrl}}\``,
- type: "error",
- mention: true,
- });
- return res.status(400).json({
- error: "This URL is not allowed",
- matchedKeyword: matchedKeyword,
- });
- }
- }
- }
-
// For link documents, storageType is optional but processDocument requires it
// Use VERCEL_BLOB as a placeholder (not actually used for links)
const finalStorageType =