What is it?
Some great Rails plugins like attachment_fu use the content type/mime type of a file to validate the instance of an object. The plugin usually gets the mime type using the CGI request, however, if the file is already in the system, this approach won't work. Adobe Flash is also known not to send the proper mime type. As an alternative, I wrote mimetype_fu, a simple plugin which will try to retrieve the mimetype.
Note that mimetype_fu used to only looks at the extension to define its mime type, that's not true anymore, this nifty little plugin uses your os to open the file and check on the header for the mime type :) (sorry windows users, mimetype will only check on the file extension)
How to use it?
mimetype_fu extends the File class:
>> @file = File.open("#{RAILS_ROOT}/test/test.mp3")
=> #<File:/Users/Matt/Documents/mimetype_fu_example/test/test.mp3>
>> File.mime_type?(@file)
=> "audio/mpeg"
You can also get the mime type of the file by only passing its path:
>> File.mime_type?('test.jpg')
=> "image/jpeg"
How to install mimetype_fu
script/plugin install http://mimetype-fu.googlecode.com/svn/trunk/
(don't forget to rename the plugin folder)
You can also use svn:
cd vendor/plugins
svn checkout http://mimetype-fu.googlecode.com/svn/trunk/ mimetype-fu
FYI: mimetype-fu is moving to github: http://github.com/matta/mimetype-fu/tree Feel free to fork mimetype-fu and send me a pull request
