Skip to content

Mime type detection in ruby via file extension or file content

License

Notifications You must be signed in to change notification settings

fursich/mimemagic

Repository files navigation

MimeMagic is a library to detect the mime type of a file by extension or by content. It uses the mime database provided by freedesktop.org (see http://freedesktop.org/wiki/Software/shared-mime-info/).

Gem Version

Warning: If you are using a version of MimeMagic < 0.3.7, or version 0.4.0, you may well be in breach of the GPL due to a GPL licensed dependency that was bundled with this gem. You should update to a version >= 0.3.7 as soon as possible. See mimemagicrb#97 for details.

Dependencies

You will require a copy of the Freedesktop.org shared-mime-info database to be available. If you're on Linux, it's probably available via your package manager, and will probably be in the location it's being looked for when the gem is installed.

macOS users can install the database via Homebrew with brew install shared-mime-info.

If for whatever reason you can't do either of those things, you'll need to obtain a copy from the Internet. Set the environment variable FREEDESKTOP_MIME_TYPES_PATH before installing the gem in order to point the build at that location.

(TODO: Locate a reliable source for the compiled file, and link to it both here, and in the error message if one isn't found.)

Usage

require 'mimemagic'
MimeMagic.by_extension('html').text?
MimeMagic.by_extension('.html').child_of? 'text/plain'
MimeMagic.by_path('filename.txt')
MimeMagic.by_magic(File.open('test.html'))
# etc...

You can add your own magic with MimeMagic.add.

API

http://www.rubydoc.info/github/minad/mimemagic

Tests

bundle install

rake test

Authors

Daniel Mendler

LICENSE

MIT

About

Mime type detection in ruby via file extension or file content

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%