refactor(media): centralize input file limit resolution

This commit is contained in:
Peter Steinberger
2026-02-17 00:29:22 +00:00
parent ed74f48bd5
commit 37c97964af
3 changed files with 36 additions and 38 deletions

View File

@@ -64,6 +64,20 @@ export type InputFileLimits = {
pdf: InputPdfLimits;
};
export type InputFileLimitsConfig = {
allowUrl?: boolean;
allowedMimes?: string[];
maxBytes?: number;
maxChars?: number;
maxRedirects?: number;
timeoutMs?: number;
pdf?: {
maxPages?: number;
maxPixels?: number;
minTextChars?: number;
};
};
export type InputImageLimits = {
allowUrl: boolean;
urlAllowlist?: string[];
@@ -154,6 +168,22 @@ export function normalizeMimeList(values: string[] | undefined, fallback: string
return new Set(input.map((value) => normalizeMimeType(value)).filter(Boolean) as string[]);
}
export function resolveInputFileLimits(config?: InputFileLimitsConfig): InputFileLimits {
return {
allowUrl: config?.allowUrl ?? true,
allowedMimes: normalizeMimeList(config?.allowedMimes, DEFAULT_INPUT_FILE_MIMES),
maxBytes: config?.maxBytes ?? DEFAULT_INPUT_FILE_MAX_BYTES,
maxChars: config?.maxChars ?? DEFAULT_INPUT_FILE_MAX_CHARS,
maxRedirects: config?.maxRedirects ?? DEFAULT_INPUT_MAX_REDIRECTS,
timeoutMs: config?.timeoutMs ?? DEFAULT_INPUT_TIMEOUT_MS,
pdf: {
maxPages: config?.pdf?.maxPages ?? DEFAULT_INPUT_PDF_MAX_PAGES,
maxPixels: config?.pdf?.maxPixels ?? DEFAULT_INPUT_PDF_MAX_PIXELS,
minTextChars: config?.pdf?.minTextChars ?? DEFAULT_INPUT_PDF_MIN_TEXT_CHARS,
},
};
}
export async function fetchWithGuard(params: {
url: string;
maxBytes: number;