File size: 1,673 Bytes
057576a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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];
};