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/).
require 'mimemagic' .('html').text? .('.html').child_of? 'text/plain' .('filename.txt') .(File.open('test.html'))# etc...
Extra magic overlay
Microsoft Office 2007+ formats (xlsx, docx, and pptx) are not supported by the mime database at freedesktop.org. These files are all zipped collections of xml files and will be detected as "application/zip". Mimemagic comes with extra magic you can overlay on top of the defaults to correctly detect these file types. Enable it like this:
require 'mimemagic' require 'mimemagic/overlay' .(File.open('test.xlsx'))
You can add your own magic with
bundle install rake test