mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
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]>
232 lines
6.8 KiB
TypeScript
232 lines
6.8 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { createTusStore } from "@/lib/storage/tus-store";
|
|
import { CopyObjectCommand } from "@aws-sdk/client-s3";
|
|
import { Server } from "@tus/server";
|
|
import { getServerSession } from "next-auth/next";
|
|
import path from "node:path";
|
|
|
|
import { getS3ClientAndConfig } from "@/lib/files/aws-client";
|
|
import { KvLocker } from "@/lib/files/tus-kv-locker";
|
|
import { newId } from "@/lib/id-helper";
|
|
import prisma from "@/lib/prisma";
|
|
import { lockKv } from "@/lib/kv";
|
|
import { CustomUser } from "@/lib/types";
|
|
import { log, safeSlugify } from "@/lib/utils";
|
|
import { getFileSizeLimit } from "@/lib/utils/get-file-size-limits";
|
|
|
|
import { authOptions } from "../../auth/[...nextauth]";
|
|
|
|
export const config = {
|
|
maxDuration: 60,
|
|
api: {
|
|
bodyParser: false,
|
|
},
|
|
};
|
|
|
|
const locker = new KvLocker({
|
|
client: lockKv,
|
|
});
|
|
|
|
const BYTES_PER_MEGABYTE = 1024 * 1024;
|
|
type TusErrorResponse = { status_code: number; body: string };
|
|
|
|
type TusAuthenticatedRequest = NextApiRequest & {
|
|
authenticatedUserId?: string;
|
|
};
|
|
|
|
const tusServer = new Server({
|
|
// `path` needs to match the route declared by the next file router
|
|
path: "/api/file/tus",
|
|
maxSize: 1024 * 1024 * 1024 * 2, // 2 GiB
|
|
respectForwardedHeaders: true,
|
|
locker,
|
|
datastore: createTusStore(),
|
|
namingFunction(req, metadata) {
|
|
const { teamId, fileName } = metadata as {
|
|
teamId: string;
|
|
fileName: string;
|
|
};
|
|
const docId = newId("doc");
|
|
const { name, ext } = path.parse(fileName);
|
|
const newName = `${teamId}/${docId}/${safeSlugify(name)}${ext}`;
|
|
return newName;
|
|
},
|
|
generateUrl(req, { proto, host, path, id }) {
|
|
// Encode the ID to be URL safe
|
|
id = Buffer.from(id, "utf-8").toString("base64url");
|
|
return `${proto}://${host}${path}/${id}`;
|
|
},
|
|
getFileIdFromRequest(req) {
|
|
// Extract the ID from the URL
|
|
const id = (req.url as string).split("/api/file/tus/")[1];
|
|
return Buffer.from(id, "base64url").toString("utf-8");
|
|
},
|
|
onResponseError(req, res, err) {
|
|
if (typeof err === "object" && err !== null) {
|
|
const tusError = err as { status_code?: unknown; body?: unknown };
|
|
if (
|
|
typeof tusError.status_code === "number" &&
|
|
typeof tusError.body === "string"
|
|
) {
|
|
const errorResponse: TusErrorResponse = {
|
|
status_code: tusError.status_code,
|
|
body: tusError.body,
|
|
};
|
|
return errorResponse;
|
|
}
|
|
}
|
|
|
|
log({
|
|
message: "Error uploading a file. Error: \n\n" + err,
|
|
type: "error",
|
|
});
|
|
return { status_code: 500, body: "Internal Server Error" };
|
|
},
|
|
async onIncomingRequest(req, res, uploadId) {
|
|
const userId = (req as TusAuthenticatedRequest).authenticatedUserId;
|
|
if (!userId) {
|
|
throw { status_code: 401, body: "Unauthorized" };
|
|
}
|
|
|
|
// Upload creation is validated in onUploadCreate; here we protect follow-up
|
|
// requests (HEAD/PATCH/DELETE) so only team members can touch an upload URL.
|
|
if (!uploadId || req.method === "POST") {
|
|
return;
|
|
}
|
|
|
|
const decodedUploadId = uploadId.includes("/")
|
|
? uploadId
|
|
: Buffer.from(uploadId, "base64url").toString("utf-8");
|
|
const uploadTeamId = decodedUploadId.split("/")[0];
|
|
|
|
if (!uploadTeamId) {
|
|
throw { status_code: 400, body: "Invalid upload id" };
|
|
}
|
|
|
|
const hasTeamAccess = await prisma.userTeam.findUnique({
|
|
where: {
|
|
userId_teamId: {
|
|
userId,
|
|
teamId: uploadTeamId,
|
|
},
|
|
},
|
|
select: {
|
|
userId: true,
|
|
},
|
|
});
|
|
|
|
if (!hasTeamAccess) {
|
|
throw { status_code: 403, body: "Unauthorized to access this team" };
|
|
}
|
|
},
|
|
async onUploadCreate(req, res, upload) {
|
|
const userId = (req as TusAuthenticatedRequest).authenticatedUserId;
|
|
if (!userId) {
|
|
throw { status_code: 401, body: "Unauthorized" };
|
|
}
|
|
|
|
const metadata = upload.metadata || {};
|
|
const teamId = metadata.teamId;
|
|
const fileName = metadata.fileName;
|
|
const contentType = metadata.contentType || "application/octet-stream";
|
|
|
|
if (!teamId || !fileName) {
|
|
throw { status_code: 400, body: "Missing required upload metadata" };
|
|
}
|
|
|
|
const team = await prisma.team.findUnique({
|
|
where: {
|
|
id: teamId,
|
|
users: {
|
|
some: {
|
|
userId,
|
|
},
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
});
|
|
|
|
if (!team) {
|
|
throw { status_code: 403, body: "Unauthorized to access this team" };
|
|
}
|
|
|
|
const uploadSize = upload.size;
|
|
if (
|
|
typeof uploadSize !== "number" ||
|
|
!Number.isFinite(uploadSize) ||
|
|
uploadSize <= 0
|
|
) {
|
|
throw { status_code: 400, body: "Missing or invalid upload length" };
|
|
}
|
|
|
|
const fileSizeLimitMb = getFileSizeLimit(contentType);
|
|
const fileSizeLimitBytes = fileSizeLimitMb * BYTES_PER_MEGABYTE;
|
|
|
|
if (uploadSize > fileSizeLimitBytes) {
|
|
throw {
|
|
status_code: 413,
|
|
body: `File size too big for ${contentType} (max. ${fileSizeLimitMb} MB)`,
|
|
};
|
|
}
|
|
|
|
return res;
|
|
},
|
|
async onUploadFinish(req, res, upload) {
|
|
try {
|
|
const metadata = upload.metadata || {};
|
|
const contentType = metadata.contentType || "application/octet-stream";
|
|
const { name, ext } = path.parse(metadata.fileName!);
|
|
const originalFileName = `${name}${ext}`;
|
|
const contentDisposition = `attachment; filename="${safeSlugify(name)}${ext}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
|
|
|
|
// The Key (object path) where the file was uploaded
|
|
const objectKey = upload.id;
|
|
|
|
// Extract teamId from the object key (format: teamId/docId/filename)
|
|
const teamId = objectKey.split("/")[0];
|
|
if (!teamId) {
|
|
throw { status_code: 500, body: "Invalid object key format" };
|
|
}
|
|
|
|
// Get team-specific S3 client and config
|
|
const { client, config } = getS3ClientAndConfig();
|
|
|
|
// Copy the object onto itself, replacing the metadata
|
|
const params = {
|
|
Bucket: config.bucket,
|
|
CopySource: `${config.bucket}/${objectKey}`,
|
|
Key: objectKey,
|
|
ContentType: contentType,
|
|
ContentDisposition: contentDisposition,
|
|
MetadataDirective: "REPLACE" as const,
|
|
};
|
|
|
|
const copyCommand = new CopyObjectCommand(params);
|
|
await client.send(copyCommand);
|
|
|
|
return res;
|
|
} catch (error) {
|
|
throw { status_code: 500, body: "Error updating metadata" };
|
|
}
|
|
},
|
|
});
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
// Get the session
|
|
const session = await getServerSession(req, res, authOptions);
|
|
const userId = (session?.user as CustomUser | undefined)?.id;
|
|
if (!userId) {
|
|
return res.status(401).json({ message: "Unauthorized" });
|
|
}
|
|
|
|
(req as TusAuthenticatedRequest).authenticatedUserId = userId;
|
|
|
|
return tusServer.handle(req, res);
|
|
}
|