Low commit activity in last 3 years
Detect MIME types from file extensions, filenames, and binary content using magic byte signatures. Includes 200+ extension mappings, 30+ magic byte patterns, custom registration, charset detection, Accept header parsing, and content negotiation.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-mime_type

Tests Gem Version Last updated

MIME type detection from file extensions and magic bytes with 200+ mappings

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem "philiprehberger-mime_type"

Or install directly:

gem install philiprehberger-mime_type

Usage

require "philiprehberger/mime_type"

Philiprehberger::MimeType.for_extension('.pdf')   # => "application/pdf"
Philiprehberger::MimeType.for_filename('photo.jpg')  # => "image/jpeg"

Magic Byte Detection

content = File.binread('image.png', 16)
Philiprehberger::MimeType.for_content(content)  # => "image/png"

Modern image formats are also detected via the ISOBMFF ftyp brand: HEIC, HEIF, AVIF, and JPEG XL.

MIME Aliases

require "philiprehberger/mime_type"

Philiprehberger::MimeType.canonical('image/jpg')  # => "image/jpeg"
Philiprehberger::MimeType.valid?('image/jpg')     # => true

Reverse Lookup

Philiprehberger::MimeType.extensions('text/html')  # => [".html", ".htm"]

Category

Philiprehberger::MimeType.category('image/png')   # => :image
Philiprehberger::MimeType.category('audio/mpeg')   # => :audio
Philiprehberger::MimeType.category('video/mp4')    # => :video

Validation

Philiprehberger::MimeType.valid?('text/plain')     # => true
Philiprehberger::MimeType.valid?('not valid')      # => false

Custom Registration

Philiprehberger::MimeType.register('.custom', 'application/x-custom')
Philiprehberger::MimeType.for_extension('.custom')  # => "application/x-custom"

Philiprehberger::MimeType.unregister('.custom')
Philiprehberger::MimeType.for_extension('.custom')  # => nil

Charset Detection

Philiprehberger::MimeType.charset('text/html')        # => "utf-8"
Philiprehberger::MimeType.charset('application/json')  # => nil

Type Predicates

Philiprehberger::MimeType.text?('text/plain')       # => true
Philiprehberger::MimeType.binary?('image/png')      # => true

Category Predicates

Philiprehberger::MimeType.image?('image/png')                   # => true
Philiprehberger::MimeType.text?('text/html; charset=utf-8')     # => true
Philiprehberger::MimeType.audio?('audio/mpeg')                  # => true
Philiprehberger::MimeType.video?('video/mp4')                   # => true
Philiprehberger::MimeType.application?('application/json')       # => true
Philiprehberger::MimeType.font?('font/woff2')                   # => true
Philiprehberger::MimeType.multipart?('multipart/form-data')      # => true
Philiprehberger::MimeType.message?('message/rfc822')             # => true
Philiprehberger::MimeType.image?(nil)                           # => false

Accept Header Parsing

Philiprehberger::MimeType.parse_accept('text/html;q=0.9, application/json')
# => [{ type: "application/json", q: 1.0 }, { type: "text/html", q: 0.9 }]

Content Negotiation

available = ['application/json', 'text/html']
Philiprehberger::MimeType.best_match(available, 'text/*;q=0.5, application/json')
# => "application/json"

API

Method Description
MimeType.for_extension(ext) Detect MIME type from a file extension
MimeType.for_filename(name) Detect MIME type from a filename
MimeType.for_content(bytes) Detect MIME type from magic bytes
MimeType.extensions(mime) Get file extensions for a MIME type
MimeType.category(mime) Get the category of a MIME type
MimeType.valid?(mime) Check if a MIME type string is valid
MimeType.canonical(mime) Map legacy/alias MIME types to their canonical form
MimeType.register(ext, mime) Register a custom MIME type mapping
MimeType.unregister(ext) Remove a custom registration
MimeType.charset(mime) Get default charset for a MIME type
MimeType.parse_accept(header) Parse HTTP Accept header string
MimeType.best_match(available, header) Content negotiation for best match
MimeType.text?(mime) Check if MIME type is text
MimeType.binary?(mime) Check if MIME type is binary
MimeType.image?(mime) Check if MIME type is image/*
MimeType.audio?(mime) Check if MIME type is audio/*
MimeType.video?(mime) Check if MIME type is video/*
MimeType.application?(mime) Check if MIME type is application/*
MimeType.font?(mime) Check if MIME type is font/*
MimeType.multipart?(mime) Check if MIME type is multipart/*
MimeType.message?(mime) Check if MIME type is message/*

Development

bundle install
bundle exec rspec
bundle exec rubocop

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT