import { BadRequestException } from '@nestjs/common'; // oxlint-disable-next-line no-control-regex const FORBIDDEN = /[/\\\u0000-\u001f\u007f-\u009f\u200e\u200f\u202a-\u202e\u2066-\u2069]/u; /** The name is stored as-is and used for display only, never in paths or keys. */ export function validateFilename(value: unknown): string { if (typeof value !== 'string') throw invalid(); const length = [...value].length; if ( length < 1 || length > 255 || /^\s*$/u.test(value) || value === '.' || value === '..' || FORBIDDEN.test(value) ) throw invalid(); return value; } function invalid(): BadRequestException { return new BadRequestException('filename is invalid'); } export function contentDisposition(filename: string): string { const fallback = filename.replace(/[^\x20-\x7e]|["\\]/gu, '_'); const encoded = encodeURIComponent(filename).replace( /['()*]/gu, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`, ); return `inline; filename="${fallback}"; filename*=UTF-8''${encoded}`; }