( fileName: string, mimeType: string )
| 364 | } |
| 365 | |
| 366 | export function validateMediaFileType( |
| 367 | fileName: string, |
| 368 | mimeType: string |
| 369 | ): FileValidationError | null { |
| 370 | const raw = extractExtension(fileName) |
| 371 | const extension = isAlphanumericExtension(raw) ? raw : '' |
| 372 | |
| 373 | const isAudio = SUPPORTED_AUDIO_EXTENSIONS.includes(extension as SupportedAudioExtension) |
| 374 | const isVideo = SUPPORTED_VIDEO_EXTENSIONS.includes(extension as SupportedVideoExtension) |
| 375 | |
| 376 | if (!isAudio && !isVideo) { |
| 377 | return { |
| 378 | code: 'UNSUPPORTED_FILE_TYPE', |
| 379 | message: `Unsupported media file type${extension ? `: ${extension}` : ` for "${fileName}"`}. Supported audio types: ${SUPPORTED_AUDIO_EXTENSIONS.join(', ')}. Supported video types: ${SUPPORTED_VIDEO_EXTENSIONS.join(', ')}`, |
| 380 | supportedTypes: [...SUPPORTED_AUDIO_EXTENSIONS, ...SUPPORTED_VIDEO_EXTENSIONS], |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | const baseMimeType = mimeType.split(';')[0].trim() |
| 385 | const allowedMimeTypes = isAudio |
| 386 | ? SUPPORTED_AUDIO_MIME_TYPES[extension as SupportedAudioExtension] |
| 387 | : SUPPORTED_VIDEO_MIME_TYPES[extension as SupportedVideoExtension] |
| 388 | |
| 389 | if (!allowedMimeTypes.includes(baseMimeType)) { |
| 390 | return { |
| 391 | code: 'MIME_TYPE_MISMATCH', |
| 392 | message: `MIME type ${baseMimeType} does not match file extension ${extension}. Expected: ${allowedMimeTypes.join(', ')}`, |
| 393 | supportedTypes: allowedMimeTypes, |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return null |
| 398 | } |
nothing calls this directly
no test coverage detected