Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Forked from szimek/paperclip_url_tempfile.rb
Created March 26, 2011 16:24
Show Gist options
  • Save zeroeth/888414 to your computer and use it in GitHub Desktop.
Save zeroeth/888414 to your computer and use it in GitHub Desktop.
# 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