Created
February 9, 2012 02:26
-
-
Save carlzulauf/1776588 to your computer and use it in GitHub Desktop.
paperclip attachment remote file download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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