(
newFiles: File[],
existingCount: number,
)
| 50 | } |
| 51 | |
| 52 | function validateFiles( |
| 53 | newFiles: File[], |
| 54 | existingCount: number, |
| 55 | ): ValidationResult { |
| 56 | const errors: string[] = [] |
| 57 | const validFiles: File[] = [] |
| 58 | |
| 59 | const availableSlots = MAX_FILES - existingCount |
| 60 | |
| 61 | if (availableSlots <= 0) { |
| 62 | errors.push(`Maximum ${MAX_FILES} files allowed`) |
| 63 | return { validFiles, errors } |
| 64 | } |
| 65 | |
| 66 | for (const file of newFiles) { |
| 67 | if (validFiles.length >= availableSlots) { |
| 68 | errors.push(`Only ${availableSlots} more file(s) allowed`) |
| 69 | break |
| 70 | } |
| 71 | if (!isValidFileType(file)) { |
| 72 | errors.push(`"${file.name}" is not a supported file type`) |
| 73 | continue |
| 74 | } |
| 75 | // Only check size for images (PDFs/text files are extracted client-side, so file size doesn't matter) |
| 76 | const isExtractedFile = isPdfFile(file) || isTextFile(file) |
| 77 | if (!isExtractedFile && file.size > MAX_IMAGE_SIZE) { |
| 78 | const maxSizeMB = MAX_IMAGE_SIZE / 1024 / 1024 |
| 79 | errors.push( |
| 80 | `"${file.name}" is ${formatFileSize(file.size)} (exceeds ${maxSizeMB}MB)`, |
| 81 | ) |
| 82 | } else { |
| 83 | validFiles.push(file) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return { validFiles, errors } |
| 88 | } |
| 89 | |
| 90 | function showValidationErrors(errors: string[]) { |
| 91 | if (errors.length === 0) return |
no test coverage detected