Skip to content

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

feat: add presetId to incoming webhooks

Marc Seitz010a1a33ea11
diff --git a/lib/utils.ts b/lib/utils.ts
index af8803ca..1110c68b 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -1,3 +1,5 @@
+import { NextRouter } from "next/router";
+
import slugify from "@sindresorhus/slugify";
import { upload } from "@vercel/blob/client";
import { Message } from "ai";
@@ -6,7 +8,6 @@ import { type ClassValue, clsx } from "clsx";
import crypto from "crypto";
import ms from "ms";
import { customAlphabet } from "nanoid";
-import { NextRouter } from "next/router";
import { ThreadMessage } from "openai/resources/beta/threads/messages/messages";
import { rgb } from "pdf-lib";
import { ParsedUrlQuery } from "querystring";
@@ -394,6 +395,10 @@ export function constructMetadata({
};
}
+export const isDataUrl = (str: string): boolean => {
+ return str?.startsWith("data:");
+};
+
export const convertDataUrlToFile = ({
dataUrl,
filename = "logo.png",
@@ -422,6 +427,30 @@ export const convertDataUrlToFile = ({
return new File([u8arr], filename, { type: mime });
};
+export const convertDataUrlToBuffer = (
+ dataUrl: string,
+): { buffer: Buffer; mimeType: string; filename: string } => {
+ // Extract mime type
+ const match = dataUrl.match(/:(.*?);/);
+ const mimeType = match ? match[1] : "";
+
+ // Extract base64 data
+ const base64Data = dataUrl.split(",")[1];
+ const buffer = Buffer.from(base64Data, "base64");
+
+ // Determine filename based on mime type
+ const filename =
+ mimeType === "image/png"
+ ? "image.png"
+ : mimeType === "image/jpeg"
+ ? "image.jpg"
+ : mimeType === "image/x-icon" || mimeType === "image/vnd.microsoft.icon"
+ ? "favicon.ico"
+ : "image";
+
+ return { buffer, mimeType, filename };
+};
+
export const validateImageDimensions = (
image: string,
minSize: number,
@@ -563,15 +592,15 @@ export const getBreadcrumbPath = (path: string[]) => {
};
export const handleInvitationStatus = (
- invitationStatus: 'accepted' | 'teamMember',
+ invitationStatus: "accepted" | "teamMember",
queryParams: ParsedUrlQuery,
router: NextRouter,
) => {
switch (invitationStatus) {
- case 'accepted':
+ case "accepted":
toast.success("Welcome to the team! You've successfully joined.");
break;
- case 'teamMember':
+ case "teamMember":
toast.error("You've already accepted this invitation!");
break;
default:
diff --git a/pages/api/webhooks/services/[...path]/index.ts b/pages/api/webhooks/services/[...path]/index.ts
index 184d3d35..4cc4d25b 100644
--- a/pages/api/webhooks/services/[...path]/index.ts
+++ b/pages/api/webhooks/services/[...path]/index.ts
@@ -1,5 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
+import { LinkPreset } from "@prisma/client";
+import { put } from "@vercel/blob";
import { waitUntil } from "@vercel/functions";
import { z } from "zod";
@@ -9,7 +11,12 @@ import { putFileServer } from "@/lib/files/put-file-server";
import { extractTeamId, isValidWebhookId } from "@/lib/incoming-webhooks";
import prisma from "@/lib/prisma";
import { ratelimit } from "@/lib/redis";
-import { generateEncrpytedPassword } from "@/lib/utils";
+import {
+ convertDataUrlToBuffer,
+ generateEncrpytedPassword,
+ isDataUrl,
+ uploadImage,
+} from "@/lib/utils";
import { getSupportedContentType } from "@/lib/utils/get-content-type";
import { sendLinkCreatedWebhook } from "@/lib/webhook/triggers/link-created";
@@ -35,6 +42,8 @@ const LinkSchema = z.object({
audienceType: z.enum(["GENERAL", "GROUP", "TEAM"]).optional(),
groupId: z.string().optional(),
allowList: z.array(z.string()).optional(),
+ denyList: z.array(z.string()).optional(),
+ presetId: z.string().optional(),
});
// Define validation schemas for different resource types
@@ -325,12 +334,62 @@ async function handleDocumentCreate(
if (createLink && document.links && document.links.length > 0 && link) {
const linkId = document.links[0].id;
+ // If preset is provided, validate it
+ let preset: LinkPreset | null = null;
+ let metaImage: string | null = null;
+ let metaFavicon: string | null = null;
+ if (link?.presetId) {
+ preset = await prisma.linkPreset.findUnique({
+ where: { pId: link.presetId, teamId: teamId },
+ });
+
+ if (!preset) {
+ return res.status(400).json({
+ error: "Link preset not found or not associated with this team",
+ });
+ }
+
+ // Handle image files for custom meta tag (if enabled)
+ if (preset.enableCustomMetaTag) {
+ // Process meta image if present
+ if (preset.metaImage && isDataUrl(preset.metaImage)) {
+ const { buffer, mimeType, filename } = convertDataUrlToBuffer(
+ preset.metaImage,
+ );
+ const blob = await put(filename, buffer, {
+ access: "public",
+ addRandomSuffix: true,
+ });
+ metaImage = blob.url;
+ }
+
+ // Process favicon if present
+ if (preset.metaFavicon && isDataUrl(preset.metaFavicon)) {
+ const { buffer, mimeType, filename } = convertDataUrlToBuffer(
+ preset.metaFavicon,
+ );
+ const blob = await put(filename, buffer, {
+ access: "public",
+ addRandomSuffix: true,
+ });
+ metaFavicon = blob.url;
+ }
+ }
+ }
+
// Process fields for link update
const hashedPassword = link.password
? await generateEncrpytedPassword(link.password)
- : null;
+ : preset?.password
+ ? preset.password
+ : null;
+
+ const expiresAtDate = link.expiresAt
+ ? new Date(link.expiresAt)
+ : preset?.expiresAt
+ ? new Date(preset.expiresAt)
+ : null;
- const expiresAtDate = link.expiresAt ? new Date(link.expiresAt) : null;
const isGroupAudience = link.audienceType === "GROUP";
let domainId = null;
@@ -355,15 +414,24 @@ async function handleDocumentCreate(
domainId: domainId,
domainSlug: link.domain || null,
slug: link.slug || null,
- emailProtected: link.emailProtected,
- emailAuthenticated: link.emailAuthenticated,
- allowDownload: link.allowDownload,
+ emailProtected: link.emailProtected || preset?.emailProtected || false,
+ emailAuthenticated:
+ link.emailAuthenticated || preset?.emailAuthenticated || false,
+ allowDownload: link.allowDownload || preset?.allowDownload,
enableNotification: link.enableNotification,
enableFeedback: link.enableFeedback,
enableScreenshotProtection: link.enableScreenshotProtection,
audienceType: link.audienceType,
groupId: isGroupAudience ? link.groupId : null,
- allowList: link.allowList,
+ allowList: link.allowList || preset?.allowList,
+ denyList: link.denyList || preset?.denyList,
+ ...(preset?.enableCustomMetaTag && {
+ enableCustomMetatag: preset?.enableCustomMetaTag,
+ metaTitle: preset?.metaTitle,
+ metaDescription: preset?.metaDescription,
+ metaImage: metaImage,
+ metaFavicon: metaFavicon,
+ }),
},
});
@@ -480,14 +548,63 @@ async function handleLinkCreate(
}
}
+ // If preset is provided, validate it
+ let preset: LinkPreset | null = null;
+ let metaImage: string | null = null;
+ let metaFavicon: string | null = null;
+ if (link.presetId) {
+ preset = await prisma.linkPreset.findUnique({
+ where: { pId: link.presetId, teamId: teamId },
+ });
+
+ if (!preset) {
+ return res.status(400).json({
+ error: "Link preset not found or not associated with this team",
+ });
+ }
+
+ // 4. Handle image files for custom meta tag (if enabled)
+ if (preset.enableCustomMetaTag) {
+ // Process meta image if present
+ if (preset.metaImage && isDataUrl(preset.metaImage)) {
+ const { buffer, mimeType, filename } = convertDataUrlToBuffer(
+ preset.metaImage,
+ );
+ const blob = await put(filename, buffer, {
+ access: "public",
+ addRandomSuffix: true,
+ });
+ metaImage = blob.url;
+ }
+
+ // Process favicon if present
+ if (preset.metaFavicon && isDataUrl(preset.metaFavicon)) {
+ const { buffer, mimeType, filename } = convertDataUrlToBuffer(
+ preset.metaFavicon,
+ );
+ const blob = await put(filename, buffer, {
+ access: "public",
+ addRandomSuffix: true,
+ });
+ metaFavicon = blob.url;
+ }
+ }
+ }
+
// Create the link
try {
// Hash password if provided
const hashedPassword = link.password
? await generateEncrpytedPassword(link.password)
- : null;
+ : preset?.password
+ ? preset.password
+ : null;
- const expiresAtDate = link.expiresAt ? new Date(link.expiresAt) : null;
+ const expiresAtDate = link.expiresAt
+ ? new Date(link.expiresAt)
+ : preset?.expiresAt
+ ? new Date(preset.expiresAt)
+ : null;
const isGroupAudience = link.audienceType === "GROUP";
@@ -503,15 +620,24 @@ async function handleLinkCreate(
domainSlug: link.domain || null,
slug: link.slug || null,
expiresAt: expiresAtDate,
- emailProtected: link.emailProtected,
- emailAuthenticated: link.emailAuthenticated,
- allowDownload: link.allowDownload,
+ emailProtected: link.emailProtected || preset?.emailProtected || false,
+ emailAuthenticated:
+ link.emailAuthenticated || preset?.emailAuthenticated || false,
+ allowDownload: link.allowDownload || preset?.allowDownload,
enableNotification: link.enableNotification,
enableFeedback: link.enableFeedback,
enableScreenshotProtection: link.enableScreenshotProtection,
audienceType: link.audienceType,
groupId: isGroupAudience ? link.groupId : null,
- allowList: link.allowList,
+ allowList: link.allowList || preset?.allowList,
+ denyList: link.denyList || preset?.denyList,
+ ...(preset?.enableCustomMetaTag && {
+ enableCustomMetatag: preset?.enableCustomMetaTag,
+ metaTitle: preset?.metaTitle,
+ metaDescription: preset?.metaDescription,
+ metaImage: metaImage,
+ metaFavicon: metaFavicon,
+ }),
},
});
@@ -589,6 +715,49 @@ async function handleDataroomCreate(
}
}
+ // If preset is provided, validate it
+ let preset: LinkPreset | null = null;
+ let metaImage: string | null = null;
+ let metaFavicon: string | null = null;
+ if (createLink && link?.presetId) {
+ preset = await prisma.linkPreset.findUnique({
+ where: { pId: link.presetId, teamId: teamId },
+ });
+
+ if (!preset) {
+ return res.status(400).json({
+ error: "Link preset not found or not associated with this team",
+ });
+ }
+
+ // Handle image files for custom meta tag (if enabled)
+ if (preset.enableCustomMetaTag) {
+ // Process meta image if present
+ if (preset.metaImage && isDataUrl(preset.metaImage)) {
+ const { buffer, mimeType, filename } = convertDataUrlToBuffer(
+ preset.metaImage,
+ );
+ const blob = await put(filename, buffer, {
+ access: "public",
+ addRandomSuffix: true,
+ });
+ metaImage = blob.url;
+ }
+
+ // Process favicon if present
+ if (preset.metaFavicon && isDataUrl(preset.metaFavicon)) {
+ const { buffer, mimeType, filename } = convertDataUrlToBuffer(
+ preset.metaFavicon,
+ );
+ const blob = await put(filename, buffer, {
+ access: "public",
+ addRandomSuffix: true,
+ });
+ metaFavicon = blob.url;
+ }
+ }
+ }
+
// Create the dataroom
try {
// Create dataroom with link if requested
@@ -602,8 +771,14 @@ async function handleDataroomCreate(
const isGroupAudience = link.audienceType === "GROUP";
const hashedPassword = link.password
? await generateEncrpytedPassword(link.password)
- : null;
- const expiresAtDate = link.expiresAt ? new Date(link.expiresAt) : null;
+ : preset?.password
+ ? preset?.password
+ : null;
+ const expiresAtDate = link.expiresAt
+ ? new Date(link.expiresAt)
+ : preset?.expiresAt
+ ? new Date(preset?.expiresAt)
+ : null;
createData.links = {
create: {
@@ -615,15 +790,25 @@ async function handleDataroomCreate(
slug: link.slug || null,
password: hashedPassword,
expiresAt: expiresAtDate,
- emailProtected: link.emailProtected,
- emailAuthenticated: link.emailAuthenticated,
- allowDownload: link.allowDownload,
+ emailProtected:
+ link.emailProtected || preset?.emailProtected || false,
+ emailAuthenticated:
+ link.emailAuthenticated || preset?.emailAuthenticated || false,
+ allowDownload: link.allowDownload || preset?.allowDownload,
enableNotification: link.enableNotification,
enableFeedback: link.enableFeedback,
enableScreenshotProtection: link.enableScreenshotProtection,
audienceType: link.audienceType,
groupId: isGroupAudience ? link.groupId : null,
- allowList: link.allowList,
+ allowList: link.allowList || preset?.allowList,
+ denyList: link.denyList || preset?.denyList,
+ ...(preset?.enableCustomMetaTag && {
+ enableCustomMetatag: preset?.enableCustomMetaTag,
+ metaTitle: preset?.metaTitle,
+ metaDescription: preset?.metaDescription,
+ metaImage: metaImage,
+ metaFavicon: metaFavicon,
+ }),
},
};
}