A generic swappable back-end for MIME type detection
Lots of ruby libraries utilize MIME type detection in some form. In order to best support multiple MIME type detection libraries, multi_mime
is a general-purpose, swappable, MIME type detection backend library.
eg MultiJson for MIME types.
gem 'multi_mime', '~> 1.1'
require 'multi_mime'
Part of utilizing various MIME type libraries is the need for a common interface. MultiMime
provides just that with following methods.
MultiMime.type_for('text/html') # 'text/html'
# alias :by_type
MultiMime.type_for_extension('.html') # 'text/html'
# alias :by_extension
MultiMime.type_for_path('/usr/local/foo/bar/foo.html') # 'text/html'
# alias :by_path
MultiMime.type_for_file(File.open('foo.html', 'w')) # 'text/html'
# alias :by_file
MultiMime
tries to have intelligent defaulting. That is, if you have any of the supported engines already loaded, it will utilize them before attempting to load any. When loading, libraries are ordered in the same order as Supported Mime Engines. Lets try using MultiMime
with Rack::Mime
loaded, then switch it to MIME::Types
.
require 'rack/mime' # true
MultiMime.default_adapter # ':rack_mime'
MultiMime.adapter # MultiMime::Adapters::RackMime
MultiMime.reset_adapter # MultiMime::Adapters::RackMime
MultiMime.adapter = :mime_types # `:mime_types`
require 'mime/types' # false (eg loaded)
MultiMime.adapter # MultiMime::Adapters::MimeTypes
MultiMime.type_for_extension('.json') # 'application/json'
When MultiMime fails to load the specified adapter, it'll throw MultiMime::AdapterError
which inherits from ArgumentError
.
- MIME::Types
- MagicMime
- Rails (ActionDispatch::Http::Mime)
- Rack (Rack::Mime)
This library aims to support and is tested against the following Ruby implementations:
Inspiration:
Cribbed: