Files
OpenFrame/lib/r2-upload-session.ts
T
yusufipk 880d0ac0fa feat: chunked (S3 multipart) uploads for R2/S3 video backend
Self-hosted instances on the R2/S3 backend could only upload a video as a
single PUT, which fails behind a Cloudflare proxy/tunnel (100MB request-body
cap) and is capped at 5GiB with no resilience. Bunny already avoids this via
tus; this brings the R2/S3 path to parity.

Files larger than a threshold (default 90MiB) are now split into parts
(default 32MiB, min 5MiB) and uploaded directly browser->R2 via presigned
UploadPart URLs, then reassembled server-side with CompleteMultipartUpload.
Each request stays under the 100MB cap, lifts the size ceiling well past
5GiB, and adds per-chunk retry. Files at/under the threshold keep the
existing single-PUT path unchanged. Bunny path is untouched.

Thresholds are env-overridable via OPENFRAME_R2_MULTIPART_THRESHOLD_BYTES
and OPENFRAME_R2_MULTIPART_PART_SIZE_BYTES.

Verified end-to-end against real Cloudflare R2 and a local MinIO behind an
nginx 90MB cap (single 141MB PUT 413s on master; 32MB parts pass here).

Closes #22
2026-07-10 19:19:07 +07:00

48 lines
1.2 KiB
TypeScript

import { db } from '@/lib/db';
export type CreateR2UploadSessionInput = {
userId: string;
projectId: string;
billedUserId: string;
objectKey: string;
thumbnailObjectKey: string;
declaredSizeBytes: bigint;
contentType: string;
reservationId: string | null;
uploadJti: string;
expiresAt: Date;
multipartUploadId?: string | null;
};
export async function createR2UploadSession(input: CreateR2UploadSessionInput) {
return db.videoUploadSession.create({
data: {
userId: input.userId,
projectId: input.projectId,
billedUserId: input.billedUserId,
objectKey: input.objectKey,
thumbnailObjectKey: input.thumbnailObjectKey,
declaredSizeBytes: input.declaredSizeBytes,
contentType: input.contentType,
reservationId: input.reservationId,
uploadJti: input.uploadJti,
expiresAt: input.expiresAt,
multipartUploadId: input.multipartUploadId ?? null,
},
});
}
export async function cancelR2UploadSession(sessionId: string) {
return db.videoUploadSession.updateMany({
where: {
id: sessionId,
status: 'INITIATED',
expiresAt: { gt: new Date() },
},
data: {
status: 'CANCELLED',
consumedAt: new Date(),
},
});
}