Files
dataroom/lib/files/bulk-download-presign.ts
zeekay 9dff9e667e rip: enterprise dirs + paywalls — full AGPL, no commercial-license code
Drop the dual-licensed `ee/` tree entirely. All features previously
locked behind the Hanzo Dataroom Commercial License are now part of the
AGPL-3.0 core and freely available — custom domains, branded viewing,
advanced analytics, watermarks, access controls, Q&A, AI vector stores,
workflows, SAML SSO, dataroom invitations, conversations, templates.

Layout changes:
* `ee/features/{ai,conversations,workflows,permissions,storage,templates,
  dataroom-invitations,access-notifications,security,conversions}/` →
  `features/<name>/` (AGPL).
* `ee/limits/` → `lib/billing/limits/` with every plan resolving to an
  unlimited profile (no users/links/documents/domains/datarooms cap,
  conversations and watermarks always on).
* `ee/stripe/` → `lib/billing/legacy/` (already a compat shim over
  `@hanzoai/commerce`; renewal-reminder webhook is now a no-op).
* `ee/features/billing/cancellation/` deleted. Pause/unpause/cancel/
  retention routes now compile against `lib/billing/cancellation.ts`
  stubs that return 410 Gone; `isTeamPaused`/`isTeamPausedById` in
  `lib/billing/paused.ts` always report active. `CancellationModal` is
  a no-op shim in `components/billing/`.
* `app/(ee)/api/*` route group folded into `app/api/*` (SAML auth,
  SCIM, AI chat, workflows, FAQ, link-upload).

Build: `npm run build` green.
2026-06-07 11:46:48 -07:00

84 lines
2.8 KiB
TypeScript

import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { getTeamStorageConfigById } from "@/features/storage/config";
export interface S3KeyInfo {
bucket: string;
key: string;
region: string;
}
const THREE_DAYS_IN_SECONDS = 3 * 24 * 60 * 60;
/**
* Parse an S3 presigned URL to extract bucket, key, and region.
* Supports both path-style and virtual-hosted-style URLs.
*
* Path-style: https://s3.{region}.amazonaws.com/{bucket}/{key}
* Virtual-hosted: https://{bucket}.s3.{region}.amazonaws.com/{key}
*/
export function parseS3PresignedUrl(presignedUrl: string): S3KeyInfo {
const url = new URL(presignedUrl);
const hostname = url.hostname;
// Path-style: s3.{region}.amazonaws.com
const pathStyleMatch = hostname.match(/^s3\.([^.]+)\.amazonaws\.com$/);
if (pathStyleMatch) {
const region = pathStyleMatch[1];
// Path is /{bucket}/{key...}
const pathParts = url.pathname.slice(1).split("/");
const bucket = decodeURIComponent(pathParts[0]);
const key = pathParts.slice(1).map(decodeURIComponent).join("/");
return { bucket, key, region };
}
// Virtual-hosted-style: {bucket}.s3.{region}.amazonaws.com
const virtualMatch = hostname.match(/^(.+)\.s3\.([^.]+)\.amazonaws\.com$/);
if (virtualMatch) {
const bucket = virtualMatch[1];
const region = virtualMatch[2];
const key = decodeURIComponent(url.pathname.slice(1));
return { bucket, key, region };
}
// Virtual-hosted without region: {bucket}.s3.amazonaws.com (us-east-1)
const virtualNoRegionMatch = hostname.match(/^(.+)\.s3\.amazonaws\.com$/);
if (virtualNoRegionMatch) {
const bucket = virtualNoRegionMatch[1];
const key = decodeURIComponent(url.pathname.slice(1));
return { bucket: bucket, key, region: "us-east-1" };
}
throw new Error(`Unable to parse S3 URL: ${presignedUrl}`);
}
/**
* Generate a fresh presigned URL for an S3 object using team credentials.
* Uses long-term IAM credentials which allow presigned URLs up to 7 days.
*/
export async function generateFreshPresignedUrl(
teamId: string,
s3Key: S3KeyInfo,
expiresInSeconds: number = THREE_DAYS_IN_SECONDS,
): Promise<string> {
const config = await getTeamStorageConfigById(teamId);
const client = new S3Client({
region: s3Key.region,
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
},
});
const command = new GetObjectCommand({
Bucket: s3Key.bucket,
Key: s3Key.key,
ResponseContentDisposition: `attachment; filename="${encodeURIComponent(s3Key.key.split("/").pop() || "download.zip")}"`,
ResponseCacheControl: "no-cache, no-store, must-revalidate",
});
return getSignedUrl(client, command, { expiresIn: expiresInSeconds });
}