-
-
Save zeroeth/888414 to your computer and use it in GitHub Desktop.
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
# Paperclip compliant class for uploading files through URL | |
# Simplified version of http://github.com/chris/paperclip_url_support | |
require "open-uri" | |
module Paperclip | |
class UrlTempfile < Tempfile | |
attr :content_type | |
def initialize(url) | |
@url = URI.parse(url) | |
raise "Unable to determine filename for URL uploaded file." unless original_filename | |
super("url_tempfile") | |
Kernel.open(url) do |file| | |
@content_type = file.content_type | |
binmode | |
write(file.read) | |
flush | |
end | |
end | |
def original_filename | |
# Take the URI path and strip off everything after last slash, assume this | |
# to be filename (URI path already removes any query string) | |
match = @url.path.match(/^.*\/(.+)$/) | |
return (match ? match[1] : nil) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment