Files
OpenFrame/lib/feature-flags.ts
T
yusufipk 4bf6e821af feat: enable S3 video uploads and update related configurations
- Added support for self-hosted S3 video uploads with new environment variables: OPENFRAME_ENABLE_S3_VIDEO_UPLOADS and OPENFRAME_MAX_VIDEO_UPLOAD_BYTES.
- Updated .env.example and .env.docker.example to reflect new configuration options.
- Enhanced Content Security Policy to include origins for S3-compatible storage.
- Updated dependencies for AWS SDK to support new features.
- Refactored upload logic to accommodate both Bunny and S3 upload providers.
- Updated documentation to clarify the usage of direct uploads and S3 configurations.
- Closes #11
2026-05-27 17:04:39 +02:00

100 lines
2.9 KiB
TypeScript

import { logError } from '@/lib/logger';
function readBooleanEnv(name: string, defaultValue: boolean): boolean {
const value = process.env[name];
if (!value) return defaultValue;
const normalized = value.trim().toLowerCase();
if (normalized === 'true') return true;
if (normalized === 'false') return false;
return defaultValue;
}
let warnedAboutConflictingUploadFlags = false;
function warnIfConflictingDirectUploadFlags(): void {
if (warnedAboutConflictingUploadFlags) return;
if (!isS3VideoUploadsFeatureEnabled() || !isBunnyUploadsFeatureEnabled()) return;
if (!hasR2Config() || !hasBunnyUploadsConfig()) return;
warnedAboutConflictingUploadFlags = true;
logError(
'OPENFRAME_ENABLE_S3_VIDEO_UPLOADS and OPENFRAME_ENABLE_BUNNY_UPLOADS are both enabled with valid config. S3 video uploads take precedence; disable Bunny uploads for self-hosted deployments.',
new Error('Conflicting direct upload feature flags')
);
}
export function isStripeFeatureEnabled() {
return readBooleanEnv('OPENFRAME_ENABLE_STRIPE', true);
}
export function hasStripeConfig() {
return Boolean(process.env.STRIPE_SECRET_KEY && process.env.STRIPE_PRICE_ID);
}
export function isStripeBillingEnabled() {
return isStripeFeatureEnabled() && hasStripeConfig();
}
export function isBunnyUploadsFeatureEnabled() {
return readBooleanEnv('OPENFRAME_ENABLE_BUNNY_UPLOADS', true);
}
export function hasBunnyUploadsConfig() {
return Boolean(
process.env.BUNNY_STREAM_API_KEY &&
(process.env.BUNNY_STREAM_LIBRARY_ID || process.env.NEXT_PUBLIC_BUNNY_STREAM_LIBRARY_ID)
);
}
export function isS3VideoUploadsFeatureEnabled() {
return readBooleanEnv('OPENFRAME_ENABLE_S3_VIDEO_UPLOADS', false);
}
export function hasR2Config() {
return Boolean(
process.env.R2_ACCESS_KEY_ID &&
process.env.R2_SECRET_ACCESS_KEY &&
process.env.R2_BUCKET_NAME &&
(process.env.R2_ENDPOINT || process.env.R2_ACCOUNT_ID)
);
}
export function isS3VideoUploadsEnabled() {
warnIfConflictingDirectUploadFlags();
return isS3VideoUploadsFeatureEnabled() && hasR2Config();
}
export function isBunnyUploadsEnabled() {
if (isS3VideoUploadsEnabled()) {
return false;
}
return isBunnyUploadsFeatureEnabled() && hasBunnyUploadsConfig();
}
export function isDirectFileUploadEnabled() {
return isS3VideoUploadsEnabled() || isBunnyUploadsEnabled();
}
export function getMaxVideoUploadBytes(): bigint {
const raw = process.env.OPENFRAME_MAX_VIDEO_UPLOAD_BYTES?.trim();
if (!raw) {
return BigInt(5) * BigInt(1024) * BigInt(1024) * BigInt(1024);
}
try {
const parsed = BigInt(raw);
if (parsed <= BigInt(0)) {
return BigInt(5) * BigInt(1024) * BigInt(1024) * BigInt(1024);
}
return parsed;
} catch {
return BigInt(5) * BigInt(1024) * BigInt(1024) * BigInt(1024);
}
}
export function isInviteCodeRequired() {
return readBooleanEnv('OPENFRAME_REQUIRE_INVITE_CODE', true);
}