1 | //
|
---|
2 | import { ExtendFile } from './types';
|
---|
3 |
|
---|
4 | // ----------------------------------------------------------------------
|
---|
5 |
|
---|
6 | // Define more types here
|
---|
7 | const FORMAT_PDF = ['pdf'];
|
---|
8 | const FORMAT_TEXT = ['txt'];
|
---|
9 | const FORMAT_PHOTOSHOP = ['psd'];
|
---|
10 | const FORMAT_WORD = ['doc', 'docx'];
|
---|
11 | const FORMAT_EXCEL = ['xls', 'xlsx'];
|
---|
12 | const FORMAT_ZIP = ['zip', 'rar', 'iso'];
|
---|
13 | const FORMAT_ILLUSTRATOR = ['ai', 'esp'];
|
---|
14 | const FORMAT_POWERPOINT = ['ppt', 'pptx'];
|
---|
15 | const FORMAT_AUDIO = ['wav', 'aif', 'mp3', 'aac'];
|
---|
16 | const FORMAT_IMG = ['jpg', 'jpeg', 'gif', 'bmp', 'png', 'svg'];
|
---|
17 | const FORMAT_VIDEO = ['m4v', 'avi', 'mpg', 'mp4', 'webm'];
|
---|
18 |
|
---|
19 | const iconUrl = (icon: string) => `/assets/icons/files/${icon}.svg`;
|
---|
20 |
|
---|
21 | // ----------------------------------------------------------------------
|
---|
22 |
|
---|
23 | export function fileFormat(fileUrl: string | undefined) {
|
---|
24 | let format;
|
---|
25 |
|
---|
26 | switch (fileUrl?.includes(fileTypeByUrl(fileUrl))) {
|
---|
27 | case FORMAT_TEXT.includes(fileTypeByUrl(fileUrl)):
|
---|
28 | format = 'txt';
|
---|
29 | break;
|
---|
30 | case FORMAT_ZIP.includes(fileTypeByUrl(fileUrl)):
|
---|
31 | format = 'zip';
|
---|
32 | break;
|
---|
33 | case FORMAT_AUDIO.includes(fileTypeByUrl(fileUrl)):
|
---|
34 | format = 'audio';
|
---|
35 | break;
|
---|
36 | case FORMAT_IMG.includes(fileTypeByUrl(fileUrl)):
|
---|
37 | format = 'image';
|
---|
38 | break;
|
---|
39 | case FORMAT_VIDEO.includes(fileTypeByUrl(fileUrl)):
|
---|
40 | format = 'video';
|
---|
41 | break;
|
---|
42 | case FORMAT_WORD.includes(fileTypeByUrl(fileUrl)):
|
---|
43 | format = 'word';
|
---|
44 | break;
|
---|
45 | case FORMAT_EXCEL.includes(fileTypeByUrl(fileUrl)):
|
---|
46 | format = 'excel';
|
---|
47 | break;
|
---|
48 | case FORMAT_POWERPOINT.includes(fileTypeByUrl(fileUrl)):
|
---|
49 | format = 'powerpoint';
|
---|
50 | break;
|
---|
51 | case FORMAT_PDF.includes(fileTypeByUrl(fileUrl)):
|
---|
52 | format = 'pdf';
|
---|
53 | break;
|
---|
54 | case FORMAT_PHOTOSHOP.includes(fileTypeByUrl(fileUrl)):
|
---|
55 | format = 'photoshop';
|
---|
56 | break;
|
---|
57 | case FORMAT_ILLUSTRATOR.includes(fileTypeByUrl(fileUrl)):
|
---|
58 | format = 'illustrator';
|
---|
59 | break;
|
---|
60 | default:
|
---|
61 | format = fileTypeByUrl(fileUrl);
|
---|
62 | }
|
---|
63 |
|
---|
64 | return format;
|
---|
65 | }
|
---|
66 |
|
---|
67 | // ----------------------------------------------------------------------
|
---|
68 |
|
---|
69 | export function fileThumb(fileUrl: string) {
|
---|
70 | let thumb;
|
---|
71 |
|
---|
72 | switch (fileFormat(fileUrl)) {
|
---|
73 | case 'folder':
|
---|
74 | thumb = iconUrl('ic_folder');
|
---|
75 | break;
|
---|
76 | case 'txt':
|
---|
77 | thumb = iconUrl('ic_txt');
|
---|
78 | break;
|
---|
79 | case 'zip':
|
---|
80 | thumb = iconUrl('ic_zip');
|
---|
81 | break;
|
---|
82 | case 'audio':
|
---|
83 | thumb = iconUrl('ic_audio');
|
---|
84 | break;
|
---|
85 | case 'video':
|
---|
86 | thumb = iconUrl('ic_video');
|
---|
87 | break;
|
---|
88 | case 'word':
|
---|
89 | thumb = iconUrl('ic_word');
|
---|
90 | break;
|
---|
91 | case 'excel':
|
---|
92 | thumb = iconUrl('ic_excel');
|
---|
93 | break;
|
---|
94 | case 'powerpoint':
|
---|
95 | thumb = iconUrl('ic_power_point');
|
---|
96 | break;
|
---|
97 | case 'pdf':
|
---|
98 | thumb = iconUrl('ic_pdf');
|
---|
99 | break;
|
---|
100 | case 'photoshop':
|
---|
101 | thumb = iconUrl('ic_pts');
|
---|
102 | break;
|
---|
103 | case 'illustrator':
|
---|
104 | thumb = iconUrl('ic_ai');
|
---|
105 | break;
|
---|
106 | case 'image':
|
---|
107 | thumb = iconUrl('ic_img');
|
---|
108 | break;
|
---|
109 | default:
|
---|
110 | thumb = iconUrl('ic_file');
|
---|
111 | }
|
---|
112 | return thumb;
|
---|
113 | }
|
---|
114 |
|
---|
115 | // ----------------------------------------------------------------------
|
---|
116 |
|
---|
117 | export function fileTypeByUrl(fileUrl = '') {
|
---|
118 | return (fileUrl && fileUrl.split('.').pop()) || '';
|
---|
119 | }
|
---|
120 |
|
---|
121 | // ----------------------------------------------------------------------
|
---|
122 |
|
---|
123 | export function fileNameByUrl(fileUrl: string) {
|
---|
124 | return fileUrl.split('/').pop();
|
---|
125 | }
|
---|
126 |
|
---|
127 | // ----------------------------------------------------------------------
|
---|
128 |
|
---|
129 | export function fileData(file: ExtendFile | string) {
|
---|
130 | // Url
|
---|
131 | if (typeof file === 'string') {
|
---|
132 | return {
|
---|
133 | key: file,
|
---|
134 | preview: file,
|
---|
135 | name: fileNameByUrl(file),
|
---|
136 | type: fileTypeByUrl(file),
|
---|
137 | };
|
---|
138 | }
|
---|
139 |
|
---|
140 | // File
|
---|
141 | return {
|
---|
142 | key: file.preview,
|
---|
143 | name: file.name,
|
---|
144 | size: file.size,
|
---|
145 | path: file.path,
|
---|
146 | type: file.type,
|
---|
147 | preview: file.preview,
|
---|
148 | lastModified: file.lastModified,
|
---|
149 | lastModifiedDate: file.lastModifiedDate,
|
---|
150 | };
|
---|
151 | }
|
---|