documentIcons.js
5.12 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
/**
* @description 文档图标工具函数
* @module utils/documentIcons
* @author Claude Code
* @created 2026-01-31
*/
// 导入所有 SVG 图标资源
import docIcon from '@/assets/images/icon/doc/doc.svg'
import pdfIcon from '@/assets/images/icon/doc/pdf.svg'
import wordIcon from '@/assets/images/icon/doc/word.svg'
import xlsIcon from '@/assets/images/icon/doc/xls.svg'
import pptIcon from '@/assets/images/icon/doc/ppt.svg'
import jpegIcon from '@/assets/images/icon/doc/jpeg.svg'
import pngIcon from '@/assets/images/icon/doc/png.svg'
import mp4Icon from '@/assets/images/icon/doc/mp4.svg'
import svgIcon from '@/assets/images/icon/doc/svg.svg'
import txtIcon from '@/assets/images/icon/doc/txt.svg'
import defaultIcon from '@/assets/images/icon/doc/其他文件.svg'
/**
* 文件扩展名到图标的映射
* @type {Object.<string, string>}
*/
const EXTENSION_ICON_MAP = {
// 文档类
'pdf': pdfIcon,
'doc': wordIcon,
'docx': wordIcon,
// 表格类
'xls': xlsIcon,
'xlsx': xlsIcon,
// 演示文稿类
'ppt': pptIcon,
'pptx': pptIcon,
// 图片类
'jpg': jpegIcon,
'jpeg': jpegIcon,
'png': pngIcon,
'gif': pngIcon,
'webp': pngIcon,
// 视频类
'mp4': mp4Icon,
'mov': mp4Icon,
'avi': mp4Icon,
'mkv': mp4Icon,
// 矢量图类
'svg': svgIcon,
// 文本类
'txt': txtIcon,
'md': txtIcon,
// 其他
'zip': defaultIcon,
'rar': defaultIcon,
'7z': defaultIcon,
};
/**
* 默认图标(未知文件类型)
* @type {string}
*/
const DEFAULT_ICON = defaultIcon;
/**
* 文件扩展名到显示标签的映射
* @type {Object.<string, string>}
*/
const EXTENSION_LABEL_MAP = {
'pdf': 'PDF',
'doc': 'Word',
'docx': 'Word',
'xls': 'Excel',
'xlsx': 'Excel',
'ppt': 'PPT',
'pptx': 'PPT',
'jpg': 'JPG',
'jpeg': 'JPEG',
'png': 'PNG',
'gif': 'GIF',
'webp': 'WebP',
'mp4': 'MP4',
'mov': 'MOV',
'avi': 'AVI',
'mkv': 'MKV',
'svg': 'SVG',
'txt': 'TXT',
'md': 'MD',
'zip': 'ZIP',
'rar': 'RAR',
'7z': '7Z',
};
/**
* 默认文件类型标签
* @type {string}
*/
const DEFAULT_LABEL = 'DOC';
/**
* 根据文件名获取文档图标路径
*
* @description 从文件名中提取扩展名,返回对应的图标路径
* @param {string} fileName - 文件名(如:document.pdf)
* @returns {string} 图标路径
*
* @example
* getDocumentIcon('报告.pdf') // 返回 PDF 图标
* getDocumentIcon('数据.xlsx') // 返回 Excel 图标
* getDocumentIcon('图片.png') // 返回 PNG 图标
*/
export function getDocumentIcon(fileName) {
if (!fileName || typeof fileName !== 'string') {
return DEFAULT_ICON;
}
// 提取文件扩展名
const lastDotIndex = fileName.lastIndexOf('.');
// 没有扩展名或以点结尾(如 "file.")
if (lastDotIndex === -1 || lastDotIndex === fileName.length - 1) {
return DEFAULT_ICON;
}
const extension = fileName.slice(lastDotIndex + 1).toLowerCase();
// 返回对应图标,找不到则返回默认图标
return EXTENSION_ICON_MAP[extension] || DEFAULT_ICON;
}
/**
* 根据文件名获取文件类型标签
*
* @description 从文件名中提取扩展名,返回对应的显示标签
* @param {string} fileName - 文件名(如:document.pdf)
* @returns {string} 文件类型标签(如:PDF、Word、Excel)
*
* @example
* getDocumentLabel('报告.pdf') // 'PDF'
* getDocumentLabel('数据.xlsx') // 'Excel'
* getDocumentLabel('图片.png') // 'PNG'
*/
export function getDocumentLabel(fileName) {
if (!fileName || typeof fileName !== 'string') {
return DEFAULT_LABEL;
}
// 提取文件扩展名
const lastDotIndex = fileName.lastIndexOf('.');
// 没有扩展名或以点结尾(如 "file.")
if (lastDotIndex === -1 || lastDotIndex === fileName.length - 1) {
return DEFAULT_LABEL;
}
const extension = fileName.slice(lastDotIndex + 1).toLowerCase();
// 返回对应标签,找不到则返回默认标签
return EXTENSION_LABEL_MAP[extension] || DEFAULT_LABEL;
}
/**
* 根据文件名判断是否为 PDF 文件
*
* @param {string} fileName - 文件名
* @returns {boolean} 是否为 PDF 文件
*
* @example
* isPDF('document.pdf') // true
* isPDF('document.docx') // false
*/
export function isPDF(fileName) {
const extension = fileName?.split('.').pop()?.toLowerCase();
return extension === 'pdf';
}
/**
* 根据文件名判断是否为图片文件
*
* @param {string} fileName - 文件名
* @returns {boolean} 是否为图片文件
*
* @example
* isImage('photo.jpg') // true
* isImage('document.pdf') // false
*/
export function isImage(fileName) {
const extension = fileName?.split('.').pop()?.toLowerCase();
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
return imageExtensions.includes(extension);
}
/**
* 根据文件名判断是否为视频文件
*
* @param {string} fileName - 文件名
* @returns {boolean} 是否为视频文件
*
* @example
* isVideo('movie.mp4') // true
* isVideo('document.pdf') // false
*/
export function isVideo(fileName) {
const extension = fileName?.split('.').pop()?.toLowerCase();
const videoExtensions = ['mp4', 'mov', 'avi', 'mkv'];
return videoExtensions.includes(extension);
}