File size: 1,088 Bytes
d988ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Utility functions for file handling
 */

/**
 * Check if a file can be previewed based on its MIME type
 */
export function isPreviewable(mimetype: string): boolean {
  // Image files
  if (mimetype.startsWith('image/')) {
    return true;
  }
  
  // PDF files
  if (mimetype === 'application/pdf') {
    return true;
  }
  
  // Text files
  if (
    mimetype.startsWith('text/') || 
    mimetype === 'application/json' ||
    mimetype === 'application/javascript' ||
    mimetype === 'application/typescript' ||
    mimetype === 'application/xml'
  ) {
    return true;
  }
  
  // Video files
  if (mimetype.startsWith('video/')) {
    return true;
  }
  
  // Audio files
  if (mimetype.startsWith('audio/')) {
    return true;
  }
  
  // All other file types are not previewable
  return false;
}

/**
 * Format file size in a human-readable format
 */
export function formatFileSize(bytes: number): string {
  if (bytes < 1024) return bytes + ' B';
  else if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' KB';
  else return (bytes / 1048576).toFixed(1) + ' MB';
}