Skip to content

Instantly share code, notes, and snippets.

@Burgestrand
Created March 2, 2011 02:44
Show Gist options
  • Save Burgestrand/850377 to your computer and use it in GitHub Desktop.
Save Burgestrand/850377 to your computer and use it in GitHub Desktop.
A file upload using RestClient (and a hacky way at that)
require 'rest-client'
require 'tempfile'
require 'stringio'
img = StringIO.new(RestClient.get 'http://localhost:9393/img')
def img.path
'http://localhost:9393/img.png'
end
puts RestClient.post('http://localhost:9393/', :img => img)
require 'sinatra'
require 'base64'
require 'cgi'
get '/' do
<<-HTML
<!DOCTYPE html>
<html>
<body>
<img src="data:image/png;base64,#{params[:img]}">
</body>
</html>
HTML
end
get '/img' do
image = []
image << 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP'
image << 'C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA'
image << 'AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J'
image << 'REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq'
image << 'ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0'
image << 'vr4MkhoXe0rZigAAAABJRU5ErkJggg=='
content_type 'image/png', :charset => 'utf-8'
Base64.decode64 image.join('')
end
post '/' do
image = IO.read(params[:img][:tempfile])
data = CGI::escape Base64.encode64(image)
"http://localhost:9393/?img=#{data}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment