Files
dataroom/pages/api/file/s3/get-presigned-post-url.ts
zeekayandHanzo Dev 23a1657d2e storage: original Hanzo storage layer, drop the relocated upstream ee code
features/storage/ was Papermark's ee/features/storage — commercial-licensed
code that commit 98f0da7fa renamed out of ee/ byte-identical rather than
deleting, leaving it under our AGPL root licence. Deleted and rewritten.

lib/storage/config.ts resolves one bucket from the environment. The upstream
version carried a hardcoded EU/US region matrix selected by the `usStorage`
feature flag; that flag is served by @vercel/edge-config, is false whenever
EDGE_CONFIG is unset, and no per-document region is persisted anywhere in the
schema — so the second region was unreachable on our deployments. Required
vars now throw at the boundary instead of resolving a partial config.

lib/storage/tus-store.ts builds a plain S3Store. MultiRegionS3Store subclassed
it and reassigned this.client/this.bucket per request on a shared instance, so
two concurrent uploads from different teams could observe each other's bucket.
Construction-time config removes the race. It also now forwards the endpoint,
so resumable uploads reach S3-compatible backends.

lib/files/aws-client.ts collapses to two functions: with one config the
team/non-team split was duplication, and getS3Client, getS3ClientForTeam and
getLambdaClient had no callers outside the module.

LICENSE: drop the carve-out clause pointing at ee/LICENSE and app/(ee) —
both directories are gone, so the pointer dangled. Papermark's copyright
line and the AGPL-3.0 text are untouched.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-03 19:16:33 -07:00

81 lines
2.2 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { getServerSession } from "next-auth";
import path from "node:path";
import { ONE_HOUR, ONE_SECOND } from "@/lib/constants";
import { getS3ClientAndConfig } from "@/lib/files/aws-client";
import { safeSlugify } from "@/lib/utils";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
import { authOptions } from "../../auth/[...nextauth]";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method !== "POST") {
return res.status(405).end("Method Not Allowed");
}
const { fileName, contentType, teamId, docId } = req.body as {
fileName: string;
contentType: string;
teamId: string;
docId: string;
};
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
}
const team = await prisma.team.findUnique({
where: {
id: teamId,
users: {
some: {
userId: (session.user as CustomUser).id,
},
},
},
select: { id: true },
});
if (!team) {
return res.status(403).end("Unauthorized to access this team");
}
try {
// Get the basename and extension for the file
const { name, ext } = path.parse(fileName);
const slugifiedName = safeSlugify(name) + ext;
const originalFileName = `${name}${ext}`;
const key = `${team.id}/${docId}/${slugifiedName}`;
const contentDisposition = `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
const { client, config } = getS3ClientAndConfig();
const putObjectCommand = new PutObjectCommand({
Bucket: config.bucket,
Key: key,
ContentType: contentType,
ContentDisposition: contentDisposition,
});
const url = await getSignedUrl(client, putObjectCommand, {
expiresIn: ONE_HOUR / ONE_SECOND,
});
return res
.status(200)
.json({ url, key, docId, fileName: slugifiedName, contentDisposition });
} catch (error) {
return res.status(500).json({ error: "Internal server error" });
}
}