Skip to content

Instantly share code, notes, and snippets.

@szimek
Created July 9, 2010 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save szimek/469283 to your computer and use it in GitHub Desktop.
Save szimek/469283 to your computer and use it in GitHub Desktop.
Paperclip extension for fetching images from URL
# 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