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"

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.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