forked from spencerwooo/onedrive-vercel-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFileIcon.ts
128 lines (109 loc) · 2.88 KB
/
getFileIcon.ts
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
import type { IconPrefix, IconName } from '@fortawesome/fontawesome-common-types'
const icons: { [key: string]: [IconPrefix, IconName] } = {
image: ['far', 'file-image'],
pdf: ['far', 'file-pdf'],
word: ['far', 'file-word'],
powerpoint: ['far', 'file-powerpoint'],
excel: ['far', 'file-excel'],
audio: ['far', 'file-audio'],
video: ['far', 'file-video'],
archive: ['far', 'file-archive'],
code: ['far', 'file-code'],
text: ['far', 'file-alt'],
file: ['far', 'file'],
markdown: ['fab', 'markdown'],
book: ['fas', 'book'],
link: ['fas', 'link'],
}
const extensions = {
gif: icons.image,
jpeg: icons.image,
jpg: icons.image,
png: icons.image,
heic: icons.image,
webp: icons.image,
pdf: icons.pdf,
doc: icons.word,
docx: icons.word,
ppt: icons.powerpoint,
pptx: icons.powerpoint,
xls: icons.excel,
xlsx: icons.excel,
aac: icons.audio,
mp3: icons.audio,
ogg: icons.audio,
flac: icons.audio,
oga: icons.audio,
opus: icons.audio,
m4a: icons.audio,
avi: icons.video,
flv: icons.video,
mkv: icons.video,
mp4: icons.video,
'7z': icons.archive,
bz2: icons.archive,
xz: icons.archive,
wim: icons.archive,
gz: icons.archive,
rar: icons.archive,
tar: icons.archive,
zip: icons.archive,
c: icons.code,
cpp: icons.code,
js: icons.code,
jsx: icons.code,
java: icons.code,
sh: icons.code,
cs: icons.code,
py: icons.code,
css: icons.code,
html: icons.code,
ts: icons.code,
tsx: icons.code,
rs: icons.code,
vue: icons.code,
json: icons.code,
yml: icons.code,
yaml: icons.code,
toml: icons.code,
txt: icons.text,
rtf: icons.text,
vtt: icons.text,
srt: icons.text,
log: icons.text,
diff: icons.text,
md: icons.markdown,
epub: icons.book,
mobi: icons.book,
azw3: icons.book,
url: icons.link,
}
/**
* To stop TypeScript complaining about indexing the object with a non-existent key
* https://dev.to/mapleleaf/indexing-objects-in-typescript-1cgi
*
* @param obj Object with keys to index
* @param key The index key
* @returns Whether or not the key exists inside the object
*/
export function hasKey<O>(obj: O, key: PropertyKey): key is keyof O {
return key in obj
}
export function getRawExtension(fileName: string): string {
return fileName.slice(((fileName.lastIndexOf('.') - 1) >>> 0) + 2)
}
export function getExtension(fileName: string): string {
return getRawExtension(fileName).toLowerCase()
}
export function getFileIcon(fileName: string, flags?: { video?: boolean }): [IconPrefix, IconName] {
const extension = getExtension(fileName)
let icon = hasKey(extensions, extension) ? extensions[extension] : icons.file
// Files with '.ts' extensions may be TypeScript files or TS Video files, we check for the flag 'video'
// to determine which icon to render for '.ts' files.
if (extension === 'ts') {
if (flags?.video) {
icon = icons.video
}
}
return icon
}