-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathfileTypeUtils.js
More file actions
216 lines (193 loc) · 5.48 KB
/
fileTypeUtils.js
File metadata and controls
216 lines (193 loc) · 5.48 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* 文件类型识别工具函数
* 根据文件名返回对应的 iconfont 图标类名
*/
/**
* 根据文件名获取对应的文件类型图标类名
* @param {string} fileName - 文件名
* @returns {string} 对应的 iconfont 图标类名
*/
export function getFileTypeIcon(fileName) {
if (!fileName || typeof fileName !== 'string') {
return 'icon-file'
}
// 获取文件扩展名(转为小写)
const extension = fileName.toLowerCase().split('.').pop()
// 文档类型
const documentTypes = {
'doc': 'icon-file_word',
'docx': 'icon-file_word',
'xls': 'icon-file_excel',
'xlsx': 'icon-file_excel',
'ppt': 'icon-file_ppt',
'pptx': 'icon-file_ppt',
'pdf': 'icon-file_pdf',
'txt': 'icon-file_txt',
'rtf': 'icon-file_txt',
'md': 'icon-file_txt'
}
// 图片类型
const imageTypes = {
'jpg': 'icon-file_img',
'jpeg': 'icon-file_img',
'png': 'icon-file_img',
'gif': 'icon-file_img',
'bmp': 'icon-file_img',
'svg': 'icon-file_img',
'webp': 'icon-file_img',
'ico': 'icon-file_img'
}
// 视频类型
const videoTypes = {
'mp4': 'icon-file_video',
'avi': 'icon-file_video',
'mkv': 'icon-file_video',
'mov': 'icon-file_video',
'wmv': 'icon-file_video',
'flv': 'icon-file_video',
'webm': 'icon-file_video',
'm4v': 'icon-file_video',
'rmvb': 'icon-file_video',
'rm': 'icon-file_video'
}
// 音频类型
const audioTypes = {
'mp3': 'icon-file_music',
'wav': 'icon-file_music',
'flac': 'icon-file_music',
'aac': 'icon-file_music',
'ogg': 'icon-file_music',
'wma': 'icon-file_music',
'm4a': 'icon-file_music'
}
// 压缩包类型
const archiveTypes = {
'zip': 'icon-file_zip',
'rar': 'icon-file_zip',
'7z': 'icon-file_zip',
'tar': 'icon-file_zip',
'gz': 'icon-file_zip',
'bz2': 'icon-file_zip'
}
// 可执行文件类型
const executableTypes = {
'exe': 'icon-file_exe',
'msi': 'icon-file_exe',
'dmg': 'icon-file_exe',
'pkg': 'icon-file_exe',
'deb': 'icon-file_exe',
'rpm': 'icon-file_exe'
}
// 代码文件类型
const codeTypes = {
'html': 'icon-file_html',
'htm': 'icon-file_html',
'css': 'icon-file_code',
'js': 'icon-file_code',
'ts': 'icon-file_code',
'vue': 'icon-file_code',
'jsx': 'icon-file_code',
'tsx': 'icon-file_code',
'php': 'icon-file_code',
'py': 'icon-file_code',
'java': 'icon-file_code',
'cpp': 'icon-file_code',
'c': 'icon-file_code',
'cs': 'icon-file_code',
'go': 'icon-file_code',
'rs': 'icon-file_code',
'swift': 'icon-file_code',
'kt': 'icon-file_code',
'rb': 'icon-file_code',
'json': 'icon-file_code',
'xml': 'icon-file_code',
'yaml': 'icon-file_code',
'yml': 'icon-file_code'
}
// 设计文件类型
const designTypes = {
'ai': 'icon-file_ai',
'psd': 'icon-file_psd',
'sketch': 'icon-file_psd',
'fig': 'icon-file_psd',
'xd': 'icon-file_psd'
}
// CAD文件类型
const cadTypes = {
'dwg': 'icon-file_cad',
'dxf': 'icon-file_cad',
'step': 'icon-file_cad',
'iges': 'icon-file_cad'
}
// Flash文件类型
const flashTypes = {
'swf': 'icon-file_flash',
'fla': 'icon-file_flash'
}
// ISO文件类型
const isoTypes = {
'iso': 'icon-file_iso',
'img': 'icon-file_iso',
'bin': 'icon-file_iso'
}
// BT种子文件
const btTypes = {
'torrent': 'icon-file_bt'
}
// 云文件类型(一些云存储相关的文件)
const cloudTypes = {
'cloud': 'icon-file_cloud',
'gdoc': 'icon-file_cloud',
'gsheet': 'icon-file_cloud',
'gslides': 'icon-file_cloud'
}
// 按优先级检查文件类型
if (documentTypes[extension]) return documentTypes[extension]
if (imageTypes[extension]) return imageTypes[extension]
if (videoTypes[extension]) return videoTypes[extension]
if (audioTypes[extension]) return audioTypes[extension]
if (archiveTypes[extension]) return archiveTypes[extension]
if (executableTypes[extension]) return executableTypes[extension]
if (codeTypes[extension]) return codeTypes[extension]
if (designTypes[extension]) return designTypes[extension]
if (cadTypes[extension]) return cadTypes[extension]
if (flashTypes[extension]) return flashTypes[extension]
if (isoTypes[extension]) return isoTypes[extension]
if (btTypes[extension]) return btTypes[extension]
if (cloudTypes[extension]) return cloudTypes[extension]
// 默认返回通用文件图标
return 'icon-file'
}
/**
* 检查项目是否为文件夹类型
* @param {Object} item - 项目对象
* @returns {boolean} 是否为文件夹
*/
export function isFolder(item) {
return item && item.vod_tag && item.vod_tag.includes('folder')
}
/**
* 检查项目是否为目录模式下的文件类型
* @param {Object} item - 项目对象
* @returns {boolean} 是否为目录模式下的文件
*/
export function isDirectoryFile(item) {
return item && item.vod_tag && !item.vod_tag.includes('folder')
}
/**
* 获取项目的显示图标类型
* @param {Object} item - 项目对象
* @returns {Object} 图标信息 { type: 'folder'|'file'|'image', iconClass?: string }
*/
export function getItemIconInfo(item) {
if (!item) {
return { type: 'image' }
}
if (isFolder(item)) {
return { type: 'folder', iconClass: 'icon-wenjianjia' }
}
if (isDirectoryFile(item)) {
return { type: 'file', iconClass: getFileTypeIcon(item.vod_name) }
}
return { type: 'image' }
}