Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 1.77 KB

README.textile

File metadata and controls

32 lines (23 loc) · 1.77 KB

Description

URLTempfile is a descendent of Tempfile that creates a temp file from the
data/response body of an HTTP GET request to the specified URL. This code was
designed to be used with the
Paperclip Rails plugin.

Note, I consider this somewhat of a hack right now. It’s being used in a
production application, but you should review the code prior to your own
use. I heartily welcome enhancements, please do send pull requests.

Usage

First, place the URLTempfile.rb file into your Rails application’s lib
directory.

To use this in your Rails app, you will need to modify your HTML form to
have a field where the user enters a URL, and then some mechanism to determine
that they have specified a URL (my app currently uses a pulldown menu to pick
between File or URL to upload, and then swaps out different fields depending
on that choice, but you could also just detect if there is text in a text field
and so on). Then, in your controller code, you’d just set the “photo” (or
whatever named attribute you’re using for Paperclip) to the URLTempfile:

params[:blog_entry][:photo] = URLTempfile.new(params[:photo_url])

That’s it, from there on, the regular Paperclip file handling/functionality
takes over and stores your file, makes thumbnails or what not.

Warning!

The implementation uses open-uri, which does not provide a way to turn off SSL certificate validation for a request. This code has a hack to temporarily set no certification validation globally for OpenSSL while the file is downloaded. But BEWARE, this means that if you have other HTTPS requests that need certificate validation occurring at the same time, they will be affected. If you have a better solution, please do share/submit a pull request!