mime-types vs file-type
File Type Detection Libraries Comparison
1 Year
mime-typesfile-typeSimilar Packages:
What's File Type Detection Libraries?

File type detection libraries are essential tools in web development for identifying the format of files based on their content or extension. They help ensure that the correct processing is applied to files, enhancing security and functionality in applications that handle file uploads or downloads. The primary purpose of these libraries is to provide accurate and reliable detection of file types, which is crucial for validating user input, managing file storage, and serving files appropriately based on their types.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
mime-types69,498,5721,37622.3 kB1220 days agoMIT
file-type22,593,7983,936105 kB10a month agoMIT
Feature Comparison: mime-types vs file-type

Detection Method

  • mime-types:

    The 'mime-types' library relies on a mapping of file extensions to their corresponding MIME types. It provides a straightforward way to retrieve MIME types based on file extensions, but it does not analyze the file content, which can lead to inaccuracies if the file extension does not match the actual file type.

  • file-type:

    The 'file-type' library uses a binary signature approach to determine the file type, analyzing the first few bytes of a file to identify its format accurately. This method provides a high level of reliability as it is not dependent on the file extension, making it suitable for security-sensitive applications where file content must be verified.

Use Cases

  • mime-types:

    Best suited for applications that require serving files over HTTP with the correct content type, such as web servers or APIs. It is also useful in scenarios where file type validation is based solely on file extensions, such as static file serving or simple file management systems.

  • file-type:

    Ideal for scenarios where file uploads are processed, and there is a need to validate the file type before further processing. It is commonly used in applications that handle user-generated content, such as image galleries, document upload systems, and file-sharing services.

Performance

  • mime-types:

    The 'mime-types' library is lightweight and fast, as it only performs lookups in a predefined mapping of extensions to MIME types. This makes it suitable for applications where performance is a priority, and the accuracy of file type detection based on content is less critical.

  • file-type:

    While 'file-type' is efficient in detecting file types, it may incur a slight overhead due to the need to read the file's binary content. However, this trade-off is often worth it for the accuracy it provides, especially in security-critical applications.

Extensibility

  • mime-types:

    The 'mime-types' library provides a simple API for adding new MIME types and extensions to its mapping. This extensibility is useful for applications that need to support custom file types or extensions that are not included in the default configuration.

  • file-type:

    The 'file-type' library is extensible, allowing developers to add custom file type signatures if needed. This feature is beneficial for applications that handle proprietary or uncommon file formats that may not be covered by the default signatures.

Community and Support

  • mime-types:

    The 'mime-types' library also has a robust community and is widely used in various applications. Its simplicity and ease of use contribute to its popularity, making it a well-supported choice for MIME type management.

  • file-type:

    The 'file-type' library has a strong community and is actively maintained, ensuring that it stays up-to-date with new file formats and security practices. This support is crucial for developers looking for reliable solutions in file type detection.

How to Choose: mime-types vs file-type
  • mime-types:

    Choose 'mime-types' if you are looking for a library that primarily deals with MIME type management, including mapping file extensions to their corresponding MIME types. This is beneficial for applications that need to serve files with the correct content type in HTTP responses or when handling file uploads based on their extensions.

  • file-type:

    Choose 'file-type' if you need a library that detects file types based on the actual content of the file rather than just the file extension. It is particularly useful for applications that require high accuracy in file validation, such as image upload services or document management systems.

README for mime-types

mime-types

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

The ultimate javascript content-type utility.

Similar to the mime@1.x module, except:

  • No fallbacks. Instead of naively returning the first available type, mime-types simply returns false, so do var type = mime.lookup('unrecognized') || 'application/octet-stream'.
  • No new Mime() business, so you could do var lookup = require('mime-types').lookup.
  • No .define() functionality
  • Bug fixes for .lookup(path)

Otherwise, the API is compatible with mime 1.x.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install mime-types

Note on MIME Type Data and Semver

This package considers the programmatic api as the semver compatibility. Additionally, the package which provides the MIME data for this package (mime-db) also considers it's programmatic api as the semver contract. This means the MIME type resolution is not considered in the semver bumps.

In the past the version of mime-db was pinned to give two decision points when adopting MIME data changes. This is no longer true. We still update the mime-db package here as a minor release when necessary, but will use a ^ range going forward. This means that if you want to pin your mime-db data you will need to do it in your application. While this expectation was not set in docs until now, it is how the pacakge operated, so we do not feel this is a breaking change.

If you wish to pin your mime-db version you can do that with overrides via your package manager of choice. See their documentation for how to correctly configure that.

Adding Types

All mime types are based on mime-db, so open a PR there if you'd like to add mime types.

API

var mime = require('mime-types')

All functions return false if input is invalid or not found.

mime.lookup(path)

Lookup the content-type associated with a file.

mime.lookup('json') // 'application/json'
mime.lookup('.md') // 'text/markdown'
mime.lookup('file.html') // 'text/html'
mime.lookup('folder/file.js') // 'application/javascript'
mime.lookup('folder/.htaccess') // false

mime.lookup('cats') // false

mime.contentType(type)

Create a full content-type header given a content-type or extension. When given an extension, mime.lookup is used to get the matching content-type, otherwise the given content-type is used. Then if the content-type does not already have a charset parameter, mime.charset is used to get the default charset and add to the returned content-type.

mime.contentType('markdown') // 'text/x-markdown; charset=utf-8'
mime.contentType('file.json') // 'application/json; charset=utf-8'
mime.contentType('text/html') // 'text/html; charset=utf-8'
mime.contentType('text/html; charset=iso-8859-1') // 'text/html; charset=iso-8859-1'

// from a full path
mime.contentType(path.extname('/path/to/file.json')) // 'application/json; charset=utf-8'

mime.extension(type)

Get the default extension for a content-type.

mime.extension('application/octet-stream') // 'bin'

mime.charset(type)

Lookup the implied default charset of a content-type.

mime.charset('text/markdown') // 'UTF-8'

var type = mime.types[extension]

A map of content-types by extension.

[extensions...] = mime.extensions[type]

A map of extensions by content-type.

License

MIT