Storing Images from remote URLs
Posted by Craig Ambrose on February 22, 2008 at 12:17 AM
Here’s a handy little modification to attachment_fu to allow the model to have it’s file data set from a remote url. I’ve been doing this for a while but recently altered it to compute the mime-type from the downloaded file, rather than just basing it off the extension. This is because some web sites host files (such as images) at urls without a file extension.
As this is a monkey patch for attachment_fu, I follow Chris Wanstrath’s evil twin convention and put it in a plugin called attachment_fu_hacks.
This code is also dependent on the mimetype-fu plugin. Mimetype-fu calculates the mime type of a file using the *nix “file” command, rather than using file extensions as the mime-types gem does. If you’re using OSX, then this wont work unless you change the two occurances of file -bir in mimetype_fu.rb to file -br —mime (which is compatible with both OSX and linux). I’ve submitted that change to the author so hopefully it will be incorporated into future versions..

Comments
There are 3 comments on this post. Post yours →
I’m trying to accomplish something similar with a different technique. I would like to try the way you’re describing by monkey-patching attachment_fu, but I don’t quite get what to do. Can you provide some example code for the controller and/or views? Here’s what I’ve got in mind for an alternative right now: http://github.com/trevorturk/el-dorado/tree/cdf9385255dffbbe3a93a6e8f1677590b0c9c7a8/app/models/url_upload.rb
One more way to do this without monkey patching:
http://matt.west.co.tt/ruby/passing-remote-web-data-to-attachment_fu/
Recently altered it to compute the mime-type from the downloaded file, rather than just basing it off the extension. This is because some web sites host files at urls without a file extension.
Post a comment
Required fields in bold.