mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
feat(uploads): size one upload against the account's own quota
The per-file ceiling was a flat 5 GiB from the environment, which is both too small for a paying account with 200 GB of storage and unaware of what an upload actually costs. The provider derives its own renditions from the file (1080p, 720p and down) and bills them to the same account, so a file allowed to fill the quota exactly is over it by the time it finishes processing. The ceiling is now 80% of whatever limit the account is held to: 160 GB on the plan, 2.4 GB on a cardless trial, and it moves on its own when either number changes. OPENFRAME_MAX_VIDEO_UPLOAD_BYTES keeps working as an absolute cap for a host that wants one, where the lower of the two applies, and an instance running without billing has no quota to divide and falls back to the flat 5 GiB. The refusal now names the ceiling, which the old one left the client to guess. Finalize re-checks only the host cap. Re-deriving the account's ceiling there would delete a finished upload over a plan that lapsed while the bytes were in flight, and an upload larger than what was declared is already caught by the declared-size check beside it.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
getMaxVideoUploadBytes,
|
||||
getConfiguredMaxVideoUploadBytes,
|
||||
getR2MultipartPartSizeBytes,
|
||||
getR2MultipartThresholdBytes,
|
||||
hasBunnyUploadsConfig,
|
||||
@@ -264,26 +264,26 @@ describe('direct upload precedence', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getMaxVideoUploadBytes', () => {
|
||||
it('defaults to 5 GiB', () => {
|
||||
expect(getMaxVideoUploadBytes()).toBe(BigInt(5) * GIB);
|
||||
describe('getConfiguredMaxVideoUploadBytes', () => {
|
||||
it('is null when unset, leaving the ceiling to the account quota', () => {
|
||||
expect(getConfiguredMaxVideoUploadBytes()).toBeNull();
|
||||
});
|
||||
|
||||
it('uses a valid explicit byte count', () => {
|
||||
vi.stubEnv('OPENFRAME_MAX_VIDEO_UPLOAD_BYTES', '1073741824');
|
||||
expect(getMaxVideoUploadBytes()).toBe(GIB);
|
||||
expect(getConfiguredMaxVideoUploadBytes()).toBe(GIB);
|
||||
});
|
||||
|
||||
it('trims surrounding whitespace before parsing', () => {
|
||||
vi.stubEnv('OPENFRAME_MAX_VIDEO_UPLOAD_BYTES', ' 1073741824 ');
|
||||
expect(getMaxVideoUploadBytes()).toBe(GIB);
|
||||
expect(getConfiguredMaxVideoUploadBytes()).toBe(GIB);
|
||||
});
|
||||
|
||||
it.each(['0', '-1', '-1073741824', 'abc', '1.5', '1e9', '1_000', ' '])(
|
||||
'falls back to 5 GiB for the invalid value %s',
|
||||
'reads the invalid value %s as no ceiling rather than as a number',
|
||||
(raw) => {
|
||||
vi.stubEnv('OPENFRAME_MAX_VIDEO_UPLOAD_BYTES', raw);
|
||||
expect(getMaxVideoUploadBytes()).toBe(BigInt(5) * GIB);
|
||||
expect(getConfiguredMaxVideoUploadBytes()).toBeNull();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parseDeclaredUploadSize } from '@/lib/upload-size';
|
||||
import { formatSizeLimit, parseDeclaredUploadSize } from '@/lib/upload-size';
|
||||
|
||||
const MAX = BigInt(5) * BigInt(1024) * BigInt(1024) * BigInt(1024);
|
||||
|
||||
@@ -48,9 +48,9 @@ describe('parseDeclaredUploadSize', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects a size over the ceiling', () => {
|
||||
it('rejects a size over the ceiling, naming the ceiling so the client knows what fits', () => {
|
||||
expect(parseDeclaredUploadSize(MAX + BigInt(1), MAX)).toEqual({
|
||||
error: 'File exceeds the maximum allowed upload size',
|
||||
error: 'File exceeds the maximum allowed upload size (5 GB)',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -58,3 +58,19 @@ describe('parseDeclaredUploadSize', () => {
|
||||
expect(size(parseDeclaredUploadSize(MAX, MAX))).toBe(MAX);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatSizeLimit', () => {
|
||||
it('drops the decimal on a whole number of gigabytes', () => {
|
||||
expect(formatSizeLimit(BigInt(160) * BigInt(1024) ** BigInt(3))).toBe('160 GB');
|
||||
});
|
||||
|
||||
it('keeps one decimal where the share of a small quota is not whole', () => {
|
||||
expect(
|
||||
formatSizeLimit((BigInt(3) * BigInt(1024) ** BigInt(3) * BigInt(80)) / BigInt(100))
|
||||
).toBe('2.4 GB');
|
||||
});
|
||||
|
||||
it('falls back to megabytes below a gigabyte, where "0.0 GB" would say nothing', () => {
|
||||
expect(formatSizeLimit(BigInt(500) * BigInt(1024) * BigInt(1024))).toBe('500 MB');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user