Paperclip is a wonderful module by Thoughtbot that lets makes handling attachments in Rails a breeze, except for one minor niggle - file types. It relies on the content type header sent by the browser which is woefully inconsistent. For example, when uploading a zip the possible values sent for content type are:
- '' (yes empty)
In short you can't rely on Content-Type from the browser to ever have a sensible value.
Thus the purpose of this module: it uses the wonderful
libmagic to work out the file type based
on the contents of the uploaded file without shelling out to
file to do its
The ruby-filemagic gem (which this gem uses) depends upon
libmagic must be
installed to give us file type detection. On Linux this means installing the
sudo apt-get install libmagic-dev # Deb sudo yum install file-devel # rpm sudo emerge file # gentoo
OSX ships with the
file binary but not the
libmagic shared library so I
recommend installing it via homebrew:
brew install libmagic # Since this will be in a non-standard place we need to build ruby-filemagic specially gem install ruby-filemagic -- --with-opt-dir=$(brew list libmagic | head -n1 | xargs dirname)
Include the gem in your Gemfile:
Its all automatic - magic-paperclip does its trick by just being in the Gemfile (via a Railtie).
When a Paperclip attachment is assigned to its content_type will be detected and will overwrite what the browser sends.
libmagic often returns charset info or similar for files (i.e.
text/plain; charset=us-asci) that this module attempts to remove to use just the base
type. Supposedly older versions of libmagic might return this in a
different format and thus not get removed. If you find an example of this
happening please let me know.
Credits & License
This gem was developed for use at DigiResults and pulled out into a reusable gem.
Released under the MIT License
Copyright (c) 2011, Ash Berlin and DigiResults Ltd.