(fileName: string, mimeType: string)
| 254 | * Validate if a file type is supported for document processing |
| 255 | */ |
| 256 | export function validateFileType(fileName: string, mimeType: string): FileValidationError | null { |
| 257 | const raw = extractExtension(fileName) |
| 258 | const extension = (isAlphanumericExtension(raw) ? raw : '') as SupportedDocumentExtension |
| 259 | |
| 260 | if (!SUPPORTED_DOCUMENT_EXTENSIONS.includes(extension)) { |
| 261 | return { |
| 262 | code: 'UNSUPPORTED_FILE_TYPE', |
| 263 | message: `Unsupported file type${extension ? `: ${extension}` : ` for "${fileName}"`}. Supported types are: ${SUPPORTED_DOCUMENT_EXTENSIONS.join(', ')}`, |
| 264 | supportedTypes: [...SUPPORTED_DOCUMENT_EXTENSIONS], |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | const baseMimeType = mimeType.split(';')[0].trim() |
| 269 | |
| 270 | // Allow empty MIME types if the extension is supported (browsers often don't recognize certain file types) |
| 271 | if (!baseMimeType) { |
| 272 | return null |
| 273 | } |
| 274 | |
| 275 | const allowedMimeTypes = SUPPORTED_MIME_TYPES[extension] |
| 276 | if (!allowedMimeTypes.includes(baseMimeType)) { |
| 277 | return { |
| 278 | code: 'MIME_TYPE_MISMATCH', |
| 279 | message: `MIME type ${baseMimeType} does not match file extension ${extension}. Expected: ${allowedMimeTypes.join(', ')}`, |
| 280 | supportedTypes: allowedMimeTypes, |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return null |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Check if file extension is supported |
no test coverage detected