MimeMagic is a library to detect the mime type of a file by extension or by content. It uses the mime database from the package shared-mime-info.
macOS users can install the database via Homebrew, and then set the environment variable
FREEDESKTOP_MIME_TYPES_PATH
to that path. Once that has been done the gem should install successfully.
brew install shared-mime-info
- FREEDESKTOP_MIME_TYPES_PATH="" gem install mimemagic
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
.
$ bundle install
$ bundle exec rake test
- Daniel Mendler
- Jon Wood
- MimeMagic Contributors
{file:LICENSE MIT}