JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12104
  • Score
    100M100P100Q134198F
  • License MIT

Utility to parse mime type from a binary file

Package Exports

  • file-type-mime
  • file-type-mime/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (file-type-mime) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

file-type-mime

FOSSA Status

Utility to parse mime type from a file content.

Usage

Browser (react)

import parse from 'file-type-mime';

export default function fileUpload() {
  async function onChange(e) {
    const [file] = e.target.files;
    const buffer = await file.arrayBuffer();
    const result = parse(buffer);

    console.log('MIME_TYPE', result);
  }

  return (
    <form>
      <input type="file" onChange={onChange}>
    </form>
  );
}

Node.js

import parse from 'file-type-mime';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const file = resolve('./path/to/file.pdf');
const buffer = readFileSync(file);
const result = parse(buffer);

console.log('MIME_TYPE', result);

API

Signature

function parse(buffer: ArrayBuffer, options: Options = {}): Result | undefined

Arguments

buffer

Type: ArrayBuffer

A buffer representing file data

options (optional)

Type: { extra?: boolean; hint?: { ext?: string; mime?: string } }

  • hint - used to short-circuit general flow by filtering signatures list
  • extra - used to parse additional file type formats (like json)

Return

Type: { ext: string; mime: string } | undefined

Supported file types

(more to come...)

File extension Content (mime) type
bmp image/bmp
gif image/gif
ico image/x-icon
jpg image/jpeg
heic image/heic
png image/png
tiff image/tiff
pdf application/pdf
rtf application/rtf
epub application/epub+zip
gz application/gzip
jar application/java-archive
zip application/zip
bz2 application/x-bzip2
rar application/x-rar-compressed
tar application/x-tar
docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
opd application/vnd.oasis.opendocument.presentation
ods application/vnd.oasis.opendocument.spreadsheet
odt application/vnd.oasis.opendocument.text
7z application/x-7z-compressed
avi video/x-msvideo
mp3 audio/mp3
mp4 video/mp4
oga audio/ogg
ogg audio/ogg
ogm video/ogg
ogv video/ogg
ogx application/ogg
wav audio/wav
json application/json

License

FOSSA Status