Files
zeekayandHanzo Dev 6b68c9299e kv: one name for the store, and it is Hanzo KV
The client was already @hanzo/kv; only the vocabulary was still Redis. Every
module, symbol and comment now says what it connects to:

  lib/redis.ts                    -> lib/kv.ts                (exports kv, lockKv)
  lib/redis-job-store.ts          -> lib/kv-job-store.ts      (KvJobStore)
  lib/redis-download-job-store.ts -> lib/kv-download-job-store.ts
  lib/files/tus-redis-locker.ts   -> lib/files/tus-kv-locker.ts (KvLocker, KvLock)
  lib/api/domains/redis.ts        -> lib/api/domains/kv.ts    (getKey)
  lib/redis/                      -> lib/kv-queue/

KV_URL is now the only endpoint variable — the REDIS_URL fallback and the
redis://localhost default are gone, and the default scheme is kv://, which
@hanzo/kv parses natively (utils/index.js). .env.example gained the KV_URL it
never documented, and lost UPSTASH_REDIS_REST_LOCKER_URL/TOKEN: nothing has read
those since the client changed — the locker opens a second connection to the
same KV, so there is no second endpoint to configure.

The second connection is `lockKv`, not `locker`: both tus routes already have a
local `const locker = new KvLocker(...)`, so naming the client `locker` made the
lock take itself as its own client.

Verified against the repo's own gate (npx tsc --noEmit): 100 errors before and
after, 0 introduced, compared set-wise by file:line:code. Behaviour is untouched
— this is naming, one connection string, and dead config.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-09 10:09:34 -07:00

291 lines
8.6 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import { getStorageConfig } from "@/lib/storage/config";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { getServerSession } from "next-auth";
import {
buildFolderNameMap,
buildFolderPathsFromHierarchy,
} from "@/lib/dataroom/build-folder-hierarchy";
import prisma from "@/lib/prisma";
import { downloadJobStore } from "@/lib/kv-download-job-store";
import { bulkDownloadTask } from "@/lib/trigger/bulk-download";
import { CustomUser } from "@/lib/types";
export const config = {
maxDuration: 60, // Reduced since we're just triggering the async task
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
}
const { teamId, id: dataroomId } = req.query as {
teamId: string;
id: string;
};
const userId = (session.user as CustomUser).id;
if (req.method === "POST") {
try {
const teamAccess = await prisma.userTeam.findUnique({
where: {
userId_teamId: {
userId: userId,
teamId: teamId,
},
},
select: { teamId: true },
});
if (!teamAccess) {
return res.status(403).end("Unauthorized to access this team");
}
const dataroom = await prisma.dataroom.findUnique({
where: {
id: dataroomId,
teamId: teamId,
},
select: {
id: true,
name: true,
folders: {
select: {
id: true,
name: true,
path: true,
parentId: true,
},
},
documents: {
select: {
id: true,
folderId: true,
document: {
select: {
name: true,
versions: {
where: { isPrimary: true },
select: {
type: true,
file: true,
storageType: true,
originalFile: true,
contentType: true,
fileSize: true,
},
take: 1,
},
},
},
},
},
},
});
if (!dataroom) {
return res.status(404).end("Dataroom not found");
}
let downloadFolders = dataroom.folders;
let downloadDocuments = dataroom.documents;
// Build folder paths from the parentId hierarchy (source of truth)
// instead of using the materialized `path` field which can become stale
// after folder renames/moves
const computedPathMap = buildFolderPathsFromHierarchy(downloadFolders);
const folderMap = buildFolderNameMap(downloadFolders, computedPathMap);
// Construct folderStructure and fileKeys
const folderStructure: {
[key: string]: {
name: string;
path: string;
files: { name: string; key: string; size?: number }[];
};
} = {};
const fileKeys: string[] = [];
const addFileToStructure = (
path: string,
fileName: string,
fileKey: string,
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: [],
};
}
folderStructure[path].files.push({
name: fileName,
key: fileKey,
size: fileSize,
});
fileKeys.push(fileKey);
};
downloadDocuments
.filter((doc) => !doc.folderId)
.filter((doc) => doc.document.versions[0].type !== "notion")
.filter((doc) => doc.document.versions[0].storageType !== "VERCEL_BLOB")
.forEach((doc) =>
addFileToStructure(
"/",
doc.document.name,
doc.document.versions[0].originalFile ??
doc.document.versions[0].file,
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);
}
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) =>
addFileToStructure(
folderPath,
doc.document.name,
doc.document.versions[0].originalFile ??
doc.document.versions[0].file,
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" });
}
// Get team-specific storage config
const storageConfig = getStorageConfig();
// Get user email for notification
const user = await prisma.user.findUnique({
where: { id: userId },
select: { email: true },
});
// Create download job in KV
const job = await downloadJobStore.createJob({
type: "bulk",
status: "PENDING",
dataroomId: dataroom.id,
dataroomName: dataroom.name,
totalFiles: fileKeys.length,
processedFiles: 0,
progress: 0,
teamId: teamId,
userId: userId,
emailNotification: !!user?.email,
emailAddress: user?.email ?? undefined,
});
// Trigger the async bulk download task
const handle = await bulkDownloadTask.trigger(
{
jobId: job.id,
dataroomId: dataroom.id,
dataroomName: dataroom.name,
teamId: teamId,
folderStructure: folderStructure,
fileKeys: fileKeys,
sourceBucket: storageConfig.bucket,
watermarkConfig: { enabled: false }, // No watermark for team member downloads
userId: userId,
emailNotification: !!user?.email,
emailAddress: user?.email ?? undefined,
},
{
idempotencyKey: job.id,
tags: [
`team_${teamId}`,
`dataroom_${dataroom.id}`,
`job_${job.id}`,
`user_${userId}`,
],
},
);
// Update job with trigger run ID
await downloadJobStore.updateJob(job.id, {
triggerRunId: handle.id,
});
// Return job ID immediately (async response)
return res.status(202).json({
jobId: job.id,
status: "PENDING",
message: "Download started. You will be notified when ready.",
});
} catch (error) {
console.error("Error starting bulk download:", error);
return res.status(500).json({
message: "Internal Server Error",
error: (error as Error).message,
});
}
} else {
// We only allow POST requests
res.setHeader("Allow", ["POST"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}