Skip to content

Commit

Permalink
feat(FilePreview): add recognition of most types of files uploaded vi…
Browse files Browse the repository at this point in the history
…a input
  • Loading branch information
mournfulCoroner committed Jan 23, 2025
1 parent 3f580e1 commit 2d56720
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/components/FilePreview/FilePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const FILE_ICON: Record<FileType, IconData> = {
video: VideoIcon,
code: CodeIcon,
archive: ArchiveIcon,
audio: MusicIcon,
music: MusicIcon,
text: TextIcon,
pdf: PdfIcon,
Expand Down
1 change: 1 addition & 0 deletions src/components/FilePreview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const FILE_TYPES = [
'code',
'archive',
'music',
'audio',
'text',
'pdf',
'table',
Expand Down
53 changes: 52 additions & 1 deletion src/components/FilePreview/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,57 @@ import {FILE_TYPES} from './types';
const isFilePreviewFileType = (str: string): str is FileType =>
FILE_TYPES.includes(str.toLowerCase() as FileType);

const APPLICATION_MIME_TO_TYPE: Record<string, FileType> = {
'application/x-compressed': 'archive',
'application/x-troff-msvideo': 'video',
'application/macbinary': 'code',
'application/mac-binary': 'code',
'application/x-binary': 'code',
'application/x-macbinary': 'code',
'application/bmp': 'image',
'application/x-bmp': 'image',
'application/x-win-bitmap': 'image',
'application/vnd.msexcel': 'image',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'text',
'application/x-msdownload': 'code',
'application/x-gzip': 'archive',
'application/java-archive': 'code',
'application/x-java-application': 'code',
'application/x-jar': 'code',
'application/x-javascript': 'code',
'application/json': 'code',
'application/ogg': 'audio',
'application/pdf': 'pdf',
'application/octet-stream': 'pdf',
'application/x-httpd-php': 'code',
'application/php': 'code',
'application/x-php': 'code',
'application/x-httpd-php-source': 'code',
'application/msword': 'text',
'application/x-rar': 'archive',
'application/rar': 'archive',
'application/x-rar-compressed': 'archive',
'application/x-tar': 'archive',
'application/x-gzip-compressed': 'archive',
'application/xhtml+xml': 'code',
'application/excel': 'table',
'application/msexcel': 'table',
'application/x-msexcel': 'table',
'application/x-ms-excel': 'table',
'application/x-excel': 'table',
'application/x-dos_ms_excel': 'table',
'application/xls': 'table',
'application/x-xls': 'table',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'table',
'application/vnd.ms-excel': 'table',
'application/xml': 'code',
'application/x-zip': 'archive',
'application/zip': 'archive',
'application/x-zip-compressed': 'archive',
'application/s-compressed': 'archive',
'multipart/x-zip': 'archive',
};

export function getFileType(fileType: File['type']): FileType;
export function getFileType(file: File): FileType;

Expand All @@ -20,5 +71,5 @@ export function getFileType(arg: File | File['type']): FileType {
return splittedFileType;
}

return 'default';
return APPLICATION_MIME_TO_TYPE[fileType] || 'default';
}

0 comments on commit 2d56720

Please sign in to comment.