Class Index [+]

Quicksearch

MIME::Types for Ruby

home

mime-types.rubyforge.org/

code

github.com/halostatue/mime-types/

bugs

github.com/halostatue/mime-types/issues

rdoc

mime-types.rubyforge.org/

code climate

continuous integration

Description

This library allows for the identification of a file’s likely MIME content type. This is release 1.25.1, fixing an issue with priority comparison for mime-types 1.x. The current release is 2.0, which only supports Ruby 1.9 or later.

Release 1.25.1 contains all features of 1.25, including the experimental caching and lazy loading functionality. The caching and lazy loading features were initially implemented by Greg Brockman (gdb). As these features are experimental, they are disabled by default and must be enabled through the use of environment variables. The cache is invalidated on a per-version basis; the cache for version 1.25 will not be reused for any later version.

To use lazy loading, set the environment variable RUBY_MIME_TYPES_LAZY_LOAD to any value other than ‘false’. When using lazy loading, the initial startup of MIME::Types is around 12–25× faster than normal startup (on my system, normal startup is about 90 ms; lazy startup is about 4 ms). This isn’t generally useful, however, as the MIME::Types database has not been loaded. Lazy startup and load is just slightly faster—around 1 ms. The real advantage comes from using the cache.

To enable the cache, set the environment variable RUBY_MIME_TYPES_CACHE to a filename where MIME::Types will have read-write access. The first time a new version of MIME::Types is run using this file, it will be created, taking a little longer than normal. Subsequent loads using the same cache file will be approximately 3½× faster (25 ms) than normal loads. This can be combined with RUBY_MIME_TYPES_LAZY_LOAD, but this is not recommended in a multithreaded or multiprocess environment where all threads or processes will be using the same cache file.

As the caching interface is still experimental, the only values cached are the default MIME::Types database, not any custom MIME::Types added by users.

MIME types are used in MIME-compliant communications, as in e-mail or HTTP traffic, to indicate the type of content which is transmitted. MIME::Types provides the ability for detailed information about MIME entities (provided as a set of MIME::Type objects) to be determined and used programmatically. There are many types defined by RFCs and vendors, so the list is long but not complete; don't hesitate to ask to add additional information. This library follows the IANA collection of MIME types (see below for reference).

MIME::Types for Ruby was originally based on MIME::Types for Perl by Mark Overmeer, copyright 2001 - 2009.

MIME::Types is built to conform to the MIME types of RFCs 2045 and 2231. It tracks the IANA registry (ftp) with some unofficial types added from the LTSW collection and added by the users of MIME::Types.

Synopsis

MIME types are used in MIME entities, as in email or HTTP traffic. It is useful at times to have information available about MIME types (or, inversely, about files). A MIME::Type stores the known information about one MIME type.

require 'mime/types'

plaintext = MIME::Types['text/plain']
# returns [text/plain, text/plain]
text      = plaintext.first
puts text.media_type            # => 'text'
puts text.sub_type              # => 'plain'

puts text.extensions.join(" ")  # => 'txt asc c cc h hh cpp hpp dat hlp'

puts text.encoding              # => quoted-printable
puts text.binary?               # => false
puts text.ascii?                # => true
puts text.obsolete?             # => false
puts text.registered?           # => true
puts text == 'text/plain'       # => true
puts MIME::Type.simplified('x-appl/x-zip')
                                # => 'appl/zip'

puts MIME::Types.any? { |type|
  type.content_type == 'text/plain'
}                               # => true
puts MIME::Types.all?(&:registered?)
                                # => false

Contributing

I value any contribution to MIME::Types you can provide: a bug report, a feature request, or code contributions.

Code contributions to MIME::Types are especially <del>welcome</del>encouraged. Because MIME::Types is a complex codebase, there are a few guidelines:

Test Dependencies

To run the test suite, you will need to install the development dependencies for MIME::Types. If you have Bundler, you can accomplish this easily:

$ bundle install

MIME::Types uses Ryan Davis’s excellent Hoe to manage the release process, and it adds a number of rake tasks. You will mostly be interested in:

$ rake

which runs the tests the same way that:

$ rake test
$ rake travis

will do.

Workflow

Here’s the most direct way to get your work merged into the project:

Contributors

Thanks to everyone else who has contributed to MIME::Types:

Licence

This software is available under three licenses: the GNU GPL version 2 (or at your option, a later version), the Perl Artistic license, or the MIT license. Note that my preference for licensing is the MIT license, but the original Perl MIME::Types was dually originally licensed with the Perl Artistic and the GNU GPL (“the same terms as Perl itself”) and given that the Ruby implementation hewed pretty closely to the Perl version, I must maintain the additional licensing terms.

MIT License

this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Perl Artistic License (version 2)

See the file docs/artistic.txt in the main distribution.

GNU GPL version 2

See the file docs/COPYING.txt in the main distribution.

[Validate]

Generated with the Darkfish Rdoc Generator 2.