Skip to content

Instantly share code, notes, and snippets.

@krukid
Created March 12, 2012 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krukid/2022252 to your computer and use it in GitHub Desktop.
Save krukid/2022252 to your computer and use it in GitHub Desktop.
em-http-request streaming upload
#
# requires:
# a) run https://gist.github.com/2022231 on localhost:3001
# b) provide a streamed.jpg at script location
#
# result:
# this fails for em-http-request v1.0.1
#
require "rubygems"
require "em-synchrony"
require "em-http"
require "launchy"
@target_host = "localhost"
@target_port = "3001"
@target_path = "/"
@source_image_name = "streamed.jpg"
@result_html_name = "result_em_http.html"
def script_data_path(filename)
File.join(File.expand_path(File.dirname(__FILE__)), filename)
end
EM.run do
file_path = script_data_path(@source_image_name)
puts "Uploading #{File.size(file_path)} bytes..."
# Content-Length is calculated internally and cannot be overridden
request_headers = {"Content-Type" => "image/jpeg", "Accept" => "*/*"}
http = EventMachine::HttpRequest.new("http://#{@target_host}:#{@target_port}").post(
:path => @target_path, :head => request_headers, :file => file_path)
http.callback do |a_http|
puts "DONE: #{a_http.response_header.status}\n"+
"BODY: #{a_http.response.size} bytes\n"+
"See #{@result_html_name}"
result_html_path = script_data_path(@result_html_name)
File.open(result_html_path, "w") do |f|
f.puts(a_http.response)
end
Launchy.open("file://#{result_html_path}")
EM.stop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment