text-dataset-tiny-code-script-py-format / ysnrfd_messenger /BACKUP2 /frontend /src /utils /fileValidation.js
| export const ALLOWED_FILE_TYPES = { | |
| image: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'], | |
| audio: ['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/mp4', 'audio/x-m4a'], | |
| document: ['application/pdf', 'text/plain', 'text/markdown'] | |
| }; | |
| export const MAX_FILE_SIZES = { | |
| image: 10 * 1024 * 1024, // 10MB | |
| audio: 25 * 1024 * 1024, // 25MB | |
| document: 50 * 1024 * 1024 // 50MB | |
| }; | |
| export const validateFile = (file) => { | |
| // Check file type | |
| let fileCategory = null; | |
| for (const [category, types] of Object.entries(ALLOWED_FILE_TYPES)) { | |
| if (types.includes(file.type)) { | |
| fileCategory = category; | |
| break; | |
| } | |
| } | |
| if (!fileCategory) { | |
| throw new Error(`File type ${file.type} is not supported. Supported types: images, audio, PDF, and text files.`); | |
| } | |
| // Check file size | |
| if (file.size > MAX_FILE_SIZES[fileCategory]) { | |
| const maxSizeMB = MAX_FILE_SIZES[fileCategory] / 1024 / 1024; | |
| throw new Error(`File too large. Maximum size for ${fileCategory} files is ${maxSizeMB}MB.`); | |
| } | |
| return fileCategory; | |
| }; | |
| export const getFileIcon = (mimeType) => { | |
| if (mimeType.startsWith('image/')) return 'image'; | |
| if (mimeType.startsWith('audio/')) return 'audio'; | |
| if (mimeType === 'application/pdf') return 'pdf'; | |
| if (mimeType.startsWith('text/')) return 'text'; | |
| return 'file'; | |
| }; | |
| export const formatFileSize = (bytes) => { | |
| if (bytes === 0) return '0 Bytes'; | |
| const k = 1024; | |
| const sizes = ['Bytes', 'KB', 'MB', 'GB']; | |
| const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
| return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | |
| }; |