mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
A reservation id was never a secret and could not have been one. An upload token is base64url(payload) followed by its signature, so a client can read every claim out of its own token, and the two R2 init routes hand their reservation ids to the client outright. The asset route takes a reservation id from the request body and deleted it on the strength of that id and the billed user alone, and every hold an account owns is billed to the same user. So a caller could start a Bunny upload, read the id out of the token they were just given, quote it while attaching a one byte image or even a bare YouTube link, and have the quota handed back while the upload carried on. Repeat and a trial worth three gigabytes uploads as much as it likes for as long as Bunny takes to report a figure of its own. Signing the id rather than handing it over bought nothing, because signing is not hiding. A hold now records what it was opened for and is only ever consumed by that flow, so naming one is no longer enough to drop it. Guests hold against the workspace owner's quota rather than their own and had no way to give it back: the release was gated on being signed in. Declaring a size and walking away cost the guest nothing and cost the owner their whole remaining allowance for two hours. The guest grant now carries the reservation and the declared size, bound to the Bunny video as well as to ours, so cancelling gives the quota back and costs them the upload it stood for. What a guest can hold without cancelling lapses in half an hour rather than two hours. The in-transaction fallback check counted the account's Bunny storage as zero on a Bunny upload, because the figure was only prefetched for R2 providers and that branch was unreachable for Bunny until this PR made it reachable. On an account whose storage is all Bunny that was a check that could not fail. It is prefetched for every provider that can reach the fallback now.
125 lines
4.0 KiB
TypeScript
125 lines
4.0 KiB
TypeScript
import {
|
|
VideoAssetKind,
|
|
VideoAssetProvider,
|
|
type UploadReservation,
|
|
type Video,
|
|
type VideoAsset,
|
|
type VideoVersion,
|
|
} from '@prisma/client';
|
|
import { db } from '@/lib/db';
|
|
import { UPLOAD_RESERVATION_PURPOSES, type UploadReservationPurpose } from '@/lib/storage-quota';
|
|
import { nextSeq } from './seq';
|
|
|
|
export interface CreateVideoInput {
|
|
projectId: string;
|
|
title?: string;
|
|
description?: string | null;
|
|
position?: number;
|
|
}
|
|
|
|
export async function createVideo(input: CreateVideoInput): Promise<Video> {
|
|
const seq = nextSeq();
|
|
return db.video.create({
|
|
data: {
|
|
projectId: input.projectId,
|
|
title: input.title ?? `Video ${seq}`,
|
|
description: input.description ?? null,
|
|
position: input.position ?? 0,
|
|
},
|
|
});
|
|
}
|
|
|
|
export interface CreateVersionInput {
|
|
/** The parent Video row. Maps to VideoVersion.videoParentId. */
|
|
videoParentId: string;
|
|
versionNumber?: number;
|
|
versionLabel?: string | null;
|
|
/** 'youtube' | 'r2' | 'bunny' | ... Maps to VideoVersion.providerId. */
|
|
providerId?: string;
|
|
/** The id the provider knows the media by. Maps to VideoVersion.videoId. */
|
|
providerVideoId?: string;
|
|
originalUrl?: string;
|
|
title?: string | null;
|
|
thumbnailUrl?: string | null;
|
|
duration?: number | null;
|
|
sizeBytes?: bigint;
|
|
isActive?: boolean;
|
|
}
|
|
|
|
export async function createVersion(input: CreateVersionInput): Promise<VideoVersion> {
|
|
const seq = nextSeq();
|
|
const providerVideoId = input.providerVideoId ?? `provider-video-${seq}`;
|
|
return db.videoVersion.create({
|
|
data: {
|
|
videoParentId: input.videoParentId,
|
|
versionNumber: input.versionNumber ?? 1,
|
|
versionLabel: input.versionLabel ?? null,
|
|
providerId: input.providerId ?? 'youtube',
|
|
videoId: providerVideoId,
|
|
originalUrl: input.originalUrl ?? `https://www.youtube.com/watch?v=${providerVideoId}`,
|
|
title: input.title ?? `Version ${seq}`,
|
|
thumbnailUrl: input.thumbnailUrl ?? null,
|
|
duration: input.duration ?? 120,
|
|
sizeBytes: input.sizeBytes ?? BigInt(0),
|
|
isActive: input.isActive ?? true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export interface CreateVideoAssetInput {
|
|
videoId: string;
|
|
billedUserId: string;
|
|
kind?: VideoAssetKind;
|
|
provider?: VideoAssetProvider;
|
|
displayName?: string;
|
|
sourceUrl?: string;
|
|
providerVideoId?: string | null;
|
|
thumbnailUrl?: string | null;
|
|
uploadedByUserId?: string | null;
|
|
uploadedByGuestIdentityId?: string | null;
|
|
uploadedByGuestName?: string | null;
|
|
sizeBytes?: bigint;
|
|
}
|
|
|
|
export async function createVideoAsset(input: CreateVideoAssetInput): Promise<VideoAsset> {
|
|
const seq = nextSeq();
|
|
return db.videoAsset.create({
|
|
data: {
|
|
videoId: input.videoId,
|
|
billedUserId: input.billedUserId,
|
|
kind: input.kind ?? VideoAssetKind.IMAGE,
|
|
provider: input.provider ?? VideoAssetProvider.R2_IMAGE,
|
|
displayName: input.displayName ?? `asset-${seq}.png`,
|
|
sourceUrl: input.sourceUrl ?? `/api/upload/image/asset-${seq}.png`,
|
|
providerVideoId: input.providerVideoId ?? null,
|
|
thumbnailUrl: input.thumbnailUrl ?? null,
|
|
uploadedByUserId: input.uploadedByUserId ?? null,
|
|
uploadedByGuestIdentityId: input.uploadedByGuestIdentityId ?? null,
|
|
uploadedByGuestName: input.uploadedByGuestName ?? null,
|
|
sizeBytes: input.sizeBytes ?? BigInt(0),
|
|
},
|
|
});
|
|
}
|
|
|
|
export interface CreateUploadReservationInput {
|
|
billedUserId: string;
|
|
sizeBytes: bigint;
|
|
/** Milliseconds from now. Negative values produce an already-expired row. */
|
|
expiresInMs?: number;
|
|
/** Which flow the hold belongs to. Only that flow can consume it. */
|
|
purpose?: UploadReservationPurpose;
|
|
}
|
|
|
|
export async function createUploadReservation(
|
|
input: CreateUploadReservationInput
|
|
): Promise<UploadReservation> {
|
|
return db.uploadReservation.create({
|
|
data: {
|
|
billedUserId: input.billedUserId,
|
|
sizeBytes: input.sizeBytes,
|
|
expiresAt: new Date(Date.now() + (input.expiresInMs ?? 30 * 60 * 1000)),
|
|
purpose: input.purpose ?? UPLOAD_RESERVATION_PURPOSES.R2_VIDEO,
|
|
},
|
|
});
|
|
}
|