Skip to content

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

feat: improve dataroom folder upload traversal perf b08f

Marc Seitz3eb63fbd9a47
diff --git a/components/datarooms/actions/remove-document-modal.tsx b/components/datarooms/actions/remove-document-modal.tsx
index 404ec06f..16201878 100644
--- a/components/datarooms/actions/remove-document-modal.tsx
+++ b/components/datarooms/actions/remove-document-modal.tsx
@@ -101,10 +101,10 @@ function RemoveDataroomItemsModal({
// Call mutate only once, after all deletions
await mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}${folderPathName ? `/folders/documents/${folderPathName.join("/")}` : "/documents"}`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}${folderPathName ? `/folder-documents/${folderPathName.join("/")}` : "/documents"}`,
);
mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders${folderPathName ? `/${folderPathName.join(" / ")}` : "?root=true"}`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders${folderPathName ? `/${folderPathName.join("/")}` : "?root=true"}`,
);
mutate(
`/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders`,
diff --git a/components/datarooms/dataroom-document-card.tsx b/components/datarooms/dataroom-document-card.tsx
index 2ecd72f6..f55ff518 100644
--- a/components/datarooms/dataroom-document-card.tsx
+++ b/components/datarooms/dataroom-document-card.tsx
@@ -139,7 +139,7 @@ export default function DataroomDocumentCard({
}
const endpoint = currentFolderPath
- ? `/folders/documents/${currentFolderPath.join("/")}`
+ ? `/folder-documents/${currentFolderPath.join("/")}`
: "/documents";
toast.promise(
diff --git a/components/datarooms/edit-dataroom-document-modal.tsx b/components/datarooms/edit-dataroom-document-modal.tsx
index 41a2db15..85c1c266 100644
--- a/components/datarooms/edit-dataroom-document-modal.tsx
+++ b/components/datarooms/edit-dataroom-document-modal.tsx
@@ -188,7 +188,7 @@ export function EditDataroomDocumentModal({
if (currentFolderPath) {
mutate(
- `${baseKey}/folders/documents/${currentFolderPath.join("/")}`,
+ `${baseKey}/folder-documents/${currentFolderPath.join("/")}`,
null,
{
populateCache: (_, docs) =>
diff --git a/components/datarooms/sortable/sortable-list.tsx b/components/datarooms/sortable/sortable-list.tsx
index 7b50535a..44b1a0e8 100644
--- a/components/datarooms/sortable/sortable-list.tsx
+++ b/components/datarooms/sortable/sortable-list.tsx
@@ -150,7 +150,7 @@ export function DataroomSortableList({
`${baseKey}/folders${folderPathName ? `/${folderPathName.join("/")}` : "?root=true"}`,
);
mutate(
- `${baseKey}${folderPathName ? `/folders/documents/${folderPathName.join("/")}` : "/documents"}`,
+ `${baseKey}${folderPathName ? `/folder-documents/${folderPathName.join("/")}` : "/documents"}`,
);
mutate(`${baseKey}/folders`);
mutate(`${baseKey}/folders?include_documents=true`);
diff --git a/components/documents/actions/delete-documents-modal.tsx b/components/documents/actions/delete-documents-modal.tsx
index c99fbae6..d4cf371a 100644
--- a/components/documents/actions/delete-documents-modal.tsx
+++ b/components/documents/actions/delete-documents-modal.tsx
@@ -139,7 +139,7 @@ function DeleteItemsModal({
await mutate(
`/api/teams/${
teamInfo?.currentTeam?.id
- }/folders/documents/${folderPathName.join("/")}`,
+ }/folder-documents/${folderPathName.join("/")}`,
);
} else {
const { search, sort, page, limit } = router.query;
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index 410e687b..cd4ae0ef 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -131,7 +131,7 @@ export function AddDocumentModal({
`/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/documents`,
);
mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders/documents/${folderPathName}`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folder-documents/${folderPathName}`,
);
toast.success("Document added to dataroom successfully! πŸŽ‰");
diff --git a/components/documents/document-card.tsx b/components/documents/document-card.tsx
index fede4f82..473d65a1 100644
--- a/components/documents/document-card.tsx
+++ b/components/documents/document-card.tsx
@@ -147,7 +147,7 @@ export default function DocumentsCard({
const queryString = queryParts.length > 0 ? `?${queryParts.join("&")}` : "";
const endpoint = currentFolderPath
- ? `/folders/documents/${currentFolderPath.join("/")}`
+ ? `/folder-documents/${currentFolderPath.join("/")}`
: `/documents${queryString}`;
toast.promise(
@@ -221,7 +221,7 @@ export default function DocumentsCard({
).then(() => {
mutate(`/api/teams/${teamInfo?.currentTeam?.id}/documents`);
mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/folders/documents/${currentFolderPath?.join("/")}`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/folder-documents/${currentFolderPath?.join("/")}`,
);
}),
{
@@ -249,7 +249,7 @@ export default function DocumentsCard({
const queryString = queryParts.length > 0 ? `?${queryParts.join("&")}` : "";
const endpoint = currentFolderPath
- ? `/folders/documents/${currentFolderPath.join("/")}`
+ ? `/folder-documents/${currentFolderPath.join("/")}`
: `/documents${queryString}`;
toast.promise(
diff --git a/components/documents/documents-list.tsx b/components/documents/documents-list.tsx
index 6a767dd2..b4253087 100644
--- a/components/documents/documents-list.tsx
+++ b/components/documents/documents-list.tsx
@@ -403,7 +403,7 @@ export function DocumentsList({
`/api/teams/${teamInfo?.currentTeam?.id}/folders/${folderPathName.join("/")}`,
);
mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/folders/documents/${folderPathName.join("/")}`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/folder-documents/${folderPathName.join("/")}`,
);
}
diff --git a/components/upload-zone.tsx b/components/upload-zone.tsx
index dc0d67b3..2f777415 100644
--- a/components/upload-zone.tsx
+++ b/components/upload-zone.tsx
@@ -18,9 +18,10 @@ import {
import { DocumentData, createDocument } from "@/lib/documents/create-document";
import { resumableUpload } from "@/lib/files/tus-upload";
import {
- createFolderInBoth,
+ BulkFolderRequestItem,
+ BulkFolderResultItem,
+ bulkCreateFoldersChunked,
createFolderInMainDocs,
- determineFolderPaths,
isSystemFile,
} from "@/lib/folders/create-folder";
import { usePlan } from "@/lib/swr/use-billing";
@@ -35,9 +36,10 @@ import {
} from "@/lib/utils/get-file-size-limits";
import { getPagesCount } from "@/lib/utils/get-page-number-count";
-// Originally these mime values were directly used in the dropzone hook.
-// There was a solid reason to take them out of the scope, primarily to solve a browser compatibility issue to determine the file type when user dropped a folder.
-// you will figure out how this change helped to fix the compatibility issue once you have went through reading of `getFilesFromDropEvent` and `traverseFolder`
+// These mime values are kept out of useDropzone's `accept` to keep the file
+// type fallback path in getFilesFromEvent reachable (some browsers, notably
+// Firefox, can't detect MIME type for files yielded from a dropped folder and
+// need this lookup table to fix it up).
const acceptableDropZoneMimeTypesWhenIsFreePlanAndNotTrial =
FREE_PLAN_ACCEPTED_FILE_TYPES;
const allAcceptableDropZoneMimeTypes = FULL_PLAN_ACCEPTED_FILE_TYPES;
@@ -97,6 +99,16 @@ export interface UploadBatchState {
const UPLOAD_CONCURRENCY = 5;
+/**
+ * Window during which per-file SWR mutate keys are coalesced. The upload
+ * loop fires up to 4 mutates per completed file; without batching, 400 files
+ * produces 1600 GET refetches that re-fetch unpaginated folder trees and
+ * saturate the per-origin connection pool. With a small debounce window
+ * each unique key fires at most once per window, regardless of how many
+ * files completed in that interval.
+ */
+const MUTATE_FLUSH_INTERVAL_MS = 500;
+
async function processWithConcurrency<T>(
tasks: (() => Promise<T>)[],
concurrency: number,
@@ -115,6 +127,50 @@ async function processWithConcurrency<T>(
return results;
}
+/** Coalesces SWR mutate calls by key, flushing on a fixed interval + on demand. */
+function createMutateQueue(intervalMs: number) {
+ const pending = new Set<string>();
+ let timer: ReturnType<typeof setTimeout> | null = null;
+
+ const flush = () => {
+ if (timer) {
+ clearTimeout(timer);
+ timer = null;
+ }
+ if (pending.size === 0) return;
+ const keys = Array.from(pending);
+ pending.clear();
+ for (const key of keys) mutate(key);
+ };
+
+ return {
+ enqueue(key: string | undefined | null | false) {
+ if (!key) return;
+ pending.add(key);
+ if (!timer) timer = setTimeout(flush, intervalMs);
+ },
+ flush,
+ };
+}
+
+function stripLeadingSlash(p: string | null | undefined): string | undefined {
+ if (!p) return undefined;
+ return p.startsWith("/") ? p.slice(1) : p;
+}
+
+/**
+ * Parent path (without leading slash) of a folder path expressed in the
+ * client-side "no leading slash" format. Returns undefined when the parent is
+ * the root (i.e. the path is empty, "/", or a single top-level segment) β€”
+ * callers should fall back to the `?root=true` key in that case.
+ */
+function parentPathOf(p: string | null | undefined): string | undefined {
+ if (!p) return undefined;
+ const segments = p.split("/").filter(Boolean);
+ if (segments.length <= 1) return undefined;
+ return segments.slice(0, -1).join("/");
+}
+
export interface UploadedTopLevelFolder {
/** Database id of the dataroom folder */
dataroomFolderId: string;
@@ -553,6 +609,8 @@ export default function UploadZone({
});
};
+ const mutateQueue = createMutateQueue(MUTATE_FLUSH_INTERVAL_MS);
+
const uploadTasks = validatedFiles.valid.map((file) => async () => {
if (dropCancelled.current) return undefined;
@@ -669,12 +727,31 @@ export default function UploadZone({
folderPathName: fileUploadPathName,
});
- mutate(`/api/teams/${teamInfo?.currentTeam?.id}/documents`);
-
- fileUploadPathName &&
- mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/folders/documents/${fileUploadPathName}`,
- );
+ mutateQueue.enqueue(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents`,
+ );
+ mutateQueue.enqueue(
+ fileUploadPathName &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/folder-documents/${fileUploadPathName}`,
+ );
+ // Refresh folder-list keys so per-folder `_count.documents` updates
+ // while the upload is in progress: root (always), the user's
+ // current view, and the parent of the folder this specific file
+ // lands in (so a subfolder card the user is viewing ticks up). The
+ // queue dedupes by key + flushes on a debounce window, so this is
+ // bounded to ~one refresh per 500 ms regardless of file count.
+ mutateQueue.enqueue(
+ `/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}?root=true`,
+ );
+ mutateQueue.enqueue(
+ folderPathName &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}/${folderPathName}`,
+ );
+ const fileParentPath = parentPathOf(fileUploadPathName);
+ mutateQueue.enqueue(
+ fileParentPath &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/folders/${fileParentPath}`,
+ );
const document = await response.json();
let dataroomResponse;
@@ -703,13 +780,20 @@ export default function UploadZone({
return undefined;
}
- mutate(
+ mutateQueue.enqueue(
`/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/documents`,
);
- (dataroomUploadPathName || fileUploadPathName) &&
- mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders/documents/${dataroomUploadPathName || fileUploadPathName}`,
- );
+ mutateQueue.enqueue(
+ (dataroomUploadPathName || fileUploadPathName) &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folder-documents/${dataroomUploadPathName || fileUploadPathName}`,
+ );
+ const dataroomParentPath = parentPathOf(
+ dataroomUploadPathName || fileUploadPathName,
+ );
+ mutateQueue.enqueue(
+ dataroomParentPath &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders/${dataroomParentPath}`,
+ );
} catch (error) {
console.error(
"An error occurred while adding document to the dataroom: ",
@@ -766,14 +850,30 @@ export default function UploadZone({
try {
const results = await processWithConcurrency(uploadTasks, UPLOAD_CONCURRENCY);
- mutate(
+ mutateQueue.enqueue(
`/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}?root=true`,
);
- mutate(`/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}`);
- folderPathName &&
- mutate(
+ mutateQueue.enqueue(
+ `/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}`,
+ );
+ mutateQueue.enqueue(
+ folderPathName &&
`/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}/${folderPathName}`,
+ );
+
+ // When a dataroom drop also replicates folders into "All Documents",
+ // refresh the main-docs caches at end of batch. Single revalidation
+ // covers what the now-removed per-folder mutate() calls used to do.
+ if (dataroomId && replicateDataroomFolders) {
+ mutateQueue.enqueue(
+ `/api/teams/${teamInfo?.currentTeam?.id}/folders?root=true`,
);
+ mutateQueue.enqueue(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents`,
+ );
+ }
+
+ mutateQueue.flush();
const uploadedDocuments = results.filter(Boolean);
const dataroomDocuments = uploadedDocuments.map((document: any) => ({
@@ -801,6 +901,8 @@ export default function UploadZone({
onUploadSuccess?.(dataroomDocuments, uploadedFolders);
} catch (error) {
console.error("Upload batch failed:", error);
+ } finally {
+ mutateQueue.flush();
}
},
[
@@ -814,17 +916,18 @@ export default function UploadZone({
isPaused,
hasDocumentLimit,
remainingDocuments,
+ dataroomId,
+ replicateDataroomFolders,
],
);
const getFilesFromEvent = useCallback(
async (event: DropEvent) => {
- // This callback also run when event.type =`dragenter`. We only need to compute files when the event.type is `drop`.
+ // useDropzone invokes getFilesFromEvent for dragenter too; only react to drop/change.
if ("type" in event && event.type !== "drop" && event.type !== "change") {
return [];
}
- // Extract top-level item names from the drop so the drawer can show them immediately
let preliminaryItems: { name: string; isFolder: boolean }[] | undefined;
if ("dataTransfer" in event && event.dataTransfer) {
preliminaryItems = Array.from(
@@ -861,366 +964,341 @@ export default function UploadZone({
? Math.max(0, remainingDocuments)
: Infinity;
const fileLimit = Math.min(maxFilesPerUpload, planDocumentLimit);
- let collectedFileCount = 0;
- const skippedPerTopLevel = new Map<string, string[]>();
- // Early check: skip folder traversal (and folder creation) if document limit is already reached
- if (fileLimit <= 0) {
+ if (fileLimit <= 0) return [];
+
+ // ----- Plain <input type="file"> path: no folder traversal needed.
+ if (
+ "target" in event &&
+ event.target &&
+ event.target instanceof HTMLInputElement &&
+ event.target.files
+ ) {
+ const out: FileWithPaths[] = [];
+ for (let i = 0; i < event.target.files.length; i++) {
+ if (out.length >= fileLimit) break;
+ const file: FileWithPaths = event.target.files[i];
+ file.path = file.name;
+ file.whereToUploadPath = folderPathName;
+ file.dataroomUploadPath = folderPathName;
+ file.topLevelItemName = file.name;
+ file.topLevelItemIsFolder = false;
+ out.push(file);
+ }
+ if (out.length < event.target.files.length) {
+ fileLimitTruncatedRef.current = true;
+ }
+ return out;
+ }
+
+ if (!("dataTransfer" in event) || !event.dataTransfer) return [];
+ if (!teamInfo?.currentTeam?.id) {
+ setRejectedFiles((prev) => [
+ { fileName: "Unknown Team", message: "Team Id not found" },
+ ...prev,
+ ]);
return [];
}
- let filesToBePassedToOnDrop: FileWithPaths[] = [];
+ const teamId = teamInfo.currentTeam.id;
+ const skippedPerTopLevel = new Map<string, string[]>();
- /**
- * Reads all entries from a FileSystemDirectoryReader.
- * Per spec, readEntries() returns at most ~100 entries per call in
- * Chromium browsers. It must be called repeatedly until it returns
- * an empty array.
- */
const readAllDirectoryEntries = async (
dirReader: FileSystemDirectoryReader,
): Promise<FileSystemEntry[]> => {
const allEntries: FileSystemEntry[] = [];
-
- let batch: FileSystemEntry[] = await new Promise<FileSystemEntry[]>(
- (resolve, reject) => dirReader.readEntries(resolve, reject),
+ let batch = await new Promise<FileSystemEntry[]>((resolve, reject) =>
+ dirReader.readEntries(resolve, reject),
);
-
while (batch.length > 0) {
allEntries.push(...batch);
batch = await new Promise<FileSystemEntry[]>((resolve, reject) =>
dirReader.readEntries(resolve, reject),
);
}
-
return allEntries;
};
- /** *********** START OF `traverseFolder` *********** */
- const traverseFolder = async (
- entry: FileSystemEntry,
- parentPathOfThisEntry?: string,
- dataroomParentPath?: string,
- folderCounter?: { count: number },
- folderPathCapture?: { value?: string },
- topLevelName?: string,
- folderIdCapture?: { value?: string },
- ): Promise<FileWithPaths[]> => {
- /**
- * Summary of this function:
- * 1. if it find a folder then corresponding folder will be created at backend.
- * 2. Smoothly handles the deeply nested folders.
- * 3. Upon folder creation it assign the path and whereToUploadPath to each entry. (Those values will be helpful for `onDrop` to upload document correctly)
- */
-
- let files: FileWithPaths[] = [];
-
- if (isSystemFile(entry.name)) {
- return files;
- }
+ // Per-file row collected during the walk, before bulk folder creation.
+ type PendingFile = {
+ entry: FileSystemFileEntry;
+ parentTempId: string | null;
+ topLevelTempId: string | null;
+ topLevelName: string;
+ topLevelIsFolder: boolean;
+ };
- if (entry.isDirectory) {
- try {
+ // Walks one top-level entry, collecting folder rows + file rows. No
+ // network I/O happens here β€” that's deferred to a single bulk POST.
+ const walkTopLevel = async (
+ topEntry: FileSystemEntry,
+ ): Promise<{
+ folders: BulkFolderRequestItem[];
+ files: PendingFile[];
+ topLevelTempId: string | null;
+ }> => {
+ const folders: BulkFolderRequestItem[] = [];
+ const files: PendingFile[] = [];
+ let topLevelTempId: string | null = null;
+
+ const recurse = async (
+ entry: FileSystemEntry,
+ parentTempId: string | null,
+ ): Promise<void> => {
+ if (isSystemFile(entry.name)) return;
+
+ if (entry.isDirectory) {
if (entry.name.trim() === "") {
setRejectedFiles((prev) => [
- {
- fileName: entry.name,
- message: "Folder name cannot be empty",
- },
- ...prev,
- ]);
- throw new Error("Folder name cannot be empty");
- }
-
- if (!teamInfo?.currentTeam?.id) {
- /** This case probably may not happen */
- setRejectedFiles((prev) => [
- {
- fileName: "Unknown Team",
- message: "Team Id not found",
- },
+ { fileName: entry.name, message: "Folder name cannot be empty" },
...prev,
]);
- throw new Error("No team found");
+ return;
}
+ const tempId = crypto.randomUUID();
+ folders.push({
+ tempId,
+ name: entry.name,
+ parentTempId,
+ });
+ if (topLevelTempId === null) topLevelTempId = tempId;
- // Create folder in main documents if not in dataroom
- if (!dataroomId) {
- // Create folder in main documents only
- const { path: folderPath } = await createFolderInMainDocs({
- teamId: teamInfo.currentTeam.id,
- name: entry.name,
- path: parentPathOfThisEntry ?? folderPathName,
- });
-
- // Revalidate SWR so the folder appears in the UI immediately
- mutate(`/api/teams/${teamInfo.currentTeam.id}/folders?root=true`);
- const parentPath = parentPathOfThisEntry ?? folderPathName;
- if (parentPath) {
- mutate(
- `/api/teams/${teamInfo.currentTeam.id}/folders/${parentPath}`,
- );
- mutate(
- `/api/teams/${teamInfo.currentTeam.id}/folders/documents/${parentPath}`,
- );
- }
-
- if (folderCounter) folderCounter.count++;
- if (folderPathCapture && folderPathCapture.value === undefined) {
- folderPathCapture.value = folderPath.startsWith("/")
- ? folderPath.slice(1)
- : folderPath;
- }
- analytics.capture("Folder Added", { folderName: entry.name });
+ const subs = await readAllDirectoryEntries(
+ (entry as FileSystemDirectoryEntry).createReader(),
+ );
+ await Promise.all(subs.map((sub) => recurse(sub, tempId)));
+ } else if (entry.isFile) {
+ files.push({
+ entry: entry as FileSystemFileEntry,
+ parentTempId,
+ topLevelTempId,
+ topLevelName: topEntry.name,
+ topLevelIsFolder: topEntry.isDirectory,
+ });
+ }
+ };
- const dirReader = (
- entry as FileSystemDirectoryEntry
- ).createReader();
- const subEntries =
- await readAllDirectoryEntries(dirReader);
+ await recurse(topEntry, null);
+ return { folders, files, topLevelTempId };
+ };
- const filteredSubEntries = subEntries.filter(
- (subEntry) => !isSystemFile(subEntry.name),
+ const topEntries = Array.from(event.dataTransfer.items, (item) => {
+ const entry =
+ (typeof item?.webkitGetAsEntry === "function" &&
+ item.webkitGetAsEntry()) ??
+ (typeof (item as any)?.getAsEntry === "function" &&
+ (item as any).getAsEntry()) ??
+ null;
+ return entry as FileSystemEntry | null;
+ }).filter((e): e is FileSystemEntry => !!e);
+
+ const walkResults = await Promise.all(topEntries.map(walkTopLevel));
+ const allFolders = walkResults.flatMap((r) => r.folders);
+ const allFiles = walkResults.flatMap((r) => r.files);
+
+ // ----- Bulk-create folders (one request per scope, in parallel).
+ let dataroomByTemp = new Map<string, BulkFolderResultItem>();
+ let mainDocsByTemp = new Map<string, BulkFolderResultItem>();
+
+ const rootPathForApi =
+ folderPathName && folderPathName.length > 0
+ ? "/" + folderPathName
+ : "/";
+
+ if (allFolders.length > 0) {
+ try {
+ if (dataroomId) {
+ // Replicated copies in main docs always live at the team root,
+ // regardless of where in the dataroom the user dropped the tree.
+ const tasks: Promise<void>[] = [
+ bulkCreateFoldersChunked({
+ url: `/api/teams/${teamId}/datarooms/${dataroomId}/folders/bulk`,
+ rootPath: rootPathForApi,
+ folders: allFolders,
+ }).then((rows) => {
+ dataroomByTemp = new Map(rows.map((r) => [r.tempId, r]));
+ }),
+ ];
+ if (replicateDataroomFolders) {
+ tasks.push(
+ bulkCreateFoldersChunked({
+ url: `/api/teams/${teamId}/folders/bulk`,
+ rootPath: "/",
+ folders: allFolders,
+ }).then((rows) => {
+ mainDocsByTemp = new Map(rows.map((r) => [r.tempId, r]));
+ }),
);
+ } else if (dataroomName) {
+ await getOrCreateDataroomFolder();
+ }
+ await Promise.all(tasks);
+ } else {
+ const rows = await bulkCreateFoldersChunked({
+ url: `/api/teams/${teamId}/folders/bulk`,
+ rootPath: rootPathForApi,
+ folders: allFolders,
+ });
+ mainDocsByTemp = new Map(rows.map((r) => [r.tempId, r]));
+ }
- const resolvedFolderPath = folderPath.startsWith("/")
- ? folderPath.slice(1)
- : folderPath;
-
- for (const subEntry of filteredSubEntries) {
- files.push(
- ...(await traverseFolder(
- subEntry,
- resolvedFolderPath,
- undefined,
- folderCounter,
- folderPathCapture,
- topLevelName,
- folderIdCapture,
- )),
- );
- }
- } else {
- const isFirstLevelFolder =
- (parentPathOfThisEntry ?? folderPathName) === folderPathName;
-
- const {
- parentDataroomPath: targetParentDataroomPath,
- parentMainDocsPath: targetParentMainDocsPath,
- } = determineFolderPaths({
- currentDataroomPath: dataroomParentPath ?? folderPathName,
- currentMainDocsPath: parentPathOfThisEntry,
- isFirstLevelFolder,
- });
-
- // If replication is disabled, ensure the dataroom folder exists in "All Documents"
- // Uses promise-lock pattern to prevent race conditions
- if (!replicateDataroomFolders && dataroomName) {
- await getOrCreateDataroomFolder();
- }
+ analytics.capture("Folder Added (bulk)", {
+ count: allFolders.length,
+ dataroomId: dataroomId,
+ replicated: dataroomId ? replicateDataroomFolders : undefined,
+ });
- const { dataroomPath, dataroomFolderId, mainDocsPath } =
- await createFolderInBoth({
- teamId: teamInfo.currentTeam.id,
- dataroomId,
- name: entry.name,
- parentMainDocsPath: targetParentMainDocsPath,
- parentDataroomPath: targetParentDataroomPath,
- setRejectedFiles,
- analytics,
- replicateDataroomFolders,
- });
-
- if (folderCounter) folderCounter.count++;
- if (folderPathCapture && folderPathCapture.value === undefined) {
- folderPathCapture.value = dataroomPath.startsWith("/")
- ? dataroomPath.slice(1)
- : dataroomPath;
- }
- if (folderIdCapture && folderIdCapture.value === undefined) {
- folderIdCapture.value = dataroomFolderId;
- }
+ // Broad one-shot revalidation of every cached folder/document key
+ // for this scope. Targeted mutates only cover root + the user's
+ // current view, which leaves deep paths the user navigated to
+ // *during* bulk-create stuck on whatever empty/404 response SWR
+ // fetched while the transaction was still in flight (SWR has
+ // revalidateOnFocus: false + dedupingInterval: 30s, so the stale
+ // cache otherwise persists). mutate(filterFn) only refetches keys
+ // already in cache β€” typically 2–10 keys, fired once at the end of
+ // bulkCreateFolders, not per file β€” so the "GET burst" cost is
+ // negligible and bounded.
+ const isDataroomFolderKey = (key: unknown) =>
+ typeof key === "string" &&
+ dataroomId !== undefined &&
+ (key.startsWith(
+ `/api/teams/${teamId}/datarooms/${dataroomId}/folders`,
+ ) ||
+ key.startsWith(
+ `/api/teams/${teamId}/datarooms/${dataroomId}/folder-documents`,
+ ) ||
+ key.startsWith(
+ `/api/teams/${teamId}/datarooms/${dataroomId}/documents`,
+ ));
+ const isMainDocsFolderKey = (key: unknown) =>
+ typeof key === "string" &&
+ (key.startsWith(`/api/teams/${teamId}/folders`) ||
+ key.startsWith(`/api/teams/${teamId}/folder-documents`) ||
+ key === `/api/teams/${teamId}/documents`);
+
+ if (dataroomId) {
+ mutate(isDataroomFolderKey);
+ if (replicateDataroomFolders) mutate(isMainDocsFolderKey);
+ } else {
+ mutate(isMainDocsFolderKey);
+ }
+ } catch (error) {
+ console.error("Bulk folder creation failed:", error);
+ setRejectedFiles((prev) => [
+ ...allFolders.map((f) => ({
+ fileName: f.name,
+ message: "Failed to create the folder",
+ })),
+ ...prev,
+ ]);
+ return [];
+ }
+ }
- const dirReader = (
- entry as FileSystemDirectoryEntry
- ).createReader();
- const subEntries =
- await readAllDirectoryEntries(dirReader);
+ // Per-top-level folder count for the upload-drawer progress bar.
+ const parentByTemp = new Map(
+ allFolders.map((f) => [f.tempId, f.parentTempId ?? null]),
+ );
+ const folderCountByTopLevelTempId = new Map<string, number>();
+ for (const f of allFolders) {
+ let cur: string | null = f.parentTempId ?? null;
+ let topLevel = f.tempId;
+ while (cur) {
+ topLevel = cur;
+ cur = parentByTemp.get(cur) ?? null;
+ }
+ folderCountByTopLevelTempId.set(
+ topLevel,
+ (folderCountByTopLevelTempId.get(topLevel) ?? 0) + 1,
+ );
+ }
- const filteredSubEntries = subEntries.filter(
- (subEntry) => !isSystemFile(subEntry.name),
- );
+ // ----- Resolve files (read content + annotate).
+ let dataroomFolderInMainDocsPath: string | undefined;
+ if (!replicateDataroomFolders && dataroomId && dataroomName) {
+ dataroomFolderInMainDocsPath = await getOrCreateDataroomFolder();
+ }
- // Use the resolved paths for all children
- // Guard against undefined mainDocsPath when replication is disabled
- const resolvedMainDocsPath = mainDocsPath
- ? mainDocsPath.startsWith("/")
- ? mainDocsPath.slice(1)
- : mainDocsPath
- : undefined;
- const resolvedDataroomPath = dataroomPath.startsWith("/")
- ? dataroomPath.slice(1)
- : dataroomPath;
-
- for (const subEntry of filteredSubEntries) {
- files.push(
- ...(await traverseFolder(
- subEntry,
- resolvedMainDocsPath,
- resolvedDataroomPath,
- folderCounter,
- folderPathCapture,
- topLevelName,
- folderIdCapture,
- )),
- );
- }
- }
- } catch (error) {
- console.error(
- "An error occurred while creating the folder: ",
- error,
+ const filesToBePassedToOnDrop: FileWithPaths[] = [];
+
+ for (let i = 0; i < allFiles.length; i++) {
+ if (filesToBePassedToOnDrop.length >= fileLimit) {
+ for (let j = i; j < allFiles.length; j++) {
+ const skipped = allFiles[j];
+ const list =
+ skippedPerTopLevel.get(skipped.topLevelName) ?? [];
+ list.push(
+ skipped.entry.fullPath.startsWith("/")
+ ? skipped.entry.fullPath.substring(1)
+ : skipped.entry.fullPath,
);
- setRejectedFiles((prev) => [
- {
- fileName: entry.name,
- message: "Failed to create the folder",
- },
- ...prev,
- ]);
- }
- } else if (entry.isFile) {
- if (isSystemFile(entry.name)) {
- return files;
- }
-
- if (collectedFileCount >= fileLimit) {
- if (topLevelName) {
- const list = skippedPerTopLevel.get(topLevelName) ?? [];
- list.push(entry.fullPath.startsWith("/") ? entry.fullPath.substring(1) : entry.fullPath);
- skippedPerTopLevel.set(topLevelName, list);
- }
- return files;
+ skippedPerTopLevel.set(skipped.topLevelName, list);
}
+ fileLimitTruncatedRef.current = true;
+ break;
+ }
- let file = await new Promise<FileWithPaths>((resolve) =>
- (entry as FileSystemFileEntry).file(resolve),
- );
+ const pending = allFiles[i];
+ let file = await new Promise<FileWithPaths>((resolve) =>
+ pending.entry.file(resolve),
+ );
- /** In some browsers e.g firefox is not able to detect the file type. (This only happens when user upload folder) */
- const browserFileTypeCompatibilityIssue = file.type === "";
-
- if (browserFileTypeCompatibilityIssue) {
- const fileExtension = file.name.split(".").pop()?.toLowerCase();
- let correctMimeType: string | undefined;
- if (fileExtension) {
- // Iterate through acceptableDropZoneFileTypes to find the MIME type for the extension
- for (const [mime, extsUntyped] of Object.entries(
- acceptableDropZoneFileTypes,
- )) {
- const exts = extsUntyped as string[]; // Explicitly type exts
- if (
- exts.some((ext) => ext.toLowerCase() === "." + fileExtension)
- ) {
- correctMimeType = mime;
- break;
- }
+ // Firefox can't always detect MIME type from drag-and-dropped folder
+ // contents; fall back to the extension table.
+ if (file.type === "") {
+ const ext = file.name.split(".").pop()?.toLowerCase();
+ let correctMimeType: string | undefined;
+ if (ext) {
+ for (const [mime, extsUntyped] of Object.entries(
+ acceptableDropZoneFileTypes,
+ )) {
+ const exts = extsUntyped as string[];
+ if (exts.some((e) => e.toLowerCase() === "." + ext)) {
+ correctMimeType = mime;
+ break;
}
}
-
- if (correctMimeType) {
- // if we can't do like ```file.type = fileType``` because of [Error: Setting getter-only property "type"]
- // The following is the only best way to resolve the problem
- file = new File([file], file.name, {
- type: correctMimeType,
- lastModified: file.lastModified,
- });
- }
}
-
- // Reason of removing "/" because webkitRelativePath doesn't start with "/"
- file.path = entry.fullPath.startsWith("/")
- ? entry.fullPath.substring(1)
- : entry.fullPath;
-
- // Determine where to upload in "All Documents"
- if (!replicateDataroomFolders && dataroomId && dataroomName) {
- // If replication is disabled, ensure the dataroom folder exists and use it
- // This await is safe because getOrCreateDataroomFolder uses a promise-lock
- const dataroomFolderPath = await getOrCreateDataroomFolder();
- file.whereToUploadPath = dataroomFolderPath;
- } else {
- // If replication is enabled or not in a dataroom, use the normal folder path
- file.whereToUploadPath = parentPathOfThisEntry ?? folderPathName;
+ if (correctMimeType) {
+ file = new File([file], file.name, {
+ type: correctMimeType,
+ lastModified: file.lastModified,
+ });
}
-
- file.dataroomUploadPath = dataroomParentPath;
-
- files.push(file);
- collectedFileCount++;
}
- return files;
- };
- /** *********** END OF `traverseFolder` *********** */
+ file.path = pending.entry.fullPath.startsWith("/")
+ ? pending.entry.fullPath.substring(1)
+ : pending.entry.fullPath;
- if ("dataTransfer" in event && event.dataTransfer) {
- const items = event.dataTransfer.items;
+ const mainDocsFolderPath = pending.parentTempId
+ ? stripLeadingSlash(mainDocsByTemp.get(pending.parentTempId)?.path)
+ : folderPathName;
+ const dataroomFolderPath = pending.parentTempId
+ ? stripLeadingSlash(dataroomByTemp.get(pending.parentTempId)?.path)
+ : folderPathName;
- const fileResults = await Promise.all(
- Array.from(items, (item) => {
- const entry =
- (typeof item?.webkitGetAsEntry === "function" &&
- item.webkitGetAsEntry()) ??
- (typeof (item as any)?.getAsEntry === "function" &&
- (item as any).getAsEntry()) ??
- null;
- if (!entry) return [];
- const counter = { count: 0 };
- const pathCapture: { value?: string } = {};
- const folderIdCapture: { value?: string } = {};
- return traverseFolder(
- entry,
- folderPathName,
- dataroomId ? folderPathName : undefined,
- counter,
- pathCapture,
- entry.name,
- folderIdCapture,
- ).then((files) => {
- for (const f of files) {
- f.topLevelItemName = entry.name;
- f.topLevelItemIsFolder = entry.isDirectory;
- f.topLevelItemFolderCount = counter.count;
- f.topLevelItemFolderPath = pathCapture.value;
- f.topLevelDataroomFolderId = folderIdCapture.value;
- }
- return files;
- });
- }),
- );
- fileResults.forEach((fileResult) =>
- filesToBePassedToOnDrop.push(...fileResult),
- );
- } else if (
- "target" in event &&
- event.target &&
- event.target instanceof HTMLInputElement &&
- event.target.files
- ) {
- for (let i = 0; i < event.target.files.length; i++) {
- const file: FileWithPaths = event.target.files[i];
- file.path = file.name;
- file.whereToUploadPath = folderPathName;
- file.dataroomUploadPath = folderPathName;
- file.topLevelItemName = file.name;
- file.topLevelItemIsFolder = false;
- filesToBePassedToOnDrop.push(event.target.files[i]);
+ if (!replicateDataroomFolders && dataroomId && dataroomName) {
+ file.whereToUploadPath = dataroomFolderInMainDocsPath;
+ } else {
+ file.whereToUploadPath = mainDocsFolderPath;
+ }
+ file.dataroomUploadPath = dataroomId ? dataroomFolderPath : undefined;
+
+ file.topLevelItemName = pending.topLevelName;
+ file.topLevelItemIsFolder = pending.topLevelIsFolder;
+ if (pending.topLevelTempId) {
+ file.topLevelItemFolderCount =
+ folderCountByTopLevelTempId.get(pending.topLevelTempId) ?? 0;
+ const topLevelDataroom = dataroomByTemp.get(pending.topLevelTempId);
+ const topLevelMainDocs = mainDocsByTemp.get(pending.topLevelTempId);
+ file.topLevelItemFolderPath = dataroomId
+ ? stripLeadingSlash(topLevelDataroom?.path)
+ : stripLeadingSlash(topLevelMainDocs?.path);
+ file.topLevelDataroomFolderId = topLevelDataroom?.id;
}
- }
- if (isFinite(fileLimit) && collectedFileCount >= fileLimit) {
- fileLimitTruncatedRef.current = true;
+ filesToBePassedToOnDrop.push(file);
}
if (skippedPerTopLevel.size > 0) {
@@ -1240,7 +1318,6 @@ export default function UploadZone({
},
[
folderPathName,
- endpointTargetType,
teamInfo,
dataroomId,
dataroomName,
diff --git a/lib/documents/move-dataroom-documents.ts b/lib/documents/move-dataroom-documents.ts
index 382746c2..6fe5f66b 100644
--- a/lib/documents/move-dataroom-documents.ts
+++ b/lib/documents/move-dataroom-documents.ts
@@ -22,7 +22,7 @@ export const moveDataroomDocumentToFolder = async ({
}
console.log("moving documents to folder", documentIds, folderId);
- const key = `/api/teams/${teamId}/datarooms/${dataroomId}${folderPathName ? `/folders/documents/${folderPathName.join("/")}` : "/documents"}`;
+ const key = `/api/teams/${teamId}/datarooms/${dataroomId}${folderPathName ? `/folder-documents/${folderPathName.join("/")}` : "/documents"}`;
// Optimistically update the UI by removing the documents from current folder
mutate(
key,
@@ -88,7 +88,7 @@ export const moveDataroomDocumentToFolder = async ({
mutate(`/api/teams/${teamId}/datarooms/${dataroomId}/folders?root=true`);
// update documents in new folder (`newPath` or home)
mutate(
- `/api/teams/${teamId}/datarooms/${dataroomId}${newPath ? `/folders/documents/${newPath}` : "/documents"}`,
+ `/api/teams/${teamId}/datarooms/${dataroomId}${newPath ? `/folder-documents/${newPath}` : "/documents"}`,
);
mutate(`/api/teams/${teamId}/datarooms/${dataroomId}/folders`);
toast.success(
diff --git a/lib/documents/move-dataroom-folders.ts b/lib/documents/move-dataroom-folders.ts
index 896d37dd..055d41e5 100644
--- a/lib/documents/move-dataroom-folders.ts
+++ b/lib/documents/move-dataroom-folders.ts
@@ -68,7 +68,7 @@ export const moveDataroomFolderToFolder = async ({
mutate(`/api/teams/${teamId}/datarooms/${dataroomId}/folders`);
// update documents in new folder (`newPath` or home)
mutate(
- `/api/teams/${teamId}/datarooms/${dataroomId}${newPath ? `/folders/documents/${newPath}` : "/documents"}`,
+ `/api/teams/${teamId}/datarooms/${dataroomId}${newPath ? `/folder-documents/${newPath}` : "/documents"}`,
);
toast.success(
`${updatedCount} folder${updatedCount > 1 ? "s" : ""} moved successfully`,
diff --git a/lib/documents/move-documents.ts b/lib/documents/move-documents.ts
index 443e5cfd..e6def485 100644
--- a/lib/documents/move-documents.ts
+++ b/lib/documents/move-documents.ts
@@ -19,7 +19,7 @@ export const moveDocumentToFolder = async ({
return;
}
- const key = `/api/teams/${teamId}${folderPathName ? `/folders/documents/${folderPathName.join("/")}` : "/documents"}`;
+ const key = `/api/teams/${teamId}${folderPathName ? `/folder-documents/${folderPathName.join("/")}` : "/documents"}`;
// Optimistically update the UI by removing the documents from current folder
mutate(
key,
@@ -85,7 +85,7 @@ export const moveDocumentToFolder = async ({
);
// update documents in new folder (or home)
mutate(
- `/api/teams/${teamId}${newPath ? `/folders/documents/${newPath}` : "/documents"}`,
+ `/api/teams/${teamId}${newPath ? `/folder-documents/${newPath}` : "/documents"}`,
);
toast.success(
`${updatedCount} document${updatedCount > 1 ? "s" : ""} moved successfully`,
diff --git a/lib/documents/move-folder.ts b/lib/documents/move-folder.ts
index 41aca55e..1546b68f 100644
--- a/lib/documents/move-folder.ts
+++ b/lib/documents/move-folder.ts
@@ -61,7 +61,7 @@ export const moveFolderToFolder = async ({
`/api/teams/${teamId}/folders${newPath ? `${newPath}` : "?root=true"}`,
);
mutate(
- `/api/teams/${teamId}${newPath ? `/folders/documents/${newPath}` : "/documents"}`,
+ `/api/teams/${teamId}${newPath ? `/folder-documents/${newPath}` : "/documents"}`,
);
toast.success(
`${updatedCount} folder${updatedCount > 1 ? "s" : ""} moved successfully`,
diff --git a/lib/folders/bulk-create.ts b/lib/folders/bulk-create.ts
new file mode 100644
index 00000000..3a6a77b8
--- /dev/null
+++ b/lib/folders/bulk-create.ts
@@ -0,0 +1,718 @@
+import { DefaultPermissionStrategy, ItemType, Prisma } from "@prisma/client";
+
+import { safeSlugify } from "@/lib/utils";
+
+/**
+ * Bulk creation of folder trees in a single transaction, used by the
+ * dataroom upload-zone to materialize a dropped directory hierarchy in one
+ * round-trip instead of N (one POST per folder).
+ *
+ * Two scoped variants are exported:
+ * - bulkCreateMainDocsFolders β†’ Folder model (all-documents)
+ * - bulkCreateDataroomFolders β†’ DataroomFolder model + default permissions
+ *
+ * Both share the same shape so the upload-zone can drive them in parallel.
+ */
+
+/** Maximum suffix " (N)" we'll try for slug-collision resolution. */
+const MAX_SLUG_SUFFIX = 50;
+
+/** Keep one bulk request bounded; callers can split larger trees if needed. */
+export const MAX_BULK_FOLDERS_PER_REQUEST = 500;
+
+/**
+ * How many times to re-resolve paths and re-attempt the level insert when a
+ * concurrent bulk request commits a colliding slug between our SELECT and our
+ * INSERT. 3 is enough for realistic concurrency (we already rate-limit to
+ * 10/min/user) without risking long retry storms.
+ */
+const MAX_INSERT_RETRIES = 3;
+
+/**
+ * Wraps `attempt` in a Postgres SAVEPOINT so that a failure inside the
+ * callback (notably P2002 from a concurrent path insert) doesn't abort the
+ * surrounding interactive transaction. On a unique-constraint error we roll
+ * back to the savepoint and let the caller re-resolve and retry.
+ *
+ * Without the savepoint, Prisma + Postgres would leave the transaction in an
+ * aborted state after the first P2002, so any retry would fail with
+ * "current transaction is aborted, commands ignored until end of transaction
+ * block".
+ */
+async function withUniqueConstraintRetry<T>(args: {
+ tx: Prisma.TransactionClient;
+ savepointName: string;
+ attempt: () => Promise<T>;
+}): Promise<T> {
+ const { tx, savepointName, attempt } = args;
+ let lastError: unknown;
+
+ for (let i = 0; i <= MAX_INSERT_RETRIES; i++) {
+ await tx.$executeRawUnsafe(`SAVEPOINT "${savepointName}"`);
+ try {
+ const result = await attempt();
+ await tx.$executeRawUnsafe(`RELEASE SAVEPOINT "${savepointName}"`);
+ return result;
+ } catch (error) {
+ await tx.$executeRawUnsafe(
+ `ROLLBACK TO SAVEPOINT "${savepointName}"`,
+ );
+ await tx.$executeRawUnsafe(`RELEASE SAVEPOINT "${savepointName}"`);
+
+ if (
+ i < MAX_INSERT_RETRIES &&
+ error instanceof Prisma.PrismaClientKnownRequestError &&
+ error.code === "P2002"
+ ) {
+ lastError = error;
+ continue;
+ }
+ throw error;
+ }
+ }
+
+ throw lastError;
+}
+
+export interface BulkFolderInput {
+ /** Client-generated id, unique within this request. */
+ tempId: string;
+ /** Folder name shown to the user. */
+ name: string;
+ /**
+ * Parent reference within this batch. null/undefined means the folder is
+ * a direct child of either `parentPath` (if set) or the request-level
+ * `rootPath`.
+ */
+ parentTempId?: string | null;
+ /**
+ * Absolute path of an existing folder in the DB (e.g. one created by a
+ * prior bulk chunk). Lets the client split very large trees across
+ * multiple requests: chunk 2 can reference parents from chunk 1 by path.
+ * Ignored if `parentTempId` is set.
+ */
+ parentPath?: string | null;
+}
+
+export interface BulkFolderResult {
+ tempId: string;
+ id: string;
+ name: string;
+ path: string;
+ parentId: string | null;
+}
+
+class BulkValidationError extends Error {
+ constructor(public readonly code: string, message: string) {
+ super(message);
+ this.name = "BulkValidationError";
+ }
+}
+
+export function getSafeBulkValidationMessage(code: string): string {
+ switch (code) {
+ case "UNKNOWN_ROOT_PATH":
+ case "UNKNOWN_PARENT_PATH":
+ return "Parent folder does not exist or is unavailable";
+ case "MISSING_TEMP_ID":
+ case "DUPLICATE_TEMP_ID":
+ case "UNKNOWN_PARENT":
+ case "CYCLE":
+ return "Invalid folder hierarchy";
+ case "EMPTY_NAME":
+ return "Folder names cannot be empty";
+ case "SLUG_EXHAUSTED":
+ return "Too many folders with similar names";
+ case "TOO_MANY_FOLDERS":
+ return `Too many folders in a single request (max ${MAX_BULK_FOLDERS_PER_REQUEST})`;
+ default:
+ return "Invalid folder request";
+ }
+}
+
+/** Validates the input is a topologically-sortable forest of folders. */
+function buildLevels(folders: BulkFolderInput[]): BulkFolderInput[][] {
+ if (folders.length === 0) return [];
+
+ // Defense-in-depth: API routes already cap via Zod, but the bulk creators
+ // are exported and may be called from other code paths (background jobs,
+ // future endpoints) that bypass the schema. Reject oversized batches here
+ // before doing any work.
+ if (folders.length > MAX_BULK_FOLDERS_PER_REQUEST) {
+ throw new BulkValidationError(
+ "TOO_MANY_FOLDERS",
+ `Too many folders in a single request (got ${folders.length}, max ${MAX_BULK_FOLDERS_PER_REQUEST})`,
+ );
+ }
+
+ const byId = new Map<string, BulkFolderInput>();
+ for (const f of folders) {
+ if (!f.tempId) {
+ throw new BulkValidationError("MISSING_TEMP_ID", "Each folder needs a tempId");
+ }
+ if (!f.name || f.name.trim() === "") {
+ throw new BulkValidationError("EMPTY_NAME", `Folder ${f.tempId} has an empty name`);
+ }
+ if (byId.has(f.tempId)) {
+ throw new BulkValidationError("DUPLICATE_TEMP_ID", `Duplicate tempId: ${f.tempId}`);
+ }
+ byId.set(f.tempId, f);
+ }
+
+ const depth = new Map<string, number>();
+ const computeDepth = (f: BulkFolderInput, seen: Set<string>): number => {
+ const cached = depth.get(f.tempId);
+ if (cached !== undefined) return cached;
+ if (seen.has(f.tempId)) {
+ throw new BulkValidationError("CYCLE", `Cycle detected via ${f.tempId}`);
+ }
+ seen.add(f.tempId);
+ if (!f.parentTempId) {
+ depth.set(f.tempId, 0);
+ return 0;
+ }
+ const parent = byId.get(f.parentTempId);
+ if (!parent) {
+ throw new BulkValidationError(
+ "UNKNOWN_PARENT",
+ `Folder ${f.tempId} references unknown parent ${f.parentTempId}`,
+ );
+ }
+ const d = computeDepth(parent, seen) + 1;
+ depth.set(f.tempId, d);
+ seen.delete(f.tempId);
+ return d;
+ };
+
+ for (const f of folders) computeDepth(f, new Set());
+
+ const levels: BulkFolderInput[][] = [];
+ for (const f of folders) {
+ const d = depth.get(f.tempId)!;
+ (levels[d] ??= []).push(f);
+ }
+ return levels;
+}
+
+/**
+ * Resolves the parent path of a folder for slug-collision computation.
+ * Order of precedence: parentTempId (parent created in this batch) β†’
+ * parentPath (parent created in a prior chunk / existing in DB) β†’ rootPath.
+ */
+function parentPathOf(
+ f: BulkFolderInput,
+ pathByTemp: Map<string, string>,
+ rootPath: string,
+): string {
+ if (f.parentTempId) return pathByTemp.get(f.parentTempId)!;
+ if (f.parentPath) return f.parentPath;
+ return rootPath;
+}
+
+/**
+ * Resolves the DB id of a folder's parent. Same precedence as parentPathOf.
+ * Throws UNKNOWN_PARENT_PATH if the input references an external path that
+ * doesn't exist in the scope-pre-loaded map.
+ */
+function resolveParentId(
+ f: BulkFolderInput,
+ idByTemp: Map<string, string>,
+ externalParentPathToId: Map<string, string>,
+ rootParentId: string | null,
+): string | null {
+ if (f.parentTempId) return idByTemp.get(f.parentTempId)!;
+ if (f.parentPath) {
+ const id = externalParentPathToId.get(f.parentPath);
+ if (!id) {
+ throw new BulkValidationError(
+ "UNKNOWN_PARENT_PATH",
+ `Parent path ${f.parentPath} does not exist`,
+ );
+ }
+ return id;
+ }
+ return rootParentId;
+}
+
+/**
+ * One-shot lookup of every external `parentPath` referenced by the input.
+ * Cheaper than per-folder lookups and avoids racing with concurrent inserts.
+ */
+async function resolveExternalParentPaths(
+ folders: BulkFolderInput[],
+ findRows: (
+ paths: string[],
+ ) => Promise<{ id: string; path: string }[]>,
+): Promise<Map<string, string>> {
+ const paths = new Set<string>();
+ for (const f of folders) {
+ if (!f.parentTempId && f.parentPath) paths.add(f.parentPath);
+ }
+ if (paths.size === 0) return new Map();
+ const rows = await findRows(Array.from(paths));
+ const map = new Map<string, string>();
+ for (const r of rows) map.set(r.path, r.id);
+ for (const p of paths) {
+ if (!map.has(p)) {
+ throw new BulkValidationError(
+ "UNKNOWN_PARENT_PATH",
+ `Parent path ${p} does not exist`,
+ );
+ }
+ }
+ return map;
+}
+
+/**
+ * Resolves slug collisions for an entire level in a single SELECT instead of
+ * up to MAX_SLUG_SUFFIX sequential findUniques per folder.
+ *
+ * Returns the resolved final path per tempId.
+ */
+async function resolveLevelPaths(args: {
+ level: BulkFolderInput[];
+ /** Returns the absolute path of the parent for a given input folder. */
+ parentPathOf: (f: BulkFolderInput) => string;
+ /** SELECT all existing folder paths that match any candidate. */
+ findExisting: (candidatePaths: string[]) => Promise<{ path: string }[]>;
+}): Promise<Map<string, { name: string; path: string }>> {
+ const { level, parentPathOf, findExisting } = args;
+ if (level.length === 0) return new Map();
+
+ const candidatesPerFolder: { tempId: string; name: string; basePath: string; candidates: string[] }[] = [];
+ const allCandidates = new Set<string>();
+
+ for (const f of level) {
+ const parentPath = parentPathOf(f);
+ const basePath = parentPath === "/" ? "/" : parentPath + "/";
+ const slug = safeSlugify(f.name);
+ const candidates: string[] = [basePath + slug];
+ for (let i = 1; i <= MAX_SLUG_SUFFIX; i++) {
+ candidates.push(basePath + safeSlugify(`${f.name} (${i})`));
+ }
+ candidatesPerFolder.push({ tempId: f.tempId, name: f.name, basePath, candidates });
+ for (const c of candidates) allCandidates.add(c);
+ }
+
+ const existingRows = await findExisting(Array.from(allCandidates));
+ const taken = new Set(existingRows.map((r) => r.path));
+
+ const resolved = new Map<string, { name: string; path: string }>();
+ for (const entry of candidatesPerFolder) {
+ let pickedPath: string | undefined;
+ let pickedName: string | undefined;
+ for (let i = 0; i < entry.candidates.length; i++) {
+ const candidate = entry.candidates[i];
+ if (!taken.has(candidate)) {
+ pickedPath = candidate;
+ pickedName = i === 0 ? entry.name : `${entry.name} (${i})`;
+ break;
+ }
+ }
+ if (!pickedPath || !pickedName) {
+ throw new BulkValidationError(
+ "SLUG_EXHAUSTED",
+ `Could not find a free slug for "${entry.name}" under ${entry.basePath}`,
+ );
+ }
+ // Reserve the path so subsequent folders at the same level don't collide
+ // with it (siblings dropped with identical names within the same batch).
+ taken.add(pickedPath);
+ resolved.set(entry.tempId, { name: pickedName, path: pickedPath });
+ }
+
+ return resolved;
+}
+
+// ---------------------------------------------------------------------------
+// All-Documents (Folder) bulk creator
+// ---------------------------------------------------------------------------
+
+export async function bulkCreateMainDocsFolders(args: {
+ tx: Prisma.TransactionClient;
+ teamId: string;
+ /** Absolute path (with leading "/") of the parent folder. */
+ rootPath: string;
+ /** Database id of the parent folder, or null for team-root. */
+ rootParentId: string | null;
+ folders: BulkFolderInput[];
+}): Promise<BulkFolderResult[]> {
+ const { tx, teamId, rootPath, rootParentId, folders } = args;
+ const levels = buildLevels(folders);
+ if (levels.length === 0) return [];
+
+ const externalParentPathToId = await resolveExternalParentPaths(
+ folders,
+ (paths) =>
+ tx.folder.findMany({
+ where: { teamId, path: { in: paths } },
+ select: { id: true, path: true },
+ }),
+ );
+
+ const idByTemp = new Map<string, string>();
+ const pathByTemp = new Map<string, string>();
+ const nameByTemp = new Map<string, string>();
+ const parentIdByTemp = new Map<string, string | null>();
+
+ for (let depth = 0; depth < levels.length; depth++) {
+ const level = levels[depth];
+
+ // Re-resolve and re-insert on P2002 so a concurrent bulk request that
+ // commits a colliding path between our SELECT and INSERT doesn't 500.
+ const { resolved, inserted } = await withUniqueConstraintRetry({
+ tx,
+ savepointName: `bulk_main_folder_lvl_${depth}`,
+ attempt: async () => {
+ const resolved = await resolveLevelPaths({
+ level,
+ parentPathOf: (f) => parentPathOf(f, pathByTemp, rootPath),
+ findExisting: (candidatePaths) =>
+ tx.folder.findMany({
+ where: { teamId, path: { in: candidatePaths } },
+ select: { path: true },
+ }),
+ });
+
+ const insertRows = level.map((f) => {
+ const r = resolved.get(f.tempId)!;
+ const parentId = resolveParentId(
+ f,
+ idByTemp,
+ externalParentPathToId,
+ rootParentId,
+ );
+ return {
+ name: r.name,
+ path: r.path,
+ parentId,
+ teamId,
+ };
+ });
+
+ const inserted = await tx.folder.createManyAndReturn({
+ data: insertRows,
+ select: { id: true, name: true, path: true, parentId: true },
+ });
+
+ return { resolved, inserted };
+ },
+ });
+
+ // Match returned rows by path (unique within a team). Do not rely on
+ // createManyAndReturn input/output ordering β€” it's not guaranteed across
+ // all Prisma + Postgres combinations and a single reordering would
+ // cascade into wrong parentId mappings on every subsequent level.
+ const insertedByPath = new Map(inserted.map((r) => [r.path, r]));
+ for (const f of level) {
+ const r = resolved.get(f.tempId)!;
+ const row = insertedByPath.get(r.path);
+ if (!row) {
+ throw new BulkValidationError(
+ "INSERT_MISSING",
+ `Insert returned no row for path ${r.path}`,
+ );
+ }
+ idByTemp.set(f.tempId, row.id);
+ pathByTemp.set(f.tempId, row.path);
+ nameByTemp.set(f.tempId, row.name);
+ parentIdByTemp.set(f.tempId, row.parentId);
+ }
+ }
+
+ return folders.map((f) => ({
+ tempId: f.tempId,
+ id: idByTemp.get(f.tempId)!,
+ name: nameByTemp.get(f.tempId)!,
+ path: pathByTemp.get(f.tempId)!,
+ parentId: parentIdByTemp.get(f.tempId) ?? null,
+ }));
+}
+
+// ---------------------------------------------------------------------------
+// Dataroom (DataroomFolder) bulk creator
+// ---------------------------------------------------------------------------
+
+export async function bulkCreateDataroomFolders(args: {
+ tx: Prisma.TransactionClient;
+ dataroomId: string;
+ rootPath: string;
+ rootParentId: string | null;
+ folders: BulkFolderInput[];
+}): Promise<BulkFolderResult[]> {
+ const { tx, dataroomId, rootPath, rootParentId, folders } = args;
+ const levels = buildLevels(folders);
+ if (levels.length === 0) return [];
+
+ const externalParentPathToId = await resolveExternalParentPaths(
+ folders,
+ (paths) =>
+ tx.dataroomFolder.findMany({
+ where: { dataroomId, path: { in: paths } },
+ select: { id: true, path: true },
+ }),
+ );
+
+ const idByTemp = new Map<string, string>();
+ const pathByTemp = new Map<string, string>();
+ const nameByTemp = new Map<string, string>();
+ const parentIdByTemp = new Map<string, string | null>();
+ const allCreatedIds: string[] = [];
+
+ for (let depth = 0; depth < levels.length; depth++) {
+ const level = levels[depth];
+
+ // Re-resolve and re-insert on P2002 so a concurrent bulk request that
+ // commits a colliding path between our SELECT and INSERT doesn't 500.
+ const { resolved, inserted } = await withUniqueConstraintRetry({
+ tx,
+ savepointName: `bulk_dataroom_folder_lvl_${depth}`,
+ attempt: async () => {
+ const resolved = await resolveLevelPaths({
+ level,
+ parentPathOf: (f) => parentPathOf(f, pathByTemp, rootPath),
+ findExisting: (candidatePaths) =>
+ tx.dataroomFolder.findMany({
+ where: { dataroomId, path: { in: candidatePaths } },
+ select: { path: true },
+ }),
+ });
+
+ const insertRows = level.map((f) => {
+ const r = resolved.get(f.tempId)!;
+ const parentId = resolveParentId(
+ f,
+ idByTemp,
+ externalParentPathToId,
+ rootParentId,
+ );
+ // orderIndex and hierarchicalIndex are intentionally left null.
+ // Matches the existing single-folder POST behavior; null sorts last
+ // so new folders append to the end of each parent. They can be
+ // re-indexed via /api/teams/.../datarooms/[id]/calculate-indexes
+ // when needed.
+ return {
+ name: r.name,
+ path: r.path,
+ parentId,
+ dataroomId,
+ };
+ });
+
+ const inserted = await tx.dataroomFolder.createManyAndReturn({
+ data: insertRows,
+ select: { id: true, name: true, path: true, parentId: true },
+ });
+
+ return { resolved, inserted };
+ },
+ });
+
+ // Match returned rows by path (unique within a dataroom). Do not rely on
+ // createManyAndReturn ordering β€” see note in bulkCreateMainDocsFolders.
+ const insertedByPath = new Map(inserted.map((r) => [r.path, r]));
+ for (const f of level) {
+ const r = resolved.get(f.tempId)!;
+ const row = insertedByPath.get(r.path);
+ if (!row) {
+ throw new BulkValidationError(
+ "INSERT_MISSING",
+ `Insert returned no row for path ${r.path}`,
+ );
+ }
+ idByTemp.set(f.tempId, row.id);
+ pathByTemp.set(f.tempId, row.path);
+ nameByTemp.set(f.tempId, row.name);
+ parentIdByTemp.set(f.tempId, row.parentId);
+ allCreatedIds.push(row.id);
+ }
+ }
+
+ await applyDefaultPermissionsBulk({
+ tx,
+ dataroomId,
+ rootParentId,
+ newFolderIds: allCreatedIds,
+ });
+
+ return folders.map((f) => ({
+ tempId: f.tempId,
+ id: idByTemp.get(f.tempId)!,
+ name: nameByTemp.get(f.tempId)!,
+ path: pathByTemp.get(f.tempId)!,
+ parentId: parentIdByTemp.get(f.tempId) ?? null,
+ }));
+}
+
+/**
+ * Bulk-applies default ACLs to all newly created folders.
+ *
+ * Every folder in a single drop share the same "first existing ancestor"
+ * (the rootParentId, or the dataroom root). So instead of N permission-lookup
+ * queries we do:
+ * - 1 read for dataroom + viewer groups + permission groups
+ * - 1 read each for parent's existing viewer/permission ACLs (only if
+ * inherit-from-parent and rootParentId is non-null)
+ * - 1 createMany per ACL table
+ */
+async function applyDefaultPermissionsBulk(args: {
+ tx: Prisma.TransactionClient;
+ dataroomId: string;
+ rootParentId: string | null;
+ newFolderIds: string[];
+}): Promise<void> {
+ const { tx, dataroomId, rootParentId, newFolderIds } = args;
+ if (newFolderIds.length === 0) return;
+
+ const [dataroom, viewerGroups, permissionGroups] = await Promise.all([
+ tx.dataroom.findUnique({
+ where: { id: dataroomId },
+ select: {
+ defaultPermissionStrategy: true,
+ defaultGroupPermissionStrategy: true,
+ },
+ }),
+ tx.viewerGroup.findMany({
+ where: { dataroomId },
+ select: { id: true },
+ }),
+ tx.permissionGroup.findMany({
+ where: { dataroomId },
+ select: { id: true },
+ }),
+ ]);
+ if (!dataroom) return;
+
+ const groupInherits =
+ dataroom.defaultGroupPermissionStrategy ===
+ DefaultPermissionStrategy.INHERIT_FROM_PARENT;
+ const linkInherits =
+ dataroom.defaultPermissionStrategy ===
+ DefaultPermissionStrategy.INHERIT_FROM_PARENT;
+
+ const [parentViewerPerms, parentLinkPerms] = await Promise.all([
+ groupInherits && rootParentId && viewerGroups.length > 0
+ ? tx.viewerGroupAccessControls.findMany({
+ where: {
+ itemId: rootParentId,
+ itemType: ItemType.DATAROOM_FOLDER,
+ },
+ select: { groupId: true, canView: true, canDownload: true },
+ })
+ : Promise.resolve(
+ [] as { groupId: string; canView: boolean; canDownload: boolean }[],
+ ),
+ linkInherits && rootParentId && permissionGroups.length > 0
+ ? tx.permissionGroupAccessControls.findMany({
+ where: {
+ itemId: rootParentId,
+ itemType: ItemType.DATAROOM_FOLDER,
+ },
+ select: {
+ groupId: true,
+ canView: true,
+ canDownload: true,
+ canDownloadOriginal: true,
+ },
+ })
+ : Promise.resolve(
+ [] as {
+ groupId: string;
+ canView: boolean;
+ canDownload: boolean;
+ canDownloadOriginal: boolean;
+ }[],
+ ),
+ ]);
+
+ // Viewer-group ACLs.
+ if (groupInherits && viewerGroups.length > 0) {
+ const sourcePerms = rootParentId
+ ? parentViewerPerms
+ : viewerGroups.map((g) => ({
+ groupId: g.id,
+ canView: true,
+ canDownload: false,
+ }));
+
+ if (sourcePerms.length > 0) {
+ const data = newFolderIds.flatMap((folderId) =>
+ sourcePerms.map((p) => ({
+ groupId: p.groupId,
+ itemId: folderId,
+ itemType: ItemType.DATAROOM_FOLDER,
+ canView: p.canView,
+ canDownload: p.canDownload,
+ })),
+ );
+ if (data.length > 0) {
+ await tx.viewerGroupAccessControls.createMany({
+ data,
+ skipDuplicates: true,
+ });
+ }
+ }
+ }
+
+ // Permission-group (link) ACLs.
+ if (linkInherits && permissionGroups.length > 0) {
+ const sourcePerms = rootParentId
+ ? parentLinkPerms
+ : permissionGroups.map((g) => ({
+ groupId: g.id,
+ canView: true,
+ canDownload: false,
+ canDownloadOriginal: false,
+ }));
+
+ if (sourcePerms.length > 0) {
+ const data = newFolderIds.flatMap((folderId) =>
+ sourcePerms.map((p) => ({
+ groupId: p.groupId,
+ itemId: folderId,
+ itemType: ItemType.DATAROOM_FOLDER,
+ canView: p.canView,
+ canDownload: p.canDownload,
+ canDownloadOriginal: p.canDownloadOriginal,
+ })),
+ );
+ if (data.length > 0) {
+ await tx.permissionGroupAccessControls.createMany({
+ data,
+ skipDuplicates: true,
+ });
+ }
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Shared helpers re-exported for the single-folder endpoints
+// ---------------------------------------------------------------------------
+
+/**
+ * Resolves a free folder slug under `parentPath` in a single DB call.
+ *
+ * Used by both the bulk and the single-folder endpoints to replace the
+ * up-to-50 sequential `findUnique` collision loop.
+ */
+export async function resolveFreeFolderPath(args: {
+ name: string;
+ parentPath: string;
+ findExisting: (candidatePaths: string[]) => Promise<{ path: string }[]>;
+}): Promise<{ name: string; path: string }> {
+ const { name, parentPath, findExisting } = args;
+ // Mirror the EMPTY_NAME guard enforced by buildLevels so single-folder
+ // callers can't slip a whitespace-only name past the slug resolver.
+ if (!name || name.trim() === "") {
+ throw new BulkValidationError("EMPTY_NAME", "Folder name cannot be empty");
+ }
+ const result = await resolveLevelPaths({
+ level: [{ tempId: "_single", name, parentTempId: null }],
+ parentPathOf: () => parentPath,
+ findExisting,
+ });
+ return result.get("_single")!;
+}
+
+export { BulkValidationError };
diff --git a/lib/folders/create-folder.ts b/lib/folders/create-folder.ts
index e7287d32..0f3f9aa1 100644
--- a/lib/folders/create-folder.ts
+++ b/lib/folders/create-folder.ts
@@ -1,5 +1,3 @@
-import { mutate } from "swr";
-
import { SYSTEM_FILES } from "../constants";
export function isSystemFile(name: string): boolean {
@@ -16,37 +14,197 @@ interface CreateFolderResponse {
name: string;
}
-export async function createFolderInDataroom({
- teamId,
- dataroomId,
- name,
- path,
-}: {
- teamId: string;
- dataroomId: string;
+export interface BulkFolderRequestItem {
+ tempId: string;
name: string;
- path?: string;
-}): Promise<CreateFolderResponse> {
- const response = await fetch(
- `/api/teams/${teamId}/datarooms/${dataroomId}/folders`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- name,
- path,
- }),
+ parentTempId?: string | null;
+ /**
+ * Server-resolved path of an existing parent β€” only used by the chunked
+ * helper when a folder's parent was created in a prior chunk. The server
+ * looks this path up in DB to resolve the parent's id.
+ */
+ parentPath?: string | null;
+}
+
+export interface BulkFolderResultItem {
+ tempId: string;
+ id: string;
+ name: string;
+ path: string;
+ parentId: string | null;
+}
+
+/**
+ * Thrown by `bulkCreateFoldersChunked` when a chunk fails after one or more
+ * earlier chunks have already committed. Each chunk is its own server-side
+ * transaction, so the rows in `created` are guaranteed to exist in the DB β€”
+ * callers that want partial-success semantics should consume them instead of
+ * treating every input folder as failed.
+ */
+export class BulkCreateFoldersChunkedError extends Error {
+ readonly created: BulkFolderResultItem[];
+ readonly failedChunkIndex: number;
+
+ constructor(
+ message: string,
+ opts: {
+ created: BulkFolderResultItem[];
+ failedChunkIndex: number;
+ cause?: unknown;
},
- );
+ ) {
+ super(message, opts.cause !== undefined ? { cause: opts.cause } : undefined);
+ this.name = "BulkCreateFoldersChunkedError";
+ this.created = opts.created;
+ this.failedChunkIndex = opts.failedChunkIndex;
+ }
+}
+/**
+ * Server cap on a single bulk request. Mirrors MAX_BULK_FOLDERS_PER_REQUEST
+ * in lib/folders/bulk-create.ts (not imported because that module pulls in
+ * Prisma and isn't safe to use in client bundles).
+ */
+const BULK_CHUNK_SIZE = 500;
+
+/**
+ * POSTs the entire folder tree to the bulk endpoint and returns the created
+ * rows keyed by tempId. Empty input is a no-op. Caller must ensure the input
+ * size is <= BULK_CHUNK_SIZE β€” use bulkCreateFoldersChunked for arbitrary
+ * sizes.
+ */
+export async function bulkCreateFolders(args: {
+ url: string;
+ rootPath?: string;
+ folders: BulkFolderRequestItem[];
+}): Promise<BulkFolderResultItem[]> {
+ if (args.folders.length === 0) return [];
+ const response = await fetch(args.url, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ rootPath: args.rootPath, folders: args.folders }),
+ });
if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message || "Failed to create folder in dataroom");
+ const body = await response.json().catch(() => ({}) as { message?: string });
+ throw new Error(body.message || `Bulk folder create failed (${response.status})`);
}
+ const data = (await response.json()) as { folders: BulkFolderResultItem[] };
+ return data.folders;
+}
- return response.json();
+/**
+ * Bulk-create that transparently splits arbitrarily large folder trees into
+ * server-sized chunks (BULK_CHUNK_SIZE). Each chunk is one POST; chunks run
+ * sequentially because chunk N+1 may reference parents resolved in chunk N
+ * via `parentPath`.
+ *
+ * Strategy:
+ * 1. Topologically sort by parentTempId depth (shallowest first), so any
+ * cross-chunk reference is always backwards.
+ * 2. Pack greedily into chunks of <= BULK_CHUNK_SIZE.
+ * 3. Before sending each chunk: for any folder whose parentTempId points
+ * to a prior chunk, swap parentTempId out for the parent's resolved
+ * `parentPath` (the server resolves it via DB lookup in one query).
+ *
+ * Failure semantics: each chunk is its own DB transaction. If chunk K
+ * fails, chunks 1..K-1 have already committed. Callers should treat this as
+ * partial success and report which folders never made it.
+ */
+export async function bulkCreateFoldersChunked(args: {
+ url: string;
+ rootPath?: string;
+ folders: BulkFolderRequestItem[];
+}): Promise<BulkFolderResultItem[]> {
+ if (args.folders.length === 0) return [];
+ if (args.folders.length <= BULK_CHUNK_SIZE) {
+ return bulkCreateFolders(args);
+ }
+
+ const byTemp = new Map(args.folders.map((f) => [f.tempId, f]));
+ const depthCache = new Map<string, number>();
+ const visiting = new Set<string>();
+ const depthOf = (f: BulkFolderRequestItem): number => {
+ const cached = depthCache.get(f.tempId);
+ if (cached !== undefined) return cached;
+ if (visiting.has(f.tempId)) {
+ throw new Error(
+ `Cycle detected in folder parentTempId graph at ${f.tempId}`,
+ );
+ }
+ if (!f.parentTempId) {
+ depthCache.set(f.tempId, 0);
+ return 0;
+ }
+ const parent = byTemp.get(f.parentTempId);
+ if (!parent) {
+ // External parent (parentPath or rootPath) β€” same depth as a root.
+ depthCache.set(f.tempId, 0);
+ return 0;
+ }
+ visiting.add(f.tempId);
+ try {
+ const d = depthOf(parent) + 1;
+ depthCache.set(f.tempId, d);
+ return d;
+ } finally {
+ visiting.delete(f.tempId);
+ }
+ };
+ const sorted = [...args.folders].sort(
+ (a, b) => depthOf(a) - depthOf(b),
+ );
+
+ const resolvedByTemp = new Map<string, BulkFolderResultItem>();
+ const allResults: BulkFolderResultItem[] = [];
+
+ for (let i = 0; i < sorted.length; i += BULK_CHUNK_SIZE) {
+ const chunkIndex = i / BULK_CHUNK_SIZE;
+ const chunk = sorted.slice(i, i + BULK_CHUNK_SIZE);
+ const chunkTempIds = new Set(chunk.map((f) => f.tempId));
+ const rewritten = chunk.map((f): BulkFolderRequestItem => {
+ if (f.parentTempId && !chunkTempIds.has(f.parentTempId)) {
+ const parent = resolvedByTemp.get(f.parentTempId);
+ if (!parent) {
+ throw new BulkCreateFoldersChunkedError(
+ `Bulk chunk references unresolved parent ${f.parentTempId}`,
+ { created: allResults, failedChunkIndex: chunkIndex },
+ );
+ }
+ return {
+ tempId: f.tempId,
+ name: f.name,
+ parentTempId: null,
+ parentPath: parent.path,
+ };
+ }
+ return f;
+ });
+
+ let chunkResult: BulkFolderResultItem[];
+ try {
+ chunkResult = await bulkCreateFolders({
+ url: args.url,
+ rootPath: args.rootPath,
+ folders: rewritten,
+ });
+ } catch (err) {
+ const message =
+ err instanceof Error
+ ? err.message
+ : `Bulk folder chunk ${chunkIndex} failed`;
+ throw new BulkCreateFoldersChunkedError(message, {
+ created: allResults,
+ failedChunkIndex: chunkIndex,
+ cause: err,
+ });
+ }
+ for (const row of chunkResult) {
+ resolvedByTemp.set(row.tempId, row);
+ allResults.push(row);
+ }
+ }
+
+ return allResults;
}
export async function createFolderInMainDocs({
@@ -78,113 +236,3 @@ export async function createFolderInMainDocs({
return response.json();
}
-
-export function determineFolderPaths({
- currentDataroomPath,
- currentMainDocsPath,
- isFirstLevelFolder,
-}: {
- currentDataroomPath?: string;
- currentMainDocsPath?: string;
- isFirstLevelFolder: boolean;
-}): {
- parentDataroomPath?: string;
- parentMainDocsPath?: string;
-} {
- return {
- parentDataroomPath: currentDataroomPath,
- parentMainDocsPath: isFirstLevelFolder ? undefined : currentMainDocsPath,
- };
-}
-
-export async function createFolderInBoth({
- teamId,
- dataroomId,
- name,
- parentMainDocsPath,
- parentDataroomPath,
- setRejectedFiles,
- analytics,
- replicateDataroomFolders = true,
-}: {
- teamId: string;
- dataroomId: string;
- name: string;
- parentMainDocsPath?: string;
- parentDataroomPath?: string;
- setRejectedFiles: (files: { fileName: string; message: string }[]) => void;
- analytics: any;
- replicateDataroomFolders?: boolean;
-}): Promise<{
- dataroomPath: string;
- dataroomFolderId: string;
- mainDocsPath: string | undefined;
-}> {
- try {
- // Always create folder in dataroom
- const dataroomResponse = await createFolderInDataroom({
- teamId,
- dataroomId,
- name,
- path: parentDataroomPath,
- });
-
- // Conditionally create folder in main docs based on user preference
- let mainDocsResponse: CreateFolderResponse | undefined;
- if (replicateDataroomFolders) {
- mainDocsResponse = await createFolderInMainDocs({
- teamId,
- name,
- path: parentMainDocsPath,
- });
- } else {
- // If not replicating, return undefined to explicitly signal no replication
- mainDocsResponse = undefined;
- }
-
- // Track analytics
- analytics.capture(
- replicateDataroomFolders
- ? "Folder Added in dataroom and in main documents"
- : "Folder Added in dataroom only",
- {
- folderName: name,
- dataroomTargetParent: parentDataroomPath,
- mainDocsTargetParent: parentMainDocsPath,
- replicateDataroomFolders,
- },
- );
-
- // Mutate dataroom folders
- mutate(`/api/teams/${teamId}/datarooms/${dataroomId}/folders?root=true`);
- mutate(`/api/teams/${teamId}/datarooms/${dataroomId}/folders`);
- mutate(
- `/api/teams/${teamId}/datarooms/${dataroomId}/folders/${dataroomResponse.path}`,
- );
-
- // Only mutate main docs folders if we created them
- if (replicateDataroomFolders && mainDocsResponse) {
- mutate(`/api/teams/${teamId}/folders?root=true`);
- mutate(`/api/teams/${teamId}/documents`);
- }
-
- return {
- dataroomPath: dataroomResponse.path,
- dataroomFolderId: dataroomResponse.id,
- mainDocsPath: mainDocsResponse?.path,
- };
- } catch (error) {
- console.error(
- "An error occurred while creating the folder in both locations: ",
- error,
- );
- setRejectedFiles([
- {
- fileName: name,
- message:
- error instanceof Error ? error.message : "Failed to create folder",
- },
- ]);
- throw error;
- }
-}
diff --git a/lib/swr/use-dataroom.ts b/lib/swr/use-dataroom.ts
index 429a6982..ae5410e5 100644
--- a/lib/swr/use-dataroom.ts
+++ b/lib/swr/use-dataroom.ts
@@ -123,7 +123,7 @@ export function useDataroomItems({
>(
teamId &&
id &&
- `/api/teams/${teamId}/datarooms/${id}${name ? `/folders/documents/${name.join("/")}` : "/documents"}`,
+ `/api/teams/${teamId}/datarooms/${id}${name ? `/folder-documents/${name.join("/")}` : "/documents"}`,
fetcher,
{
@@ -321,7 +321,7 @@ export function useDataroomFolderDocuments({ name }: { name: string[] }) {
teamId &&
id &&
name &&
- `/api/teams/${teamId}/datarooms/${id}/folders/documents/${name.join("/")}`,
+ `/api/teams/${teamId}/datarooms/${id}/folder-documents/${name.join("/")}`,
fetcher,
{
revalidateOnFocus: false,
diff --git a/lib/swr/use-documents.ts b/lib/swr/use-documents.ts
index 55316763..b02c0382 100644
--- a/lib/swr/use-documents.ts
+++ b/lib/swr/use-documents.ts
@@ -65,7 +65,7 @@ export function useFolderDocuments({ name }: { name: string[] }) {
>(
teamInfo?.currentTeam?.id &&
name.length > 0 &&
- `/api/teams/${teamInfo?.currentTeam?.id}/folders/documents/${name.join("/")}`,
+ `/api/teams/${teamInfo?.currentTeam?.id}/folder-documents/${name.join("/")}`,
fetcher,
{
revalidateOnFocus: false,
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/documents/[...name].ts b/pages/api/teams/[teamId]/datarooms/[id]/folder-documents/[...name].ts
similarity index 79%
rename from pages/api/teams/[teamId]/datarooms/[id]/folders/documents/[...name].ts
rename to pages/api/teams/[teamId]/datarooms/[id]/folder-documents/[...name].ts
index 20a9566d..9ef9104a 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/documents/[...name].ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folder-documents/[...name].ts
@@ -13,7 +13,14 @@ export default async function handle(
res: NextApiResponse,
) {
if (req.method === "GET") {
- // GET /api/teams/:teamId/datarooms/:id/folders/documents/:name
+ // GET /api/teams/:teamId/datarooms/:id/folder-documents/:name
+ //
+ // Returns the documents directly inside the folder at the given path.
+ // Lives outside the `folders/` namespace because `folders/[...name]` is
+ // a catch-all β€” a literal `folders/documents/<path>` URL would otherwise
+ // be ambiguous (and Next.js would route it here instead of to the folder
+ // listing endpoint, breaking any folder whose top-level segment slugifies
+ // to "documents").
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
@@ -39,16 +46,15 @@ export default async function handle(
const path = "/" + validatedName.join("/"); // construct the materialized path
try {
- // Check if the user is part of the team
+ // Scope the dataroom through the team membership check to avoid IDORs
+ // where a valid team member supplies another team's dataroom id.
const team = await prisma.team.findUnique({
where: {
id: teamId,
- users: {
- some: {
- userId: userId,
- },
- },
+ users: { some: { userId } },
+ datarooms: { some: { id: dataroomId } },
},
+ select: { id: true },
});
if (!team) {
@@ -114,6 +120,10 @@ export default async function handle(
},
});
+ if (documents.length === 0) {
+ return res.status(200).json([]);
+ }
+
const sortedDocuments = sortItemsByIndexAndName(documents);
return res.status(200).json(sortedDocuments);
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/bulk.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/bulk.ts
new file mode 100644
index 00000000..d2ea6ad9
--- /dev/null
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/bulk.ts
@@ -0,0 +1,153 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { Prisma } from "@prisma/client";
+import { getServerSession } from "next-auth/next";
+import { z } from "zod";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+
+import {
+ BulkFolderInput,
+ BulkValidationError,
+ MAX_BULK_FOLDERS_PER_REQUEST,
+ bulkCreateDataroomFolders,
+ getSafeBulkValidationMessage,
+} from "@/lib/folders/bulk-create";
+import prisma from "@/lib/prisma";
+import { ratelimit } from "@/lib/redis";
+import { CustomUser } from "@/lib/types";
+
+const BulkSchema = z.object({
+ /**
+ * Path of the parent folder under which to create the tree.
+ * Empty string / undefined means dataroom root. Leading slash optional.
+ */
+ rootPath: z.string().max(2000).optional(),
+ folders: z
+ .array(
+ z.object({
+ tempId: z.string().min(1).max(64),
+ name: z.string().min(1).max(255),
+ parentTempId: z.string().min(1).max(64).optional().nullable(),
+ /**
+ * Absolute path of an existing folder (e.g. created by a prior bulk
+ * chunk) β€” used by the client when splitting very large trees across
+ * multiple requests. Ignored if `parentTempId` is set.
+ */
+ parentPath: z.string().min(1).max(2000).optional().nullable(),
+ }),
+ )
+ .min(1)
+ .max(MAX_BULK_FOLDERS_PER_REQUEST),
+});
+
+function normaliseRootPath(input: string | undefined): string {
+ if (!input) return "/";
+ const trimmed = input.trim();
+ if (trimmed === "" || trimmed === "/") return "/";
+ return trimmed.startsWith("/") ? trimmed : "/" + trimmed;
+}
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method !== "POST") {
+ res.setHeader("Allow", ["POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const userId = (session.user as CustomUser).id;
+ const { teamId, id: dataroomId } = req.query as {
+ teamId: string;
+ id: string;
+ };
+
+ const parsed = BulkSchema.safeParse(req.body);
+ if (!parsed.success) {
+ return res.status(400).json({
+ error: "Invalid request body",
+ details: parsed.error.flatten(),
+ });
+ }
+
+ const rootPath = normaliseRootPath(parsed.data.rootPath);
+ const inputFolders: BulkFolderInput[] = parsed.data.folders;
+
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: { some: { userId } },
+ datarooms: { some: { id: dataroomId } },
+ },
+ select: { id: true },
+ });
+
+ if (!team) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ // 10/min allows ~10 chunks per drop (with MAX_BULK_FOLDERS_PER_REQUEST
+ // = 500 that's up to ~5,000 folders/min per user per dataroom); higher
+ // bursts are unusual and worth throttling.
+ const { success } = await ratelimit(10, "1 m").limit(
+ `bulk-dataroom-folders:${teamId}:${dataroomId}:${userId}`,
+ );
+ if (!success) {
+ return res.status(429).json({
+ error: "RATE_LIMITED",
+ message: "Too many bulk folder requests. Please try again shortly.",
+ });
+ }
+
+ const created = await prisma.$transaction(async (tx) => {
+ let rootParentId: string | null = null;
+ if (rootPath !== "/") {
+ const parent = await tx.dataroomFolder.findUnique({
+ where: { dataroomId_path: { dataroomId, path: rootPath } },
+ select: { id: true },
+ });
+ if (!parent) {
+ throw new BulkValidationError(
+ "UNKNOWN_ROOT_PATH",
+ "Parent folder does not exist",
+ );
+ }
+ rootParentId = parent.id;
+ }
+
+ return bulkCreateDataroomFolders({
+ tx,
+ dataroomId,
+ rootPath,
+ rootParentId,
+ folders: inputFolders,
+ });
+ });
+
+ return res.status(201).json({ folders: created });
+ } catch (error) {
+ if (error instanceof BulkValidationError) {
+ return res.status(400).json({
+ error: error.code,
+ message: getSafeBulkValidationMessage(error.code),
+ });
+ }
+ if (error instanceof Prisma.PrismaClientKnownRequestError) {
+ console.error("Bulk dataroom folder create failed:", error);
+ return res
+ .status(500)
+ .json({ error: "DB_ERROR", message: "Failed to create folders" });
+ }
+ console.error("Bulk dataroom folder create failed:", error);
+ return res
+ .status(500)
+ .json({ error: "INTERNAL_ERROR", message: "Failed to create folders" });
+ }
+}
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
index 2feeaaa8..63cb302e 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
@@ -2,9 +2,9 @@ import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { DefaultPermissionStrategy, ItemType } from "@prisma/client";
-import { safeSlugify } from "@/lib/utils";
import { getServerSession } from "next-auth/next";
+import { resolveFreeFolderPath } from "@/lib/folders/bulk-create";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
@@ -221,16 +221,14 @@ export default async function handle(
};
try {
- // Check if the user is part of the team
+ // Check team membership and dataroom ownership together.
const team = await prisma.team.findUnique({
where: {
id: teamId,
- users: {
- some: {
- userId: userId,
- },
- },
+ users: { some: { userId } },
+ datarooms: { some: { id: dataroomId } },
},
+ select: { id: true },
});
if (!team) {
@@ -436,16 +434,14 @@ export default async function handle(
const parentFolderPath = path ? "/" + path : "/";
try {
- // Check if the user is part of the team
+ // Check team membership and dataroom ownership together.
const team = await prisma.team.findUnique({
where: {
id: teamId,
- users: {
- some: {
- userId: userId,
- },
- },
+ users: { some: { userId } },
+ datarooms: { some: { id: dataroomId } },
},
+ select: { id: true },
});
if (!team) {
@@ -466,35 +462,19 @@ export default async function handle(
},
});
- // Duplicate name handling
- let folderName = name;
- let counter = 1;
- const MAX_RETRIES = 50;
-
- // Split path into segments
- // Slugify the final folder name
- const pathSegments = path ? path.split("/").filter(Boolean) : [];
- const basePath =
- pathSegments.length > 0 ? "/" + pathSegments.join("/") + "/" : "/";
-
- let childFolderPath = basePath + safeSlugify(folderName);
-
- while (counter <= MAX_RETRIES) {
- const existingFolder = await prisma.dataroomFolder.findUnique({
- where: {
- dataroomId_path: {
- dataroomId: dataroomId,
- path: childFolderPath,
- },
- },
- });
- if (!existingFolder) break;
- folderName = `${name} (${counter})`;
- childFolderPath = basePath + safeSlugify(folderName);
- counter++;
- }
-
- if (counter > MAX_RETRIES) {
+ const resolved = await resolveFreeFolderPath({
+ name,
+ parentPath: parentFolderPath,
+ findExisting: (candidates) =>
+ prisma.dataroomFolder.findMany({
+ where: { dataroomId, path: { in: candidates } },
+ select: { path: true },
+ }),
+ }).catch((err) => {
+ if (err?.code === "SLUG_EXHAUSTED") return null;
+ throw err;
+ });
+ if (!resolved) {
return res.status(400).json({
error: "Failed to create folder",
message: "Too many folders with similar names",
@@ -503,8 +483,8 @@ export default async function handle(
const folder = await prisma.dataroomFolder.create({
data: {
- name: folderName,
- path: childFolderPath,
+ name: resolved.name,
+ path: resolved.path,
parentId: parentFolder?.id ?? null,
dataroomId: dataroomId,
icon: icon ?? null,
@@ -512,7 +492,7 @@ export default async function handle(
},
});
- await applyFolderPermissions(dataroomId, folder.id, childFolderPath);
+ await applyFolderPermissions(dataroomId, folder.id, resolved.path);
const folderWithDocs = {
...folder,
diff --git a/pages/api/teams/[teamId]/folders/documents/[...name].ts b/pages/api/teams/[teamId]/folder-documents/[...name].ts
similarity index 95%
rename from pages/api/teams/[teamId]/folders/documents/[...name].ts
rename to pages/api/teams/[teamId]/folder-documents/[...name].ts
index 360892bf..b59b59f3 100644
--- a/pages/api/teams/[teamId]/folders/documents/[...name].ts
+++ b/pages/api/teams/[teamId]/folder-documents/[...name].ts
@@ -12,7 +12,10 @@ export default async function handle(
res: NextApiResponse,
) {
if (req.method === "GET") {
- // GET /api/teams/:teamId/folders/documents/:name
+ // GET /api/teams/:teamId/folder-documents/:name
+ //
+ // See dataroom variant for why this sits outside the `folders/`
+ // namespace.
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
@@ -79,6 +82,9 @@ export default async function handle(
// Then, get counts efficiently with separate GROUP BY queries
const documentIds = documents.map((d) => d.id);
+ if (documentIds.length === 0) {
+ return res.status(200).json([]);
+ }
const [linkCounts, viewCounts, versionCounts, dataroomCounts] =
await Promise.all([
diff --git a/pages/api/teams/[teamId]/folders/bulk.ts b/pages/api/teams/[teamId]/folders/bulk.ts
new file mode 100644
index 00000000..5625f296
--- /dev/null
+++ b/pages/api/teams/[teamId]/folders/bulk.ts
@@ -0,0 +1,134 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { Prisma } from "@prisma/client";
+import { getServerSession } from "next-auth/next";
+import { z } from "zod";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+
+import {
+ BulkFolderInput,
+ BulkValidationError,
+ MAX_BULK_FOLDERS_PER_REQUEST,
+ bulkCreateMainDocsFolders,
+ getSafeBulkValidationMessage,
+} from "@/lib/folders/bulk-create";
+import prisma from "@/lib/prisma";
+import { ratelimit } from "@/lib/redis";
+import { CustomUser } from "@/lib/types";
+
+const BulkSchema = z.object({
+ rootPath: z.string().max(2000).optional(),
+ folders: z
+ .array(
+ z.object({
+ tempId: z.string().min(1).max(64),
+ name: z.string().min(1).max(255),
+ parentTempId: z.string().min(1).max(64).optional().nullable(),
+ parentPath: z.string().min(1).max(2000).optional().nullable(),
+ }),
+ )
+ .min(1)
+ .max(MAX_BULK_FOLDERS_PER_REQUEST),
+});
+
+function normaliseRootPath(input: string | undefined): string {
+ if (!input) return "/";
+ const trimmed = input.trim();
+ if (trimmed === "" || trimmed === "/") return "/";
+ return trimmed.startsWith("/") ? trimmed : "/" + trimmed;
+}
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method !== "POST") {
+ res.setHeader("Allow", ["POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const userId = (session.user as CustomUser).id;
+ const { teamId } = req.query as { teamId: string };
+
+ const parsed = BulkSchema.safeParse(req.body);
+ if (!parsed.success) {
+ return res.status(400).json({
+ error: "Invalid request body",
+ details: parsed.error.flatten(),
+ });
+ }
+
+ const rootPath = normaliseRootPath(parsed.data.rootPath);
+ const inputFolders: BulkFolderInput[] = parsed.data.folders;
+
+ try {
+ const teamAccess = await prisma.userTeam.findUnique({
+ where: { userId_teamId: { userId, teamId } },
+ select: { teamId: true },
+ });
+ if (!teamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ // See dataroom variant for the rationale on 10/min.
+ const { success } = await ratelimit(10, "1 m").limit(
+ `bulk-folders:${teamId}:${userId}`,
+ );
+ if (!success) {
+ return res.status(429).json({
+ error: "RATE_LIMITED",
+ message: "Too many bulk folder requests. Please try again shortly.",
+ });
+ }
+
+ const created = await prisma.$transaction(async (tx) => {
+ let rootParentId: string | null = null;
+ if (rootPath !== "/") {
+ const parent = await tx.folder.findUnique({
+ where: { teamId_path: { teamId, path: rootPath } },
+ select: { id: true },
+ });
+ if (!parent) {
+ throw new BulkValidationError(
+ "UNKNOWN_ROOT_PATH",
+ "Parent folder does not exist",
+ );
+ }
+ rootParentId = parent.id;
+ }
+
+ return bulkCreateMainDocsFolders({
+ tx,
+ teamId,
+ rootPath,
+ rootParentId,
+ folders: inputFolders,
+ });
+ });
+
+ return res.status(201).json({ folders: created });
+ } catch (error) {
+ if (error instanceof BulkValidationError) {
+ return res.status(400).json({
+ error: error.code,
+ message: getSafeBulkValidationMessage(error.code),
+ });
+ }
+ if (error instanceof Prisma.PrismaClientKnownRequestError) {
+ console.error("Bulk main-docs folder create failed:", error);
+ return res
+ .status(500)
+ .json({ error: "DB_ERROR", message: "Failed to create folders" });
+ }
+ console.error("Bulk main-docs folder create failed:", error);
+ return res
+ .status(500)
+ .json({ error: "INTERNAL_ERROR", message: "Failed to create folders" });
+ }
+}
diff --git a/pages/api/teams/[teamId]/folders/index.ts b/pages/api/teams/[teamId]/folders/index.ts
index 8d9b1877..70f219e9 100644
--- a/pages/api/teams/[teamId]/folders/index.ts
+++ b/pages/api/teams/[teamId]/folders/index.ts
@@ -1,9 +1,9 @@
import { NextApiRequest, NextApiResponse } from "next";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
-import { safeSlugify } from "@/lib/utils";
import { getServerSession } from "next-auth/next";
+import { resolveFreeFolderPath } from "@/lib/folders/bulk-create";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
@@ -147,34 +147,19 @@ export default async function handle(
},
});
- let folderName = name;
- let counter = 1;
- const MAX_RETRIES = 50;
-
- let childFolderPath = path
- ? "/" + path + "/" + safeSlugify(folderName)
- : "/" + safeSlugify(folderName);
-
- while (counter <= MAX_RETRIES) {
- const existingFolder = await prisma.folder.findUnique({
- where: {
- teamId_path: {
- teamId: teamId,
- path: childFolderPath,
- },
- },
- });
-
- if (!existingFolder) break;
-
- folderName = `${name} (${counter})`;
- childFolderPath = path
- ? "/" + path + "/" + safeSlugify(folderName)
- : "/" + safeSlugify(folderName);
- counter++;
- }
-
- if (counter > MAX_RETRIES) {
+ const resolved = await resolveFreeFolderPath({
+ name,
+ parentPath: parentFolderPath,
+ findExisting: (candidates) =>
+ prisma.folder.findMany({
+ where: { teamId, path: { in: candidates } },
+ select: { path: true },
+ }),
+ }).catch((err) => {
+ if (err?.code === "SLUG_EXHAUSTED") return null;
+ throw err;
+ });
+ if (!resolved) {
return res.status(400).json({
error: "Failed to create folder",
message: "Too many folders with similar names",
@@ -183,8 +168,8 @@ export default async function handle(
const folder = await prisma.folder.create({
data: {
- name: folderName,
- path: childFolderPath,
+ name: resolved.name,
+ path: resolved.path,
parentId: parentFolder?.id ?? null,
teamId: teamId,
icon: icon ?? null,