DEV Community

Cover image for name2mime - A simple node package to get MIME types of file
Hashir Baig
Hashir Baig

Posted on

6

name2mime - A simple node package to get MIME types of file

Background

So I recently had a client who wanted a Lambda trigger on his S3 bucket which writes some files to a directory in his bucket. Everything was going well, except that the content type of all the files was being set to application/octet-stream. So he wanted me to fix this issue such that when we put the object in the bucket, the ContentType gets set to an appropriate one. I thought it'll hardly take an hour. All I'd have to do is to search for a node package, install it and the issue is solved. But nope. NOPE!

Exiting solutions

  1. mmmagic:
    It's a great package, uses C-language binding behind the scene, takes file buffer as input, and spits out a lot of metadata about a file.

    Limitations: When passed an SVG/DXF, it categorizes them as text/html.

  2. file-type:
    It also takes a file buffer as input and returns MIME type.

    Limitations: When passed an SVG/DXF or some other kind of files, it retuns null/undefined.

  3. mime-kind:
    It takes a file buffer and a default value as input and if the file type is not determined, it returns that default value.

    Limitations: Same results here, it was assigning that default value for SVF/DXF and some other files.

  4. ext2mime:
    This one works similar in fashion to the one I created. It takes in files name and spits out its MIME types.

    Limitations: The dataset of extensions was very limited and couldn't recognize all kinds of files.

600+ extensions supported

No credits to me. Thanks to the good guys at FreeFormatter. I wrote a simple script in the browser console to extract all the extensions from their site.

document.write(JSON.stringify(Array.from(document.getElementsByClassName('bordered-table zebra-striped table-sort')[0].children[1].children).reduce((obj, row) => {
    let objs = {...obj};
    row.children[2].childNodes[0].data.split(', ').map(ext => objs = objs[ext] ? objs : ({...objs, [ext]: {'type': row.children[1].childNodes[0].data, 'name': row.children[0].childNodes[0].data}}))
    return objs;
}, {})))
Enter fullscreen mode Exit fullscreen mode

The Magic Script

Hit ENTER!

The MAGIC Itself

Building the idea into an NPM package

I used this amazing boilerplate code to build my NPM package. It already had template for test cases and Travis-CI, so whenever I push my code to github, it automatically runs test cases and updates the status on my repo homepage. Amazing isn't it?

Installation

$ npm install name2mime --save
Enter fullscreen mode Exit fullscreen mode

or

$ yarn add name2mime
Enter fullscreen mode Exit fullscreen mode

Sample

const getMime = require('name2mime');

const value = getMime('filename.jpg');

console.log(value);

// { type: 'image/jpeg', name: 'JPEG Image' }
Enter fullscreen mode Exit fullscreen mode

For Maintainers & Contributors

Commands

  • npm run clean - Remove lib/ directory
  • npm test - Run tests with linting and coverage results.
  • npm test:only - Run tests without linting or coverage.
  • npm test:watch - You can even re-run tests on file changes!
  • npm test:prod - Run tests with minified code.
  • npm run test:examples - Test written examples on pure JS for better understanding module usage.
  • npm run lint - Run ESlint with airbnb-config
  • npm run cover - Get coverage report for your code.
  • npm run build - Babel will transpile ES6 => ES5 and minify the code.
  • npm run prepublish - Hook for npm. Do all the checks before publishing your module.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (3)

Collapse
 
rahulahire profile image
Rahul Ahire •

does it supports mime dectection for buffer or base64 strings? that'd be more awesome

Collapse
 
ramspantoja profile image
Ramiro Angel Pantoja Cordoba •

It works for me, thank you so much.

Collapse
 
quangtc profile image
Quang Cao •

thank you, you help me a lot