Skip to content

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 ​

FunctionReturnsDescription
getFileUrlExtension(url)stringLower-cased extension of a URL or file name, ignoring query string and hash. Empty string if none.
getExtensionType(ext)FileTypeCategoryCategory of an extension (case-insensitive), 'application' when unknown.
getFileType(file)stringFor 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)stringFor 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:

CategoryExtensionsIconColor
imagejpg, jpeg, png, gif, tiff, webp, svgmdi-file-image#87CEEB
audiom4a, flac, mp3, wav, wma, aac, oggmdi-music#673AB7
videomp4, mov, wmv, avi, mkv, webmmdi-filmstrip#3F51B5
pdfpdfmdi-file-pdf-box#FF0000
documentdoc, docxmdi-file-document#0000FF
excelxls, xlsxmdi-file-excel#008000
csvcsvmdi-file-excel#008000
powerPointppt, pptxmdi-file-powerpoint#ED6C47
texttxt, mdmdi-file-document-outline#795548
archivezip, rar, 7z, tar, gzmdi-folder-zip#607D8B
codejs, ts, json, html, css, py, java, cpp, c, php, sqlmdi-code-json#9C27B0
applicationanything elsemdi-file#009688

Source Code ​