2022-05-10 03:11:36 +00:00
|
|
|
import type { IconPrefix, IconName } from '@fortawesome/fontawesome-svg-core'
|
2021-06-23 22:51:23 +00:00
|
|
|
|
|
|
|
const icons: { [key: string]: [IconPrefix, IconName] } = {
|
2021-06-23 20:27:51 +00:00
|
|
|
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'],
|
2021-07-24 08:31:46 +00:00
|
|
|
book: ['fas', 'book'],
|
2022-01-08 07:10:36 +00:00
|
|
|
link: ['fas', 'link'],
|
2021-06-23 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-06-24 13:54:59 +00:00
|
|
|
flac: icons.audio,
|
|
|
|
oga: icons.audio,
|
|
|
|
opus: icons.audio,
|
2021-08-22 04:16:05 +00:00
|
|
|
m4a: icons.audio,
|
2021-06-23 20:27:51 +00:00
|
|
|
|
|
|
|
avi: icons.video,
|
|
|
|
flv: icons.video,
|
|
|
|
mkv: icons.video,
|
|
|
|
mp4: icons.video,
|
|
|
|
|
2021-06-24 13:54:59 +00:00
|
|
|
'7z': icons.archive,
|
|
|
|
bz2: icons.archive,
|
|
|
|
xz: icons.archive,
|
|
|
|
wim: icons.archive,
|
2021-06-23 20:27:51 +00:00
|
|
|
gz: icons.archive,
|
|
|
|
rar: icons.archive,
|
2021-06-24 13:54:59 +00:00
|
|
|
tar: icons.archive,
|
2021-06-23 20:27:51 +00:00
|
|
|
zip: icons.archive,
|
|
|
|
|
2022-02-10 14:00:40 +00:00
|
|
|
c: icons.code,
|
|
|
|
cpp: icons.code,
|
|
|
|
js: icons.code,
|
|
|
|
jsx: icons.code,
|
|
|
|
java: icons.code,
|
|
|
|
sh: icons.code,
|
|
|
|
cs: icons.code,
|
2021-06-23 20:27:51 +00:00
|
|
|
py: icons.code,
|
2022-02-10 14:00:40 +00:00
|
|
|
css: icons.code,
|
2021-06-23 20:27:51 +00:00
|
|
|
html: icons.code,
|
|
|
|
ts: icons.code,
|
2022-02-10 14:00:40 +00:00
|
|
|
tsx: icons.code,
|
|
|
|
rs: icons.code,
|
|
|
|
vue: icons.code,
|
|
|
|
json: icons.code,
|
2022-02-10 14:32:50 +00:00
|
|
|
yml: icons.code,
|
2022-02-10 14:00:40 +00:00
|
|
|
yaml: icons.code,
|
|
|
|
toml: icons.code,
|
2021-06-23 20:27:51 +00:00
|
|
|
|
|
|
|
txt: icons.text,
|
|
|
|
rtf: icons.text,
|
2022-02-04 11:53:36 +00:00
|
|
|
vtt: icons.text,
|
|
|
|
srt: icons.text,
|
|
|
|
log: icons.text,
|
|
|
|
diff: icons.text,
|
|
|
|
|
2021-06-23 20:27:51 +00:00
|
|
|
md: icons.markdown,
|
2021-07-24 08:31:46 +00:00
|
|
|
|
|
|
|
epub: icons.book,
|
|
|
|
mobi: icons.book,
|
|
|
|
azw3: icons.book,
|
2022-01-08 07:10:36 +00:00
|
|
|
|
|
|
|
url: icons.link,
|
2021-06-23 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-12-17 13:21:25 +00:00
|
|
|
export function hasKey<O>(obj: O, key: PropertyKey): key is keyof O {
|
2021-06-23 20:27:51 +00:00
|
|
|
return key in obj
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:27:47 +00:00
|
|
|
export function getRawExtension(fileName: string): string {
|
|
|
|
return fileName.slice(((fileName.lastIndexOf('.') - 1) >>> 0) + 2)
|
|
|
|
}
|
2021-12-17 13:21:25 +00:00
|
|
|
export function getExtension(fileName: string): string {
|
2022-02-15 11:27:47 +00:00
|
|
|
return getRawExtension(fileName).toLowerCase()
|
2021-06-23 22:51:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 08:03:35 +00:00
|
|
|
export function getFileIcon(fileName: string, flags?: { video?: boolean }): [IconPrefix, IconName] {
|
2021-06-23 22:51:23 +00:00
|
|
|
const extension = getExtension(fileName)
|
2022-01-24 08:03:35 +00:00
|
|
|
let icon = hasKey(extensions, extension) ? extensions[extension] : icons.file
|
|
|
|
|
2022-01-26 07:40:21 +00:00
|
|
|
// 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.
|
2022-01-24 08:03:35 +00:00
|
|
|
if (extension === 'ts') {
|
|
|
|
if (flags?.video) {
|
|
|
|
icon = icons.video
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return icon
|
2021-06-23 20:27:51 +00:00
|
|
|
}
|