Handling fs
This commit is contained in:
+12
-3
@@ -6,6 +6,7 @@ export interface Config {
|
||||
storageDir: string;
|
||||
maxFileSize: number;
|
||||
fileTtlMs: number;
|
||||
maxStorageUsage: number;
|
||||
publicBaseUrl?: string;
|
||||
rateLimitEnabled: boolean;
|
||||
rateLimitMax: number;
|
||||
@@ -13,7 +14,8 @@ export interface Config {
|
||||
trustProxy: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 1 week
|
||||
const MAX_TTL_MS = 48 * 60 * 60 * 1000; // 48 hours hard maximum
|
||||
const DEFAULT_TTL_MS = MAX_TTL_MS;
|
||||
|
||||
const SIZE_UNITS: Record<string, number> = {
|
||||
B: 1,
|
||||
@@ -56,10 +58,16 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
|
||||
const maxFileSize = parseSize(env.MAX_FILE_SIZE ?? "10GB");
|
||||
if (maxFileSize < 1024) throw new Error("MAX_FILE_SIZE must be at least 1KB");
|
||||
|
||||
const fileTtlMs = Number(env.FILE_TTL_MS ?? String(DEFAULT_TTL_MS));
|
||||
if (!Number.isInteger(fileTtlMs) || fileTtlMs < 1) {
|
||||
const fileTtlMsRaw = Number(env.FILE_TTL_MS ?? String(DEFAULT_TTL_MS));
|
||||
if (!Number.isInteger(fileTtlMsRaw) || fileTtlMsRaw < 1) {
|
||||
throw new Error(`Invalid FILE_TTL_MS: ${env.FILE_TTL_MS}`);
|
||||
}
|
||||
const fileTtlMs = Math.min(fileTtlMsRaw, MAX_TTL_MS);
|
||||
|
||||
const maxStorageUsage = Number(env.MAX_STORAGE_USAGE ?? "0.5");
|
||||
if (!Number.isFinite(maxStorageUsage) || maxStorageUsage < 0 || maxStorageUsage > 1) {
|
||||
throw new Error(`Invalid MAX_STORAGE_USAGE: ${env.MAX_STORAGE_USAGE}`);
|
||||
}
|
||||
|
||||
const rateLimitMax = Number(env.RATE_LIMIT_MAX ?? "300");
|
||||
if (!Number.isInteger(rateLimitMax) || rateLimitMax < 1) {
|
||||
@@ -88,6 +96,7 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
|
||||
storageDir,
|
||||
maxFileSize,
|
||||
fileTtlMs,
|
||||
maxStorageUsage,
|
||||
publicBaseUrl,
|
||||
rateLimitEnabled: boolFromEnv(env.RATE_LIMIT_ENABLED, true),
|
||||
rateLimitMax,
|
||||
|
||||
Reference in New Issue
Block a user