File helpers
Helpers to classify files by extension and pick an icon and a color for them. They accept a File, a URL / path string or a plain object (e.g. a file record returned by the API) and are auto-imported.
Usage
vue
<script setup lang="ts">
const props = defineProps<{ file: { name: string, url: string } }>()
const category = computed(() => getFileCategory(props.file))
</script>
<template>
<v-chip :prepend-icon="category.icon" :color="category.color">
{{ getFileName(file) }}
</v-chip>
</template>ts
getFileUrlExtension('https://cdn.example.com/docs/Report.PDF?token=1') // 'pdf'
getExtensionType('xlsx') // 'excel'
getFileType('photo.jpg') // 'image/jpg'
getFileType({ name: 'notes.md' }) // 'text/md'
getFileName('https://cdn.example.com/docs/report.pdf#page=2') // 'report.pdf'
getFileCategory('archive.zip') // { icon: 'mdi-folder-zip', color: '#607D8B' }Functions
| Function | Returns | Description |
|---|---|---|
getFileUrlExtension(url) | string | Lower-cased extension of a URL or file name, ignoring query string and hash. Empty string if none. |
getExtensionType(ext) | FileTypeCategory | Category of an extension (case-insensitive), 'application' when unknown. |
getFileType(file) | string | For a File, its MIME type (or application/<ext> when the browser gives none). For a string or object, <category>/<ext>; objects use file.extension when present, otherwise the extension of getFileName(file). |
getFileName(file) | string | For objects (and File), the first of name, file_name, filename, url. For strings, the last path segment without query string and hash. |
getFileCategory(file) | { icon, color } | Icon and color from fileTypeMap for the category in getFileType(file); falls back to application. For a File this uses the first part of its real MIME type (image/png → image). |
Data
extensions is the list of known extensions ({ value, type }[]), fileTypeMap maps each category to an MDI icon and a color:
| Category | Extensions | Icon | Color |
|---|---|---|---|
image | jpg, jpeg, png, gif, tiff, webp, svg | mdi-file-image | #87CEEB |
audio | m4a, flac, mp3, wav, wma, aac, ogg | mdi-music | #673AB7 |
video | mp4, mov, wmv, avi, mkv, webm | mdi-filmstrip | #3F51B5 |
pdf | mdi-file-pdf-box | #FF0000 | |
document | doc, docx | mdi-file-document | #0000FF |
excel | xls, xlsx | mdi-file-excel | #008000 |
csv | csv | mdi-file-excel | #008000 |
powerPoint | ppt, pptx | mdi-file-powerpoint | #ED6C47 |
text | txt, md | mdi-file-document-outline | #795548 |
archive | zip, rar, 7z, tar, gz | mdi-folder-zip | #607D8B |
code | js, ts, json, html, css, py, java, cpp, c, php, sql | mdi-code-json | #9C27B0 |
application | anything else | mdi-file | #009688 |