Skip to content

Instantly share code, notes, and snippets.

@carlzulauf
Created February 9, 2012 02:26
Show Gist options
  • Save carlzulauf/1776588 to your computer and use it in GitHub Desktop.
Save carlzulauf/1776588 to your computer and use it in GitHub Desktop.
paperclip attachment remote file download
require 'open-uri'
def get_file(url)
filename = url.split("/").last.gsub(/_[0-9a-f\-]{36}\./,".")
begin
file = open( url )
file.instance_variable_set(:@original_filename, filename)
def file.original_filename; @original_filename; end
file
rescue OpenURI::HTTPError
nil
end
end
# Usage
m = PaperclipModel.new
m.attachment = get_file("http://some-place-with-a-file/")
m.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment