Skip to main content

Overview

MIME type utilities provide file type detection based on file extensions and categorization of files by their content type.

getMimeType()

Get the MIME type for a filename based on its extension.

Signature

function getMimeType(filename: string): string

Parameters

  • filename string - File name or path

Returns

MIME type string. Returns 'application/octet-stream' if extension is unknown.

Usage

import { getMimeType } from '@core/utils/mime';

getMimeType('document.txt'); // 'text/plain'
getMimeType('README.md'); // 'text/markdown'
getMimeType('config.json'); // 'application/json'
getMimeType('script.js'); // 'text/javascript'

getFileCategory()

Categorize a file by its MIME type.

Signature

function getFileCategory(mime: string): 'text' | 'image' | 'video' | 'audio' | 'archive' | 'binary'

Parameters

  • mime string - MIME type string

Returns

Category: 'text', 'image', 'video', 'audio', 'archive', or 'binary'

Usage

import { getFileCategory, getMimeType } from '@core/utils/mime';

const mime = getMimeType('document.pdf');
const category = getFileCategory(mime);
// 'binary'

getFileCategory('text/plain'); // 'text'
getFileCategory('image/png'); // 'image'
getFileCategory('video/mp4'); // 'video'
getFileCategory('audio/mpeg'); // 'audio'
getFileCategory('application/zip'); // 'archive'
getFileCategory('application/pdf'); // 'binary'

isBinaryMime()

Check if a MIME type represents binary (non-text) data.

Signature

function isBinaryMime(mime: string): boolean

Parameters

  • mime string - MIME type string

Returns

true if the MIME type is binary (not text), false otherwise.

Usage

import { isBinaryMime } from '@core/utils/mime';

isBinaryMime('text/plain'); // false
isBinaryMime('application/json'); // false (treated as text)
isBinaryMime('image/png'); // true
isBinaryMime('video/mp4'); // true
isBinaryMime('application/pdf'); // true

Supported File Types

Text Files

ExtensionMIME Type
.txttext/plain
.mdtext/markdown
.jsonapplication/json
.jstext/javascript
.tstext/typescript
.jsxtext/jsx
.tsxtext/tsx
.csstext/css
.htmltext/html
.xmltext/xml
.yaml, .ymltext/yaml
.csvtext/csv
.logtext/plain

Programming Languages

ExtensionMIME Type
.pytext/x-python
.rbtext/x-ruby
.gotext/x-go
.rstext/x-rust
.ctext/x-c
.cpptext/x-c++
.javatext/x-java
.sh, .bash, .zshtext/x-shellscript
.sqltext/x-sql

Images

ExtensionMIME Type
.pngimage/png
.jpg, .jpegimage/jpeg
.gifimage/gif
.svgimage/svg+xml
.webpimage/webp
.icoimage/x-icon

Video

ExtensionMIME Type
.mp4video/mp4
.webmvideo/webm
.avivideo/x-msvideo
.movvideo/quicktime
.mkvvideo/x-matroska

Audio

ExtensionMIME Type
.mp3audio/mpeg
.wavaudio/wav
.oggaudio/ogg
.flacaudio/flac
.m4aaudio/mp4

Archives

ExtensionMIME Type
.zipapplication/zip
.tarapplication/x-tar
.gz, .tgzapplication/gzip
.bz2application/x-bzip2
.7zapplication/x-7z-compressed
.rarapplication/vnd.rar

Binary / Application

ExtensionMIME Type
.pdfapplication/pdf
.wasmapplication/wasm
.exe, .dllapplication/x-msdownload
.so, .dylibapplication/x-sharedlib

Usage Examples

File Upload Validation

import { getMimeType, isBinaryMime } from '@core/utils/mime';

function validateUpload(filename) {
  const mime = getMimeType(filename);
  
  const allowedTypes = [
    'image/png',
    'image/jpeg',
    'application/pdf',
  ];
  
  if (!allowedTypes.includes(mime)) {
    throw new Error(`File type ${mime} not allowed`);
  }
  
  return mime;
}

validateUpload('photo.jpg'); // 'image/jpeg'
validateUpload('script.js'); // throws Error

Content Type Headers

import { getMimeType } from '@core/utils/mime';

function serveFile(path, response) {
  const mime = getMimeType(path);
  response.setHeader('Content-Type', mime);
  // ... send file
}

File Processing by Type

import { getMimeType, getFileCategory } from '@core/utils/mime';

function processFile(filename, content) {
  const mime = getMimeType(filename);
  const category = getFileCategory(mime);
  
  switch (category) {
    case 'text':
      return processTextFile(content);
    case 'image':
      return processImage(content);
    case 'archive':
      return extractArchive(content);
    default:
      return content;
  }
}

Filter Files by Type

import { getMimeType, isBinaryMime } from '@core/utils/mime';

function filterTextFiles(files) {
  return files.filter(file => {
    const mime = getMimeType(file.name);
    return !isBinaryMime(mime);
  });
}

const files = [
  { name: 'readme.md' },
  { name: 'photo.jpg' },
  { name: 'config.json' },
  { name: 'video.mp4' },
];

filterTextFiles(files);
// [{ name: 'readme.md' }, { name: 'config.json' }]

File Icon Selection

import { getFileCategory, getMimeType } from '@core/utils/mime';

function getFileIcon(filename) {
  const category = getFileCategory(getMimeType(filename));
  
  const icons = {
    text: '📄',
    image: '🖼️',
    video: '🎬',
    audio: '🎵',
    archive: '📦',
    binary: '📁',
  };
  
  return icons[category];
}

getFileIcon('document.txt'); // '📄'
getFileIcon('photo.png'); // '🖼️'
getFileIcon('archive.zip'); // '📦'

Directory Listing

import { getMimeType, getFileCategory } from '@core/utils/mime';

function listFiles(files) {
  files.forEach(file => {
    const mime = getMimeType(file.name);
    const category = getFileCategory(mime);
    console.log(`${file.name} [${category}] - ${mime}`);
  });
}

listFiles([
  { name: 'index.html' },
  { name: 'styles.css' },
  { name: 'logo.svg' },
]);
// index.html [text] - text/html
// styles.css [text] - text/css
// logo.svg [image] - image/svg+xml

Notes

  • Detection is based on file extension only (not content analysis)
  • Extensions are case-insensitive
  • Unknown extensions return 'application/octet-stream'
  • application/json is categorized as 'text'
  • Path separators are allowed in filename

Build docs developers (and LLMs) love