Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: folder downloads
Marc Seitzb3c7da4fd96b
diff --git a/components/datarooms/download-progress-modal.tsx b/components/datarooms/download-progress-modal.tsxindex 39d799a8..86b44815 100644--- a/components/datarooms/download-progress-modal.tsx+++ b/components/datarooms/download-progress-modal.tsx@@ -31,6 +31,7 @@ import { Progress } from "@/components/ui/progress";export interface DownloadJobStatus {id: string;status: "PENDING" | "PROCESSING" | "COMPLETED" | "FAILED";+ phase?: "VALIDATING" | "BUILDING" | "ZIPPING";progress: number;totalFiles: number;processedFiles: number;@@ -43,6 +44,32 @@ export interface DownloadJobStatus {expiresAt?: string;}+function getProgressMessage(status: DownloadJobStatus | null): string {+ if (!status) return "Starting download...";+ if (status.status === "PENDING") {+ switch (status.phase) {+ case "VALIDATING":+ return "Verifying access...";+ case "BUILDING":+ return "Building file list...";+ default:+ return "Preparing your download...";+ }+ }+ if (status.status === "PROCESSING") {+ if (status.totalFiles > 0) {+ return `Processing ${status.processedFiles} of ${status.totalFiles} files...`;+ }+ return "Creating ZIP archive...";+ }+ if (status.status === "COMPLETED") {+ return status.downloadUrls && status.downloadUrls.length > 1+ ? `Your download is ready! ${status.downloadUrls.length} ZIP files have been created.`+ : "Your download is ready!";+ }+ return status.error || "Download failed. Please try again.";+}+interface DownloadProgressModalProps {isOpen: boolean;onClose: () => void;@@ -383,17 +410,7 @@ export function DownloadProgressModal({: "text-muted-foreground",)}>- {!status- ? "Starting download..."- : status.status === "PENDING"- ? "Preparing your download..."- : status.status === "PROCESSING"- ? `Processing ${status.processedFiles} of ${status.totalFiles} files...`- : status.status === "COMPLETED"- ? status.downloadUrls && status.downloadUrls.length > 1- ? `Your download is ready! ${status.downloadUrls.length} ZIP files have been created.`- : "Your download is ready!"- : status.error || "Download failed. Please try again."}+ {getProgressMessage(status)}</p>{/* Progress Bar */}diff --git a/components/view/dataroom/downloads-panel.tsx b/components/view/dataroom/downloads-panel.tsxindex 38c22994..ec67cd2e 100644--- a/components/view/dataroom/downloads-panel.tsx+++ b/components/view/dataroom/downloads-panel.tsx@@ -17,6 +17,7 @@ import { DownloadOtpVerification } from "@/components/view/dataroom/download-otptype Job = {id: string;status: string;+ phase?: "VALIDATING" | "BUILDING" | "ZIPPING";progress: number;totalFiles: number;processedFiles: number;@@ -29,6 +30,26 @@ type Job = {expiresAt?: string;};+function getJobStatusLabel(job: Job): string {+ if (job.status === "COMPLETED") return "Ready";+ if (job.status === "FAILED") return job.error || "Failed";+ if (job.status === "PROCESSING") {+ if (job.totalFiles > 0) {+ return `${job.processedFiles} / ${job.totalFiles} files`;+ }+ return "Creating ZIP...";+ }+ // PENDING — break out the sub-phase so users see meaningful progress.+ switch (job.phase) {+ case "VALIDATING":+ return "Verifying access...";+ case "BUILDING":+ return "Building file list...";+ default:+ return "Preparing...";+ }+}+const POLL_INTERVAL_MS = 5_000;export function DownloadsPanel({ linkId }: { linkId: string }) {@@ -346,11 +367,7 @@ export function DownloadsPanel({ linkId }: { linkId: string }) {<FileArchive className="h-4 w-4 shrink-0 text-muted-foreground" />)}<span className="text-muted-foreground">- {job.status === "COMPLETED"- ? "Ready"- : job.status === "PROCESSING"- ? `${job.processedFiles} / ${job.totalFiles} files`- : job.status}+ {getJobStatusLabel(job)}</span></div>{job.status === "COMPLETED" &&diff --git a/lib/redis-download-job-store.ts b/lib/redis-download-job-store.tsindex d43024a0..eb1db933 100644--- a/lib/redis-download-job-store.ts+++ b/lib/redis-download-job-store.ts@@ -8,10 +8,28 @@ export type DownloadJobStatus =| "COMPLETED"| "FAILED";+/**+ * Sub-phase within a job's lifecycle. Lets the UI show meaningful copy+ * during the "PENDING" stretch, which can be many seconds for big+ * datarooms while we re-validate the link, load folders/documents, and+ * persist analytics rows before the first Lambda call.+ *+ * - VALIDATING: re-checking link/dataroom is still permitted to download+ * - BUILDING: loading folders/documents/permissions and building the+ * folder tree + writing analytics rows+ * - ZIPPING: Lambda invocations to actually produce ZIP parts+ */+export type DownloadJobPhase = "VALIDATING" | "BUILDING" | "ZIPPING";+export interface DownloadJob {id: string;type: "bulk" | "folder";status: DownloadJobStatus;+ /**+ * Optional finer-grained phase for the UI. Only set by the trigger+ * task; older jobs and the legacy admin path may not have it.+ */+ phase?: DownloadJobPhase;dataroomId: string;dataroomName: string;totalFiles: number;diff --git a/lib/trigger/bulk-download.ts b/lib/trigger/bulk-download.tsindex da6be8fe..6baf1e3f 100644--- a/lib/trigger/bulk-download.ts+++ b/lib/trigger/bulk-download.ts@@ -1,12 +1,28 @@import { logger, task } from "@trigger.dev/sdk";-import { getTeamStorageConfigById } from "@/ee/features/storage/config";-import { InvocationType, InvokeCommand } from "@aws-sdk/client-lambda";-import { getLambdaClientForTeam } from "@/lib/files/aws-client";+import {+ getTeamStorageConfigById,+ type StorageConfig,+} from "@/ee/features/storage/config";+import { ItemType } from "@prisma/client";+import {+ InvocationType,+ InvokeCommand,+ type LambdaClient,+} from "@aws-sdk/client-lambda";++import {+ buildFolderNameMap,+ buildFolderPathsFromHierarchy,+ collectDescendantIds,+} from "@/lib/dataroom/build-folder-hierarchy";import { sendDownloadReadyEmail } from "@/lib/emails/send-download-ready-email";+import { getLambdaClientForTeam } from "@/lib/files/aws-client";import { parseS3PresignedUrl } from "@/lib/files/bulk-download-presign";+import { notifyDocumentDownload } from "@/lib/integrations/slack/events";import prisma from "@/lib/prisma";import { downloadJobStore } from "@/lib/redis-download-job-store";+import { safeSlugify } from "@/lib/utils";import { constructLinkUrl } from "@/lib/utils/link-url";// Maximum files per batch (Lambda payload limit)@@ -14,6 +30,10 @@ const MAX_FILES_PER_BATCH = 500;// Maximum size for a single ZIP file (500MB to stay within Lambda's 15min timeout)// Lambda needs time to: read from S3 + apply watermarks + create ZIP + upload to S3const MAX_ZIP_SIZE_BYTES = 500 * 1024 * 1024;+// Estimated size for files where size info is missing+const UNKNOWN_FILE_SIZE_ESTIMATE = 10 * 1024 * 1024;+// Threshold above which we don't store the per-document list in downloadMetadata+const DOCUMENT_LIST_METADATA_THRESHOLD = 100;/*** Generate a UTC timestamp in the format: "20260202T131428Z"@@ -45,27 +65,58 @@ function generateZipFileName(return `${base}${paddedPart ? `-${paddedPart}` : ""}`;}+export type BulkDownloadFolderStructure = {+ [key: string]: {+ name: string;+ path: string;+ files: {+ name: string;+ key: string;+ type?: string;+ numPages?: number;+ needsWatermark?: boolean;+ size?: number; // File size in bytes+ }[];+ };+};++/**+ * When `sourceContext` is provided the task fetches dataroom folders,+ * documents and permissions itself, builds the folder structure, persists+ * download views and emits the Slack notification. This is the path used by+ * the viewer-facing folder/bulk download endpoints so the API can return a+ * jobId immediately without doing heavy DB work under the request timeout.+ *+ * When `folderStructure` + `fileKeys` are provided directly (legacy path+ * used by the team admin endpoint) the task skips that work and runs the+ * Lambda batching/zip pipeline against the supplied structure.+ */export type BulkDownloadPayload = {jobId: string;dataroomId: string;dataroomName: string;teamId: string;- folderStructure: {- [key: string]: {- name: string;- path: string;- files: {- name: string;- key: string;- type?: string;- numPages?: number;- needsWatermark?: boolean;- size?: number; // File size in bytes- }[];- };+ // Source bucket is optional – when absent, the task resolves it from the+ // team's storage config. The new viewer endpoints skip this fetch in the+ // request path so the API can return a jobId immediately.+ sourceBucket?: string;+ // Optional pre-built folder structure (admin path)+ folderStructure?: BulkDownloadFolderStructure;+ fileKeys?: string[];+ // Optional context that lets the task build the folder structure itself+ sourceContext?: {+ type: "bulk" | "folder";+ folderId?: string;+ linkId: string;+ viewId: string;+ viewerId?: string;+ viewerEmail?: string;+ groupId?: string;+ permissionGroupId?: string;+ verified: boolean;+ enableWatermark: boolean;+ notifySlack: boolean;};- fileKeys: string[];- sourceBucket: string;watermarkConfig?: {enabled: boolean;config?: any;@@ -97,33 +148,79 @@ export const bulkDownloadTask = task({dataroomId,dataroomName,teamId,- folderStructure,- fileKeys,- sourceBucket,watermarkConfig,- viewId,- viewerEmail,emailNotification,emailAddress,- folderName,+ sourceContext,} = payload;+ let sourceBucket = payload.sourceBucket;++ let folderStructure = payload.folderStructure;+ let fileKeys = payload.fileKeys;+ let folderName = payload.folderName;+logger.info("Starting bulk download task", {jobId,dataroomId,dataroomName,- totalFiles: fileKeys.length,+ mode: sourceContext ? "build" : "prebuilt",+ preBuiltFileCount: fileKeys?.length ?? 0,});// Generate timestamp once for all parts of this downloadconst downloadTimestamp = generateTimestamp();try {- // Update job status to processing+ // Resolve the team's storage config and Lambda client ONCE for the+ // whole task (used by every batch). `getLambdaClientForTeam` also+ // looks up the config, but they run in parallel so the wall-clock+ // cost is a single round-trip rather than `O(batches)` of them.+ const [storageConfig, lambdaClient] = await Promise.all([+ getTeamStorageConfigById(teamId),+ getLambdaClientForTeam(teamId),+ ]);+ const resolvedSourceBucket = sourceBucket ?? storageConfig.bucket;++ // If the API didn't pre-build the folder structure, do it here while+ // the job is still in PENDING (so the UI keeps showing "Preparing...").+ if (sourceContext) {+ const built = await buildDownloadContext({+ jobId,+ dataroomId,+ dataroomName,+ teamId,+ sourceContext,+ });++ folderStructure = built.folderStructure;+ fileKeys = built.fileKeys;+ if (sourceContext.type === "folder" && built.folderName) {+ folderName = built.folderName;+ }+ }++ if (!folderStructure || !fileKeys) {+ throw new Error("Missing folderStructure/fileKeys for bulk download");+ }++ if (fileKeys.length === 0) {+ logger.warn("No files to download – marking job as failed", { jobId });+ await downloadJobStore.updateJob(jobId, {+ status: "FAILED",+ error: "No files available to download",+ });+ return { success: false, jobId, downloadUrls: [] };+ }++ // Now that we know the real total, flip to PROCESSING with an+ // accurate totalFiles so the UI can render meaningful progress.await downloadJobStore.updateJob(jobId, {status: "PROCESSING",+ phase: "ZIPPING",processedFiles: 0,progress: 0,+ totalFiles: fileKeys.length,});// Calculate total size from folder structure for batch decisions@@ -133,6 +230,12 @@ export const bulkDownloadTask = task({folder.files.reduce((fSum, file) => fSum + (file.size || 0), 0),0,);+ const filesWithKnownSize = Object.values(folderStructure).reduce(+ (count, folder) =>+ count + folder.files.filter((f) => f.key && (f.size || 0) > 0).length,+ 0,+ );+ const filesWithUnknownSize = fileKeys.length - filesWithKnownSize;const hasReliableSizeInfo = totalPayloadSize > 0;// For small datarooms, process in a single batch (check both count AND size)@@ -145,13 +248,16 @@ export const bulkDownloadTask = task({jobId,fileCount: fileKeys.length,totalSizeMB: Math.round(totalPayloadSize / (1024 * 1024)),+ filesWithKnownSize,+ filesWithUnknownSize,});const result = await processDownloadBatch({- teamId,+ lambdaClient,+ storageConfig,folderStructure,fileKeys,- sourceBucket,+ sourceBucket: resolvedSourceBucket,watermarkConfig,dataroomName,zipPartNumber: 1,@@ -206,6 +312,11 @@ export const bulkDownloadTask = task({jobId,fileCount: fileKeys.length,totalSizeMB: Math.round(totalPayloadSize / (1024 * 1024)),+ filesWithKnownSize,+ filesWithUnknownSize,+ unknownSizeEstimateMB: Math.round(+ UNKNOWN_FILE_SIZE_ESTIMATE / (1024 * 1024),+ ),maxFilesPerBatch: MAX_FILES_PER_BATCH,maxSizePerBatch: `${MAX_ZIP_SIZE_BYTES / (1024 * 1024)}MB`,});@@ -223,8 +334,11 @@ export const bulkDownloadTask = task({batchDetails: batches.map((b, i) => ({batch: i + 1,files: b.fileKeys.length,- sizeBytes: b.totalSize,- sizeMB: Math.round(b.totalSize / (1024 * 1024)),+ knownSizeFiles: b.knownSizeFiles,+ unknownSizeFiles: b.unknownSizeFiles,+ knownSizeBytes: b.knownSizeBytes,+ knownSizeMB: Math.round(b.knownSizeBytes / (1024 * 1024)),+ estimatedSizeMB: Math.round(b.estimatedSizeBytes / (1024 * 1024)),})),});@@ -237,14 +351,19 @@ export const bulkDownloadTask = task({jobId,batchNumber,fileCount: batch.fileKeys.length,+ knownSizeMB: Math.round(batch.knownSizeBytes / (1024 * 1024)),+ estimatedSizeMB: Math.round(+ batch.estimatedSizeBytes / (1024 * 1024),+ ),});try {const result = await processDownloadBatch({- teamId,+ lambdaClient,+ storageConfig,folderStructure: batch.folderStructure,fileKeys: batch.fileKeys,- sourceBucket,+ sourceBucket: resolvedSourceBucket,watermarkConfig,dataroomName,zipPartNumber: batchNumber,@@ -328,25 +447,39 @@ export const bulkDownloadTask = task({downloadUrls,};} catch (error) {+ const isPermissionFailure = error instanceof DownloadNotPermittedError;+ const userMessage = isPermissionFailure+ ? error.userMessage+ : error instanceof Error+ ? error.message+ : String(error);+logger.error("Bulk download task failed", {jobId,+ permissionFailure: isPermissionFailure,error: error instanceof Error ? error.message : String(error),});// Update job status to failedawait downloadJobStore.updateJob(jobId, {status: "FAILED",- error: error instanceof Error ? error.message : String(error),+ error: userMessage,});+ // Permission failures aren't transient: don't waste a retry on them.+ if (isPermissionFailure) {+ return { success: false, jobId, downloadUrls: [] };+ }+throw error;}},});interface ProcessDownloadBatchParams {- teamId: string;- folderStructure: BulkDownloadPayload["folderStructure"];+ lambdaClient: LambdaClient;+ storageConfig: StorageConfig;+ folderStructure: BulkDownloadFolderStructure;fileKeys: string[];sourceBucket: string;watermarkConfig?: BulkDownloadPayload["watermarkConfig"];@@ -363,7 +496,8 @@ interface ProcessDownloadBatchResult {}async function processDownloadBatch({- teamId,+ lambdaClient,+ storageConfig,folderStructure,fileKeys,sourceBucket,@@ -374,11 +508,6 @@ async function processDownloadBatch({zipFileName,expirationHours = 72,}: ProcessDownloadBatchParams): Promise<ProcessDownloadBatchResult> {- const [client, storageConfig] = await Promise.all([- getLambdaClientForTeam(teamId),- getTeamStorageConfigById(teamId),- ]);-const command = new InvokeCommand({FunctionName: storageConfig.lambdaFunctionName,InvocationType: InvocationType.RequestResponse,@@ -395,7 +524,7 @@ async function processDownloadBatch({}),});- const response = await client.send(command);+ const response = await lambdaClient.send(command);if (!response.Payload) {throw new Error("Lambda response payload is undefined or empty");@@ -421,29 +550,33 @@ async function processDownloadBatch({}interface FileBatch {- folderStructure: BulkDownloadPayload["folderStructure"];+ folderStructure: BulkDownloadFolderStructure;fileKeys: string[];- totalSize: number;+ knownSizeBytes: number;+ estimatedSizeBytes: number;+ knownSizeFiles: number;+ unknownSizeFiles: number;}interface FileInfo {key: string;folderPath: string;size: number;- file: BulkDownloadPayload["folderStructure"][string]["files"][number];+ file: BulkDownloadFolderStructure[string]["files"][number];}function splitFilesIntoBatches(- folderStructure: BulkDownloadPayload["folderStructure"],+ folderStructure: BulkDownloadFolderStructure,fileKeys: string[],): FileBatch[] {const batches: FileBatch[] = [];// Build a list of files with their infoconst filesWithInfo: FileInfo[] = [];+ const fileKeysSet = new Set(fileKeys);for (const [path, folder] of Object.entries(folderStructure)) {for (const file of folder.files) {- if (file.key && fileKeys.includes(file.key)) {+ if (file.key && fileKeysSet.has(file.key)) {filesWithInfo.push({key: file.key,folderPath: path,@@ -464,7 +597,7 @@ function splitFilesIntoBatches(let currentBatchSize = 0;for (const fileInfo of filesWithInfo) {- const fileSize = fileInfo.size || 10 * 1024 * 1024; // Estimate 10MB for unknown sizes+ const fileSize = fileInfo.size || UNKNOWN_FILE_SIZE_ESTIMATE; // Estimate for unknown sizes// If adding this file would exceed limit, start a new batch// Also enforce max file count per batch to avoid Lambda payload limits@@ -499,15 +632,25 @@ function splitFilesIntoBatches(function buildBatchFromFiles(files: FileInfo[],- folderStructure: BulkDownloadPayload["folderStructure"],+ folderStructure: BulkDownloadFolderStructure,): FileBatch {- const batchFolderStructure: BulkDownloadPayload["folderStructure"] = {};+ const batchFolderStructure: BulkDownloadFolderStructure = {};const batchFileKeys: string[] = [];- let totalSize = 0;+ let knownSizeBytes = 0;+ let estimatedSizeBytes = 0;+ let knownSizeFiles = 0;+ let unknownSizeFiles = 0;for (const fileInfo of files) {batchFileKeys.push(fileInfo.key);- totalSize += fileInfo.size || 0;+ if (fileInfo.size > 0) {+ knownSizeBytes += fileInfo.size;+ estimatedSizeBytes += fileInfo.size;+ knownSizeFiles += 1;+ } else {+ estimatedSizeBytes += UNKNOWN_FILE_SIZE_ESTIMATE;+ unknownSizeFiles += 1;+ }// Initialize folder if not exists in batchif (!batchFolderStructure[fileInfo.folderPath]) {@@ -541,10 +684,657 @@ function buildBatchFromFiles(return {folderStructure: batchFolderStructure,fileKeys: batchFileKeys,- totalSize,+ knownSizeBytes,+ estimatedSizeBytes,+ knownSizeFiles,+ unknownSizeFiles,+ };+}++interface BuildDownloadContextParams {+ jobId: string;+ dataroomId: string;+ dataroomName: string;+ teamId: string;+ sourceContext: NonNullable<BulkDownloadPayload["sourceContext"]>;+}++interface BuildDownloadContextResult {+ folderStructure: BulkDownloadFolderStructure;+ fileKeys: string[];+ folderName?: string;+}++/**+ * Sentinel error thrown by the validation step when the link is no longer+ * permitted to download. The task's catch block converts it into a clean+ * "FAILED" job status with a user-facing message instead of a noisy stack.+ */+class DownloadNotPermittedError extends Error {+ constructor(public readonly userMessage: string) {+ super(userMessage);+ this.name = "DownloadNotPermittedError";+ }+}++/**+ * Re-validate the link/dataroom against the live DB before doing any work.+ * Defends against the time-of-check / time-of-use gap between the request+ * handler returning `202` and the trigger task actually running: a link+ * could have been archived, deleted, expired, or had download disabled+ * in that window.+ *+ * Returns the *live* values for fields the rest of the pipeline depends on+ * (`enableWatermark`, `permissionGroupId`) so we never trust the snapshot+ * the API put on the wire.+ */+async function revalidateDownloadContext(params: {+ jobId: string;+ dataroomId: string;+ teamId: string;+ type: "bulk" | "folder";+ linkId: string;+}): Promise<{+ enableWatermark: boolean;+ permissionGroupId: string | null;+}> {+ const { jobId, dataroomId, teamId, type, linkId } = params;++ await downloadJobStore.updateJob(jobId, { phase: "VALIDATING" });++ const link = await prisma.link.findUnique({+ where: { id: linkId },+ select: {+ id: true,+ teamId: true,+ allowDownload: true,+ isArchived: true,+ deletedAt: true,+ expiresAt: true,+ enableWatermark: true,+ permissionGroupId: true,+ dataroom: {+ select: { id: true, allowBulkDownload: true },+ },+ },+ });++ const NOT_PERMITTED = new DownloadNotPermittedError(+ "This download is no longer available. The link may have been archived, expired, or had its permissions changed.",+ );++ if (!link || !link.dataroom) throw NOT_PERMITTED;+ if (link.dataroom.id !== dataroomId) throw NOT_PERMITTED;+ if (link.teamId !== teamId) throw NOT_PERMITTED;+ if (!link.allowDownload) throw NOT_PERMITTED;+ if (link.isArchived) throw NOT_PERMITTED;+ if (link.deletedAt) throw NOT_PERMITTED;+ if (link.expiresAt && link.expiresAt < new Date()) throw NOT_PERMITTED;+ if (type === "bulk" && !link.dataroom.allowBulkDownload) throw NOT_PERMITTED;++ return {+ enableWatermark: !!link.enableWatermark,+ permissionGroupId: link.permissionGroupId ?? null,};}+/**+ * Heavy work that used to live in the API handlers: re-validate the link,+ * load the relevant folders/documents/permissions, build the folder+ * structure and file list, persist `view.createMany` rows for analytics+ * and emit the Slack notification. Performed inside the trigger task so+ * the API can return a jobId immediately.+ */+async function buildDownloadContext({+ jobId,+ dataroomId,+ dataroomName,+ teamId,+ sourceContext,+}: BuildDownloadContextParams): Promise<BuildDownloadContextResult> {+ const {+ type,+ folderId,+ linkId,+ viewId,+ viewerId,+ viewerEmail,+ groupId,+ verified,+ notifySlack,+ } = sourceContext;++ if (type === "folder" && !folderId) {+ throw new Error("folderId is required for folder downloads");+ }++ // 1. Re-validate the link/dataroom against the live DB. We use the live+ // `enableWatermark` and `permissionGroupId` from here on, ignoring+ // any snapshots in `sourceContext`.+ const { enableWatermark, permissionGroupId } = await revalidateDownloadContext(+ { jobId, dataroomId, teamId, type, linkId },+ );++ // 2. Heavy DB work + tree building.+ await downloadJobStore.updateJob(jobId, { phase: "BUILDING" });++ // Load all folders for this dataroom – needed for hierarchy traversal and+ // path computation.+ const allDataroomFolders = await prisma.dataroomFolder.findMany({+ where: { dataroomId },+ select: { id: true, name: true, path: true, parentId: true },+ });++ let rootFolder:+ | { id: string; name: string; path: string; parentId: string | null }+ | undefined;++ let foldersInScope = allDataroomFolders;+ if (type === "folder") {+ rootFolder = allDataroomFolders.find((f) => f.id === folderId);+ if (!rootFolder) {+ throw new Error(`Folder ${folderId} not found for dataroom ${dataroomId}`);+ }+ const descendantIds = collectDescendantIds(folderId!, allDataroomFolders);+ foldersInScope = allDataroomFolders.filter(+ (f) => f.id === folderId || descendantIds.has(f.id),+ );+ }++ // DB-level prune: documents whose primary version is in VERCEL_BLOB+ // can never be bulk-downloaded, so don't even pull them off the wire.+ // We still re-check `type === "notion"` in memory because `type` is+ // nullable and Prisma's `not` operator treats NULL as a non-match.+ const documentsInScope = await prisma.dataroomDocument.findMany({+ where: {+ dataroomId,+ ...(type === "folder"+ ? { folderId: { in: foldersInScope.map((f) => f.id) } }+ : {}),+ document: {+ versions: {+ some: {+ isPrimary: true,+ storageType: { not: "VERCEL_BLOB" },+ },+ },+ },+ },+ select: {+ id: true,+ folderId: true,+ document: {+ select: {+ id: true,+ name: true,+ versions: {+ where: { isPrimary: true },+ select: {+ type: true,+ file: true,+ storageType: true,+ originalFile: true,+ numPages: true,+ contentType: true,+ fileSize: true,+ },+ take: 1,+ },+ },+ },+ },+ });++ // Apply per-group permissions (canDownload). We use the live+ // `permissionGroupId` from the link, not the snapshot in `sourceContext`.+ let effectiveFolders = foldersInScope;+ let effectiveDocuments = documentsInScope;+ const effectiveGroupId = groupId || permissionGroupId;++ if (effectiveGroupId) {+ let groupPermissions: { itemType: ItemType; itemId: string }[] = [];++ if (groupId) {+ groupPermissions = await prisma.viewerGroupAccessControls.findMany({+ where: { groupId, canDownload: true },+ select: { itemType: true, itemId: true },+ });+ } else if (permissionGroupId) {+ groupPermissions = await prisma.permissionGroupAccessControls.findMany({+ where: { groupId: permissionGroupId, canDownload: true },+ select: { itemType: true, itemId: true },+ });+ }++ const permittedFolderIds = new Set<string>();+ const permittedDocumentIds = new Set<string>();+ for (const p of groupPermissions) {+ if (p.itemType === ItemType.DATAROOM_FOLDER) {+ permittedFolderIds.add(p.itemId);+ } else if (p.itemType === ItemType.DATAROOM_DOCUMENT) {+ permittedDocumentIds.add(p.itemId);+ }+ }++ effectiveFolders = effectiveFolders.filter((f) =>+ permittedFolderIds.has(f.id),+ );+ effectiveDocuments = effectiveDocuments.filter((d) =>+ permittedDocumentIds.has(d.id),+ );+ }++ // Single pass: compute downloadable docs AND index docs by folderId so+ // both build*ModeStructure helpers can reuse the same map without+ // walking effectiveDocuments again.+ const downloadableDocuments: BuildDocumentRow[] = [];+ const docsByFolderId = new Map<string, BuildDocumentRow[]>();+ const rootDocs: BuildDocumentRow[] = [];++ for (const doc of effectiveDocuments) {+ const version = doc.document.versions[0];+ if (+ !version ||+ version.type === "notion" ||+ version.storageType === "VERCEL_BLOB"+ ) {+ continue;+ }++ downloadableDocuments.push(doc);++ if (doc.folderId) {+ const list = docsByFolderId.get(doc.folderId);+ if (list) {+ list.push(doc);+ } else {+ docsByFolderId.set(doc.folderId, [doc]);+ }+ } else {+ rootDocs.push(doc);+ }+ }++ let folderStructure: BulkDownloadFolderStructure;+ let fileKeys: string[];++ if (type === "folder") {+ const built = buildFolderModeStructure({+ rootFolder: rootFolder!,+ // Use the FULL folder list so parent folders that only have canView+ // permissions still resolve correct child paths.+ fullFolders: foldersInScope,+ effectiveFolders,+ docsByFolderId,+ enableWatermark,+ });+ folderStructure = built.folderStructure;+ fileKeys = built.fileKeys;+ } else {+ const built = buildBulkModeStructure({+ allFolders: allDataroomFolders,+ effectiveFolders,+ docsByFolderId,+ rootDocs,+ enableWatermark,+ });+ folderStructure = built.folderStructure;+ fileKeys = built.fileKeys;+ }++ logger.info("Built folder download context", {+ jobId,+ type,+ folderCount: effectiveFolders.length,+ documentCount: effectiveDocuments.length,+ downloadableDocuments: downloadableDocuments.length,+ fileKeys: fileKeys.length,+ });++ // Persist analytics rows.+ if (downloadableDocuments.length > 0) {+ const downloadType = type === "folder" ? "FOLDER" : "BULK";+ const includeDocumentsList =+ downloadableDocuments.length < DOCUMENT_LIST_METADATA_THRESHOLD;+ const summaryMetadata =+ type === "folder"+ ? {+ folderName: rootFolder!.name,+ folderPath: rootFolder!.path,+ documentCount: downloadableDocuments.length,+ }+ : {+ dataroomName,+ documentCount: downloadableDocuments.length,+ };+ const downloadMetadata = includeDocumentsList+ ? {+ ...summaryMetadata,+ documents: downloadableDocuments.map((doc) => ({+ id: doc.document.id,+ name: doc.document.name,+ })),+ }+ : summaryMetadata;++ await prisma.view.createMany({+ data: downloadableDocuments.map((doc) => ({+ viewType: "DOCUMENT_VIEW",+ documentId: doc.document.id,+ linkId,+ dataroomId,+ groupId: groupId ?? null,+ dataroomViewId: viewId,+ viewerEmail: viewerEmail ?? null,+ downloadedAt: new Date(),+ downloadType,+ downloadMetadata,+ viewerId: viewerId ?? null,+ verified,+ })),+ skipDuplicates: true,+ });+ }++ // Slack notification (best-effort, mirrors prior API behaviour).+ if (notifySlack) {+ try {+ await notifyDocumentDownload({+ teamId,+ documentId: undefined,+ dataroomId,+ linkId,+ viewerEmail: viewerEmail ?? undefined,+ viewerId: viewerId ?? undefined,+ metadata:+ type === "folder"+ ? {+ folderName: rootFolder!.name,+ documentCount: downloadableDocuments.length,+ isFolderDownload: true,+ }+ : {+ documentCount: downloadableDocuments.length,+ isBulkDownload: true,+ },+ });+ } catch (err) {+ logger.warn("Slack notification failed", {+ jobId,+ error: err instanceof Error ? err.message : String(err),+ });+ }+ }++ return {+ folderStructure,+ fileKeys,+ folderName: type === "folder" ? rootFolder!.name : undefined,+ };+}++type FolderRow = {+ id: string;+ name: string;+ path: string;+ parentId: string | null;+};++type BuildDocumentRow = {+ id: string;+ folderId: string | null;+ document: {+ id: string;+ name: string;+ versions: {+ type: string | null;+ file: string;+ storageType: string;+ originalFile: string | null;+ numPages: number | null;+ contentType: string | null;+ fileSize: bigint | null;+ }[];+ };+};++/**+ * Pick the S3 key to ship to Lambda for a given primary version. PDFs use+ * the watermarkable rendition (`file`) when watermarking is on; everything+ * else falls back to the original upload.+ */+function pickFileKey(+ version: BuildDocumentRow["document"]["versions"][number],+ enableWatermark: boolean,+): string {+ return enableWatermark && version.type === "pdf"+ ? version.file+ : (version.originalFile ?? version.file);+}++function buildFolderModeStructure({+ rootFolder,+ fullFolders,+ effectiveFolders,+ docsByFolderId,+ enableWatermark,+}: {+ rootFolder: FolderRow;+ fullFolders: FolderRow[];+ effectiveFolders: FolderRow[];+ docsByFolderId: Map<string, BuildDocumentRow[]>;+ enableWatermark: boolean;+}) {+ const folderStructure: BulkDownloadFolderStructure = {};+ const fileKeys: string[] = [];++ // Build paths from the FULL folder list (same as the previous API logic+ // so parent folders only granted canView still produce correct child paths).+ const computedPathMap = buildFolderPathsFromHierarchy(fullFolders);+ const computedRootPath =+ computedPathMap.get(rootFolder.id) ?? rootFolder.path;+ const rootFolderInfo = {+ name: rootFolder.name,+ computedPath: computedRootPath,+ };++ const addFileToStructure = (+ computedFolderPath: string,+ fileName: string,+ fileKey: string,+ fileType?: string,+ numPages?: number,+ fileSize?: number,+ ) => {+ let relativePath = "";+ if (computedFolderPath !== rootFolderInfo.computedPath) {+ const escapedRoot = rootFolderInfo.computedPath.replace(+ /[.*+?^${}()|[\]\\]/g,+ "\\$&",+ );+ const pathRegex = new RegExp(`^${escapedRoot}/(.*)$`);+ const match = computedFolderPath.match(pathRegex);+ relativePath = match ? match[1] : "";+ }++ const pathParts = [safeSlugify(rootFolderInfo.name)];+ if (relativePath) {+ pathParts.push(...relativePath.split("/").filter(Boolean));+ }++ let currentPath = "";+ for (const part of pathParts) {+ currentPath += "/" + part;+ if (!folderStructure[currentPath]) {+ folderStructure[currentPath] = {+ name: part,+ path: currentPath,+ files: [],+ };+ }+ }++ if (fileName && fileKey) {+ const needsWatermark =+ enableWatermark && (fileType === "pdf" || fileType === "image");+ folderStructure[currentPath].files.push({+ name: fileName,+ key: fileKey,+ type: fileType,+ numPages: numPages,+ needsWatermark: needsWatermark ?? undefined,+ size: fileSize,+ });+ fileKeys.push(fileKey);+ }+ };++ for (const folder of effectiveFolders) {+ const folderPath = computedPathMap.get(folder.id) ?? folder.path;+ const docs = docsByFolderId.get(folder.id);++ if (!docs || docs.length === 0) {+ addFileToStructure(folderPath, "", "");+ continue;+ }++ for (const doc of docs) {+ // docsByFolderId is already filtered to downloadable primary+ // versions (see buildDownloadContext), so versions[0] is guaranteed+ // present and not notion / VERCEL_BLOB.+ const version = doc.document.versions[0];+ const fileKey = pickFileKey(version, enableWatermark);+ addFileToStructure(+ folderPath,+ doc.document.name,+ fileKey,+ version.type ?? undefined,+ version.numPages ?? undefined,+ version.fileSize ? Number(version.fileSize) : undefined,+ );+ }+ }++ const rootPath = "/" + safeSlugify(rootFolder.name);+ if (!folderStructure[rootPath]) {+ folderStructure[rootPath] = {+ name: safeSlugify(rootFolder.name),+ path: rootPath,+ files: [],+ };+ }++ return { folderStructure, fileKeys };+}++function buildBulkModeStructure({+ allFolders,+ effectiveFolders,+ docsByFolderId,+ rootDocs,+ enableWatermark,+}: {+ allFolders: FolderRow[];+ effectiveFolders: FolderRow[];+ docsByFolderId: Map<string, BuildDocumentRow[]>;+ rootDocs: BuildDocumentRow[];+ enableWatermark: boolean;+}) {+ const folderStructure: BulkDownloadFolderStructure = {};+ const fileKeys: string[] = [];++ const computedPathMap = buildFolderPathsFromHierarchy(allFolders);+ const folderMap = buildFolderNameMap(allFolders, computedPathMap);++ const addFileToStructure = (+ path: string,+ fileName: string,+ fileKey: string,+ fileType?: string,+ numPages?: number,+ fileSize?: number,+ ) => {+ const pathParts = path.split("/").filter(Boolean);+ let currentPath = "";++ for (const part of pathParts) {+ currentPath += "/" + part;+ if (!folderStructure[currentPath]) {+ const folderInfo = folderMap.get(currentPath);+ folderStructure[currentPath] = {+ name: folderInfo ? folderInfo.name : part,+ path: currentPath,+ files: [],+ };+ }+ }++ if (!folderStructure[path]) {+ const folderInfo = folderMap.get(path) || { name: "Root", id: null };+ folderStructure[path] = {+ name: folderInfo.name,+ path: path,+ files: [],+ };+ }++ if (fileName && fileKey) {+ const needsWatermark =+ enableWatermark && (fileType === "pdf" || fileType === "image");+ folderStructure[path].files.push({+ name: fileName,+ key: fileKey,+ type: fileType,+ numPages: numPages,+ needsWatermark: needsWatermark ?? undefined,+ size: fileSize,+ });+ fileKeys.push(fileKey);+ }+ };++ // Root-level documents (already pre-filtered to downloadable primary+ // versions in buildDownloadContext).+ for (const doc of rootDocs) {+ const version = doc.document.versions[0];+ const fileKey = pickFileKey(version, enableWatermark);+ addFileToStructure(+ "/",+ doc.document.name,+ fileKey,+ version.type ?? undefined,+ version.numPages ?? undefined,+ version.fileSize ? Number(version.fileSize) : undefined,+ );+ }++ // Documents nested in folders.+ for (const folder of effectiveFolders) {+ const folderPath = computedPathMap.get(folder.id) ?? folder.path;+ const docs = docsByFolderId.get(folder.id);++ if (!docs || docs.length === 0) {+ addFileToStructure(folderPath, "", "");+ continue;+ }++ for (const doc of docs) {+ const version = doc.document.versions[0];+ const fileKey = pickFileKey(version, enableWatermark);+ addFileToStructure(+ folderPath,+ doc.document.name,+ fileKey,+ version.type ?? undefined,+ version.numPages ?? undefined,+ version.fileSize ? Number(version.fileSize) : undefined,+ );+ }+ }++ return { folderStructure, fileKeys };+}+async function sendEmailNotification({emailAddress,dataroomName,diff --git a/pages/api/links/download/[jobId].ts b/pages/api/links/download/[jobId].tsindex 8f967b25..2cb772cc 100644--- a/pages/api/links/download/[jobId].ts+++ b/pages/api/links/download/[jobId].ts@@ -59,6 +59,7 @@ export default async function handler(return res.status(200).json({id: job.id,status: job.status,+ phase: job.phase,progress: job.progress,totalFiles: job.totalFiles,processedFiles: job.processedFiles,diff --git a/pages/api/links/download/bulk.ts b/pages/api/links/download/bulk.tsindex 9869baae..9eaf6108 100644--- a/pages/api/links/download/bulk.ts+++ b/pages/api/links/download/bulk.ts@@ -1,20 +1,17 @@import { NextApiRequest, NextApiResponse } from "next";-import { getTeamStorageConfigById } from "@/ee/features/storage/config";-import { ItemType, ViewType } from "@prisma/client";+import { ViewType } from "@prisma/client";import { verifyDataroomSessionInPagesRouter } from "@/lib/auth/dataroom-auth";-import {- buildFolderNameMap,- buildFolderPathsFromHierarchy,-} from "@/lib/dataroom/build-folder-hierarchy";-import { notifyDocumentDownload } from "@/lib/integrations/slack/events";import prisma from "@/lib/prisma";import { downloadJobStore } from "@/lib/redis-download-job-store";import { bulkDownloadTask } from "@/lib/trigger/bulk-download";import { getIpAddress } from "@/lib/utils/ip";export const config = {+ // Lightweight handler: validate access + create job + trigger task. The+ // heavy folder/document/permission queries and view inserts run inside the+ // trigger task so the viewer never sees a request timeout.maxDuration: 60,};@@ -73,39 +70,6 @@ export default async function handle(name: true,teamId: true,allowBulkDownload: true,- folders: {- select: {- id: true,- name: true,- path: true,- parentId: true,- },- },- documents: {- select: {- id: true,- folderId: true,- document: {- select: {- id: true,- name: true,- versions: {- where: { isPrimary: true },- select: {- type: true,- file: true,- storageType: true,- originalFile: true,- contentType: true,- numPages: true,- fileSize: true,- },- take: 1,- },- },- },- },- },},},},@@ -125,7 +89,6 @@ export default async function handle(return res.status(401).json({ error: "Session required to download" });}- // Verified session and email are only required when the viewer requested email notificationif (emailNotification) {if (!view.viewerEmail) {return res.status(400).json({@@ -145,7 +108,6 @@ export default async function handle(return res.status(403).json({ error: "Error downloading" });}- // if link is archived, we should not allow the downloadif (view.link.isArchived) {return res.status(403).json({ error: "Error downloading" });}@@ -154,24 +116,20 @@ export default async function handle(return res.status(403).json({ error: "Error downloading" });}- // if link is expired, we should not allow the downloadif (view.link.expiresAt && view.link.expiresAt < new Date()) {return res.status(403).json({ error: "Error downloading" });}- // if dataroom does not exist, we should not allow the downloadif (!view.dataroom) {return res.status(404).json({ error: "Error downloading" });}- // if dataroom does not allow bulk download, we should not allow the downloadif (!view.dataroom.allowBulkDownload) {return res.status(403).json({ error: "Bulk download is disabled for this dataroom" });}- // if viewedAt is longer than 23 hours ago, we should not allow the downloadif (view.viewedAt &&view.viewedAt < new Date(Date.now() - 23 * 60 * 60 * 1000)@@ -179,272 +137,19 @@ export default async function handle(return res.status(403).json({ error: "Error downloading" });}- // Build folder paths from the FULL folder list (source of truth) BEFORE- // permission filtering. This ensures parent folders that only have canView- // (not canDownload) are still included in the hierarchy so child paths are- // computed correctly (e.g. "/legal/contracts" instead of just "/contracts").- const allFolders = view.dataroom.folders;- const computedPathMap = buildFolderPathsFromHierarchy(allFolders);- const folderMap = buildFolderNameMap(allFolders, computedPathMap);-- let downloadFolders = allFolders;- let downloadDocuments = view.dataroom.documents;-- // Check permissions based on groupId (ViewerGroup) or permissionGroupId (PermissionGroup)- const effectiveGroupId = view.groupId || view.link.permissionGroupId;-- if (effectiveGroupId) {- let groupPermissions: any[] = [];-- if (view.groupId) {- // This is a ViewerGroup (legacy behavior)- groupPermissions = await prisma.viewerGroupAccessControls.findMany({- where: { groupId: view.groupId, canDownload: true },- });- } else if (view.link.permissionGroupId) {- // This is a PermissionGroup (new behavior)- groupPermissions = await prisma.permissionGroupAccessControls.findMany({- where: {- groupId: view.link.permissionGroupId,- canDownload: true,- },- });- }-- const permittedFolderIds = new Set(- groupPermissions- .filter(- (permission) => permission.itemType === ItemType.DATAROOM_FOLDER,- )- .map((permission) => permission.itemId),- );- const permittedDocumentIds = new Set(- groupPermissions- .filter(- (permission) => permission.itemType === ItemType.DATAROOM_DOCUMENT,- )- .map((permission) => permission.itemId),- );-- downloadFolders = downloadFolders.filter((folder) =>- permittedFolderIds.has(folder.id),- );- downloadDocuments = downloadDocuments.filter((doc) =>- permittedDocumentIds.has(doc.id),- );- }-- // Create individual document views for each document being downloaded- const downloadableDocuments = downloadDocuments.filter(- (doc) =>- doc.document.versions[0] &&- doc.document.versions[0].type !== "notion" &&- doc.document.versions[0].storageType !== "VERCEL_BLOB",- );-- // For bulk downloads, only store metadata if there are less than 100 documents- const downloadMetadata =- downloadableDocuments.length < 100- ? {- dataroomName: view.dataroom!.name,- documentCount: downloadableDocuments.length,- documents: downloadableDocuments.map((doc) => ({- id: doc.document.id,- name: doc.document.name,- })),- }- : {- dataroomName: view.dataroom!.name,- documentCount: downloadableDocuments.length,- };-- await prisma.view.createMany({- data: downloadableDocuments.map((doc) => ({- viewType: "DOCUMENT_VIEW",- documentId: doc.document.id,- linkId: linkId,- dataroomId: view.dataroom!.id,- groupId: view.groupId,- dataroomViewId: view.id,- viewerEmail: view.viewerEmail,- downloadedAt: new Date(),- downloadType: "BULK",- downloadMetadata: downloadMetadata,- viewerId: view.viewerId,- verified: view.verified,- })),- skipDuplicates: true,- });-- // Construct folderStructure and fileKeys- const folderStructure: {- [key: string]: {- name: string;- path: string;- files: {- name: string;- key: string;- type?: string;- numPages?: number;- needsWatermark?: boolean;- size?: number; // File size in bytes- }[];- };- } = {};- const fileKeys: string[] = [];-- const addFileToStructure = (- path: string,- fileName: string,- fileKey: string,- fileType?: string,- numPages?: number,- fileSize?: number,- ) => {- const pathParts = path.split("/").filter(Boolean);- let currentPath = "";-- // Add folder information for each level of the path- pathParts.forEach((part, index) => {- currentPath += "/" + part;- const folderInfo = folderMap.get(currentPath);- if (!folderStructure[currentPath]) {- folderStructure[currentPath] = {- name: folderInfo ? folderInfo.name : part,- path: currentPath,- files: [],- };- }- });-- // Add the file to the leaf folder- if (!folderStructure[path]) {- const folderInfo = folderMap.get(path) || { name: "Root", id: null };- folderStructure[path] = {- name: folderInfo.name,- path: path,- files: [],- };- }-- const needsWatermark =- view.link.enableWatermark &&- (fileType === "pdf" || fileType === "image");-- folderStructure[path].files.push({- name: fileName,- key: fileKey,- type: fileType,- numPages: numPages,- needsWatermark: needsWatermark ?? undefined,- size: fileSize,- });- fileKeys.push(fileKey);- };-- // Add root level documents- downloadDocuments- .filter((doc) => !doc.folderId)- .filter((doc) => doc.document.versions[0].type !== "notion")- .filter((doc) => doc.document.versions[0].storageType !== "VERCEL_BLOB")- .forEach((doc) => {- const fileKey =- view.link.enableWatermark && doc.document.versions[0].type === "pdf"- ? doc.document.versions[0].file- : (doc.document.versions[0].originalFile ??- doc.document.versions[0].file);-- addFileToStructure(- "/",- doc.document.name,- fileKey,- doc.document.versions[0].type ?? undefined,- doc.document.versions[0].numPages ?? undefined,- doc.document.versions[0].fileSize- ? Number(doc.document.versions[0].fileSize)- : undefined,- );- });-- // Pre-index documents by folderId for O(1) lookup per folder- const docsByFolderId = new Map<string, typeof downloadDocuments>();- for (const doc of downloadDocuments) {- if (!doc.folderId) continue;- const list = docsByFolderId.get(doc.folderId) ?? [];- list.push(doc);- docsByFolderId.set(doc.folderId, list);- }-- // Add documents in folders- downloadFolders.forEach((folder) => {- // Use the computed path from parentId hierarchy instead of the stored path- const folderPath = computedPathMap.get(folder.id) ?? folder.path;-- const folderDocs = (docsByFolderId.get(folder.id) ?? [])- .filter((doc) => doc.document.versions[0].type !== "notion")- .filter(- (doc) => doc.document.versions[0].storageType !== "VERCEL_BLOB",- );-- folderDocs &&- folderDocs.forEach((doc) => {- // Use .file if watermark is enabled and document is PDF, otherwise use .originalFile- const fileKey =- view.link.enableWatermark && doc.document.versions[0].type === "pdf"- ? doc.document.versions[0].file- : (doc.document.versions[0].originalFile ??- doc.document.versions[0].file);-- addFileToStructure(- folderPath,- doc.document.name,- fileKey,- doc.document.versions[0].type ?? undefined,- doc.document.versions[0].numPages ?? undefined,- doc.document.versions[0].fileSize- ? Number(doc.document.versions[0].fileSize)- : undefined,- );- });-- // If the folder is empty, ensure it's still added to the structure- if (folderDocs && folderDocs.length === 0) {- addFileToStructure(folderPath, "", "");- }- });-- if (fileKeys.length === 0) {- return res.status(404).json({ error: "No files to download" });- }-- if (view.dataroom?.teamId) {- void notifyDocumentDownload({- teamId: view.dataroom.teamId,- documentId: undefined,- dataroomId: view.dataroom.id,- linkId,- viewerEmail: view.viewerEmail ?? undefined,- viewerId: view.viewerId ?? undefined,- metadata: {- documentCount: downloadDocuments.length,- isBulkDownload: true,- },- }).catch((err) =>- console.error("Error sending Slack notification:", err),- );- }-- const teamId = view.dataroom!.teamId;- const storageConfig = await getTeamStorageConfigById(teamId);+ const teamId = view.dataroom.teamId;const sendEmail =!!emailNotification && !!view.viewerEmail && !!session.verified;const job = await downloadJobStore.createJob({type: "bulk",status: "PENDING",- dataroomId: view.dataroom!.id,- dataroomName: view.dataroom!.name,- totalFiles: fileKeys.length,+ dataroomId: view.dataroom.id,+ dataroomName: view.dataroom.name,+ // The task fills in the real total once it builds the folder+ // structure; computing it here would defeat the purpose of moving the+ // heavy queries off of the request path.+ totalFiles: 0,processedFiles: 0,progress: 0,teamId,@@ -459,12 +164,9 @@ export default async function handle(const handle = await bulkDownloadTask.trigger({jobId: job.id,- dataroomId: view.dataroom!.id,- dataroomName: view.dataroom!.name,+ dataroomId: view.dataroom.id,+ dataroomName: view.dataroom.name,teamId,- folderStructure,- fileKeys,- sourceBucket: storageConfig.bucket,watermarkConfig: view.link.enableWatermark? {enabled: true,@@ -490,12 +192,24 @@ export default async function handle(linkId,emailNotification: sendEmail,emailAddress: sendEmail ? (view.viewerEmail ?? undefined) : undefined,+ sourceContext: {+ type: "bulk",+ linkId,+ viewId: view.id,+ viewerId: view.viewerId ?? undefined,+ viewerEmail: view.viewerEmail ?? undefined,+ groupId: view.groupId ?? undefined,+ permissionGroupId: view.link.permissionGroupId ?? undefined,+ verified: view.verified ?? false,+ enableWatermark: !!view.link.enableWatermark,+ notifySlack: true,+ },},{idempotencyKey: job.id,tags: [`team_${teamId}`,- `dataroom_${view.dataroom!.id}`,+ `dataroom_${view.dataroom.id}`,`job_${job.id}`,`link_${linkId}`,],diff --git a/pages/api/links/download/dataroom-folder.ts b/pages/api/links/download/dataroom-folder.tsindex eaea6369..9c66c47b 100644--- a/pages/api/links/download/dataroom-folder.ts+++ b/pages/api/links/download/dataroom-folder.ts@@ -1,21 +1,17 @@import { NextApiRequest, NextApiResponse } from "next";-import { getTeamStorageConfigById } from "@/ee/features/storage/config";-import { ItemType, ViewType } from "@prisma/client";+import { ViewType } from "@prisma/client";import { verifyDataroomSessionInPagesRouter } from "@/lib/auth/dataroom-auth";-import {- buildFolderPathsFromHierarchy,- collectDescendantIds,-} from "@/lib/dataroom/build-folder-hierarchy";-import { notifyDocumentDownload } from "@/lib/integrations/slack/events";import prisma from "@/lib/prisma";import { downloadJobStore } from "@/lib/redis-download-job-store";import { bulkDownloadTask } from "@/lib/trigger/bulk-download";-import { safeSlugify } from "@/lib/utils";import { getIpAddress } from "@/lib/utils/ip";export const config = {+ // Lightweight handler: validate access + create job + trigger task. The+ // heavy folder/document/permission queries and view inserts run inside the+ // trigger task so the viewer never sees a request timeout.maxDuration: 60,};@@ -105,22 +101,18 @@ export default async function handler(return res.status(403).json({ error: "Error downloading" });}- // if link is archived, we should not allow the downloadif (view.link.isArchived) {return res.status(403).json({ error: "Error downloading" });}- // if link is deleted, we should not allow the downloadif (view.link.deletedAt) {return res.status(403).json({ error: "Error downloading" });}- // if link is expired, we should not allow the downloadif (view.link.expiresAt && view.link.expiresAt < new Date()) {return res.status(403).json({ error: "Error downloading" });}- // if viewedAt is longer than 23 hours ago, we should not allow the downloadif (view.viewedAt &&view.viewedAt < new Date(Date.now() - 23 * 60 * 60 * 1000)@@ -128,323 +120,24 @@ export default async function handler(return res.status(403).json({ error: "Error downloading" });}+ // Cheap existence check: confirm the folder belongs to this dataroom and+ // grab the dataroom name in one round trip. The task will reload the+ // folder hierarchy itself.const rootFolder = await prisma.dataroomFolder.findUnique({- where: {- id: folderId,- dataroomId,- },- select: { id: true, name: true, path: true, parentId: true },- });-- if (!rootFolder) {- return res.status(404).json({ error: "Folder not found" });- }-- // Fetch all folders in this dataroom so we can traverse the parentId- // hierarchy. This avoids relying on the materialized `path` field which- // can become stale after renames/moves.- const allDataroomFolders = await prisma.dataroomFolder.findMany({- where: { dataroomId },- select: { id: true, name: true, path: true, parentId: true },- });-- // Collect descendants via parentId chain (source of truth)- const descendantIds = collectDescendantIds(- rootFolder.id,- allDataroomFolders,- );- const subfolders = allDataroomFolders.filter((f) =>- descendantIds.has(f.id),- );-- // Keep the full (unfiltered) folder list for path computation below.- // Permission filtering may remove parent folders that only have canView- // (not canDownload), which would break child path computation.- const fullFolders = [rootFolder, ...subfolders];- let allFolders = [...fullFolders];- let allDocuments = await prisma.dataroomDocument.findMany({- where: {- dataroomId,- folderId: {- in: allFolders.map((f) => f.id),- },- },+ where: { id: folderId, dataroomId },select: {id: true,- folderId: true,- document: {- select: {- id: true,- name: true,- versions: {- where: { isPrimary: true },- select: {- type: true,- file: true,- storageType: true,- originalFile: true,- numPages: true,- contentType: true,- fileSize: true,- },- take: 1,- },- },- },+ name: true,+ dataroom: { select: { name: true } },},});- // Check permissions based on groupId (ViewerGroup) or permissionGroupId (PermissionGroup)- const effectiveGroupId = view.groupId || view.link.permissionGroupId;-- if (effectiveGroupId) {- let groupPermissions: any[] = [];-- if (view.groupId) {- // This is a ViewerGroup (legacy behavior)- groupPermissions = await prisma.viewerGroupAccessControls.findMany({- where: { groupId: view.groupId, canDownload: true },- });- } else if (view.link.permissionGroupId) {- // This is a PermissionGroup (new behavior)- groupPermissions = await prisma.permissionGroupAccessControls.findMany({- where: { groupId: view.link.permissionGroupId, canDownload: true },- });- }-- const permittedFolderIds = new Set(- groupPermissions- .filter(- (permission) => permission.itemType === ItemType.DATAROOM_FOLDER,- )- .map((permission) => permission.itemId),- );- const permittedDocumentIds = new Set(- groupPermissions- .filter(- (permission) => permission.itemType === ItemType.DATAROOM_DOCUMENT,- )- .map((permission) => permission.itemId),- );-- allFolders = allFolders.filter((folder) =>- permittedFolderIds.has(folder.id),- );- allDocuments = allDocuments.filter((doc) =>- permittedDocumentIds.has(doc.id),- );- }-- // Build folder paths from the FULL (unfiltered) folder hierarchy so that- // parent folders removed by permission filtering are still present in the- // path map, producing correct child paths (e.g. "/legal/contracts" instead- // of just "/contracts").- const computedPathMap = buildFolderPathsFromHierarchy(fullFolders);-- // Compute the root folder's path from the hierarchy- const computedRootPath =- computedPathMap.get(rootFolder.id) ?? rootFolder.path;-- const folderStructure: {- [key: string]: {- name: string;- path: string;- files: {- name: string;- key: string;- type?: string;- numPages?: number;- needsWatermark?: boolean;- size?: number;- }[];- };- } = {};-- const fileKeys: string[] = [];-- const addFileToStructure = (- computedFolderPath: string,- rootFolderInfo: { name: string; computedPath: string },- fileName: string,- fileKey: string,- fileType?: string,- numPages?: number,- fileSize?: number,- ) => {- let relativePath = "";- if (computedFolderPath !== rootFolderInfo.computedPath) {- const escapedRoot = rootFolderInfo.computedPath.replace(- /[.*+?^${}()|[\]\\]/g,- "\\$&",- );- const pathRegex = new RegExp(`^${escapedRoot}/(.*)$`);- const match = computedFolderPath.match(pathRegex);- relativePath = match ? match[1] : "";- }-- const pathParts = [safeSlugify(rootFolderInfo.name)];- if (relativePath) {- pathParts.push(...relativePath.split("/").filter(Boolean));- }-- let currentPath = "";- for (const part of pathParts) {- currentPath += "/" + part;- if (!folderStructure[currentPath]) {- folderStructure[currentPath] = {- name: part,- path: currentPath,- files: [],- };- }- }-- if (fileName && fileKey) {- const needsWatermark =- view.link.enableWatermark &&- (fileType === "pdf" || fileType === "image");-- folderStructure[currentPath].files.push({- name: fileName,- key: fileKey,- type: fileType,- numPages: numPages,- needsWatermark: needsWatermark ?? undefined,- size: fileSize,- });- fileKeys.push(fileKey);- }- };-- const rootFolderInfo = {- name: rootFolder.name,- computedPath: computedRootPath,- };-- // Pre-index documents by folderId for O(1) lookup per folder- const docsByFolderId = new Map<string, typeof allDocuments>();- for (const doc of allDocuments) {- if (!doc.folderId) continue;- const list = docsByFolderId.get(doc.folderId) ?? [];- list.push(doc);- docsByFolderId.set(doc.folderId, list);- }-- for (const folder of allFolders) {- const folderPath = computedPathMap.get(folder.id) ?? folder.path;- const docs = docsByFolderId.get(folder.id) ?? [];-- if (docs.length === 0) {- addFileToStructure(- folderPath,- rootFolderInfo,- "",- "",- undefined,- undefined,- );- continue;- }-- for (const doc of docs) {- const version = doc.document.versions[0];- if (- !version ||- version.type === "notion" ||- version.storageType === "VERCEL_BLOB"- )- continue;-- // Use .file if watermark is enabled and document is PDF, otherwise use .originalFile- const fileKey =- view.link.enableWatermark && version.type === "pdf"- ? version.file- : (version.originalFile ?? version.file);- addFileToStructure(- folderPath,- rootFolderInfo,- doc.document.name,- fileKey,- version.type ?? undefined,- version.numPages ?? undefined,- version.fileSize ? Number(version.fileSize) : undefined,- );- }- }-- const rootPath = "/" + safeSlugify(rootFolder.name);- if (!folderStructure[rootPath]) {- folderStructure[rootPath] = {- name: safeSlugify(rootFolder.name),- path: rootPath,- files: [],- };- }-- // Don't update the DATAROOM_VIEW with downloadedAt for folder downloads- // Only bulk downloads should update the DATAROOM_VIEW-- // Create individual document views for each document in the folder- const downloadableDocuments = allDocuments.filter(- (doc) =>- doc.document.versions[0] &&- doc.document.versions[0].type !== "notion" &&- doc.document.versions[0].storageType !== "VERCEL_BLOB",- );-- // Prepare metadata with folder name and document list- const downloadMetadata = {- folderName: rootFolder.name,- folderPath: rootFolder.path,- documents: downloadableDocuments.map((doc) => ({- id: doc.document.id,- name: doc.document.name,- })),- };-- await prisma.view.createMany({- data: downloadableDocuments.map((doc) => ({- viewType: "DOCUMENT_VIEW",- documentId: doc.document.id,- linkId: linkId,- dataroomId: dataroomId,- groupId: view.groupId,- dataroomViewId: view.id,- viewerEmail: view.viewerEmail,- downloadedAt: new Date(),- downloadType: "FOLDER",- downloadMetadata: downloadMetadata,- viewerId: view.viewerId,- verified: view.verified,- })),- skipDuplicates: true,- });-- if (view.link.teamId) {- void notifyDocumentDownload({- teamId: view.link.teamId,- documentId: undefined,- dataroomId,- linkId,- viewerEmail: view.viewerEmail ?? undefined,- viewerId: view.viewerId ?? undefined,- metadata: {- folderName: rootFolder.name,- documentCount: allDocuments.length,- isFolderDownload: true,- },- }).catch((err) =>- console.error("Error sending Slack notification:", err),- );+ if (!rootFolder) {+ return res.status(404).json({ error: "Folder not found" });}const teamId = view.link.teamId!;- const storageConfig = await getTeamStorageConfigById(teamId);- const dataroom = await prisma.dataroom.findUnique({- where: { id: dataroomId },- select: { name: true },- });- const dataroomName = dataroom?.name ?? "Dataroom";+ const dataroomName = rootFolder.dataroom?.name ?? "Dataroom";const sendEmail =!!emailNotification && !!view.viewerEmail && !!session.verified;@@ -454,7 +147,10 @@ export default async function handler(dataroomId,dataroomName,folderName: rootFolder.name,- totalFiles: fileKeys.length,+ // The task fills in the real total once it finishes building the+ // folder structure; we don't know it yet without doing the heavy+ // queries the task is meant to perform.+ totalFiles: 0,processedFiles: 0,progress: 0,teamId,@@ -472,9 +168,6 @@ export default async function handler(dataroomId,dataroomName,teamId,- folderStructure,- fileKeys,- sourceBucket: storageConfig.bucket,watermarkConfig: view.link.enableWatermark? {enabled: true,@@ -499,6 +192,19 @@ export default async function handler(emailNotification: sendEmail,emailAddress: sendEmail ? (view.viewerEmail ?? undefined) : undefined,folderName: rootFolder.name,+ sourceContext: {+ type: "folder",+ folderId,+ linkId,+ viewId: view.id,+ viewerId: view.viewerId ?? undefined,+ viewerEmail: view.viewerEmail ?? undefined,+ groupId: view.groupId ?? undefined,+ permissionGroupId: view.link.permissionGroupId ?? undefined,+ verified: view.verified ?? false,+ enableWatermark: !!view.link.enableWatermark,+ notifySlack: true,+ },},{idempotencyKey: job.id,@@ -524,7 +230,6 @@ export default async function handler(console.error("Download error:", error);return res.status(500).json({message: "Internal Server Error",- error: (error as Error).message,});}}diff --git a/pages/api/links/download/jobs.ts b/pages/api/links/download/jobs.tsindex 2e289cef..85b1eb75 100644--- a/pages/api/links/download/jobs.ts+++ b/pages/api/links/download/jobs.ts@@ -53,6 +53,7 @@ export default async function handler(return {id: job.id,status: job.status,+ phase: job.phase,progress: job.progress,totalFiles: job.totalFiles,processedFiles: job.processedFiles,diff --git a/pages/api/teams/[teamId]/datarooms/[id]/download/[jobId].ts b/pages/api/teams/[teamId]/datarooms/[id]/download/[jobId].tsindex bc81b331..5dd288c6 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/download/[jobId].ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/download/[jobId].ts@@ -92,6 +92,7 @@ export default async function handler(return res.status(200).json({id: job.id,status: job.status,+ phase: job.phase,progress: job.progress,totalFiles: job.totalFiles,processedFiles: job.processedFiles,@@ -110,7 +111,6 @@ export default async function handler(console.error("Error fetching download:", error);return res.status(500).json({error: "Failed to fetch download",- details: (error as Error).message,});}}