mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
A Bunny init asked the quota whether it could store zero bytes, which is a question with only one answer. Nothing an upload was about to consume was visible to the next request, so every init inside the same window read the same total and every one of them passed, and an upload that could never fit was only refused after it had been sent. The client now declares the size up front. It is checked against the account's remaining room before Bunny is asked for anything, and held as a reservation the next init has to see. The declaration is a claim rather than proof, so it is signed into the upload token: the same token already binds the video id, which is what makes the reservation safe to release on a caller's say-so, since releasing it costs them the video it belongs to. The declared size is then written onto the version or asset row and the reservation is dropped in the same transaction, because Bunny reports no size at all for a video until it has finished encoding it. On a half hour of footage that is most of an hour during which the upload did not appear on the uploader's own storage page and did not count against the next upload. Per-video accounting now takes the larger of what Bunny reports and what was declared, so the estimate stands in until the real figure arrives and Bunny's wins once it does. Two smaller things came out of the same reading. The asset route's in-transaction fallback compared against the plan limit, so a caller quoting a reservation that no longer existed was measured against 200 GiB even on a trial worth three. And the guest branch reserves without being able to release early, because a guest grant is bound to our video id and the caller's network context rather than to the Bunny video, which would let the reservation be dropped while the upload it stands for carried on.
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { parseDeclaredUploadSize } from '@/lib/upload-size';
|
|
|
|
const MAX = BigInt(5) * BigInt(1024) * BigInt(1024) * BigInt(1024);
|
|
|
|
function size(result: ReturnType<typeof parseDeclaredUploadSize>): bigint | null {
|
|
return 'sizeBytes' in result ? result.sizeBytes : null;
|
|
}
|
|
|
|
describe('parseDeclaredUploadSize', () => {
|
|
it('accepts a size sent as a string, which is how a client sends bytes it cannot hold in a number', () => {
|
|
expect(size(parseDeclaredUploadSize('4294967296', MAX))).toBe(BigInt(4294967296));
|
|
});
|
|
|
|
it('accepts a size sent as a number', () => {
|
|
expect(size(parseDeclaredUploadSize(1024, MAX))).toBe(BigInt(1024));
|
|
});
|
|
|
|
it('rejects a missing size', () => {
|
|
expect(parseDeclaredUploadSize(undefined, MAX)).toEqual({
|
|
error: 'sizeBytes must be a positive integer',
|
|
});
|
|
});
|
|
|
|
// Zero was the old behaviour of every Bunny init: it asked the quota whether
|
|
// it could store nothing, and the answer was always yes.
|
|
it('rejects zero', () => {
|
|
expect(parseDeclaredUploadSize(0, MAX)).toEqual({
|
|
error: 'sizeBytes must be a positive integer',
|
|
});
|
|
});
|
|
|
|
it('rejects a negative size', () => {
|
|
expect(parseDeclaredUploadSize(-1, MAX)).toEqual({
|
|
error: 'sizeBytes must be a positive integer',
|
|
});
|
|
});
|
|
|
|
it('rejects a fractional size rather than rounding it', () => {
|
|
expect(parseDeclaredUploadSize(1.5, MAX)).toEqual({
|
|
error: 'sizeBytes must be a positive integer',
|
|
});
|
|
});
|
|
|
|
it('rejects text that is not a number', () => {
|
|
expect(parseDeclaredUploadSize('a lot', MAX)).toEqual({
|
|
error: 'sizeBytes must be a positive integer',
|
|
});
|
|
});
|
|
|
|
it('rejects a size over the ceiling', () => {
|
|
expect(parseDeclaredUploadSize(MAX + BigInt(1), MAX)).toEqual({
|
|
error: 'File exceeds the maximum allowed upload size',
|
|
});
|
|
});
|
|
|
|
it('accepts a size exactly at the ceiling', () => {
|
|
expect(size(parseDeclaredUploadSize(MAX, MAX))).toBe(MAX);
|
|
});
|
|
});
|