class FileFM::StreamingUploaderMultipart
Public Instance Methods
put(to_url, params = {}, &block)
click to toggle source
# File lib/filefm/streaming_uploader.rb, line 21 def put(to_url, params = {}, &block) boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ' parts = [] content_file = nil params.each do |key, value| if value.kind_of?(File) filepath = value.path parts << StreamPart.new(value, File.size(filepath)) end end unless params.nil? || params.empty? params.each do |key, value| if value.kind_of?(File) content_file = value filepath = value.path filename = File.basename(filepath) parts << StringPart.new( "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + key.to_s + "\"; filename=\"" + filename + "\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n") parts << StreamPart.new(value, File.size(filepath)) parts << StringPart.new("\r\n") else parts << StringPart.new( "--" + boundary + "\r\n" + "Content-Disposition: form-data; Content-Type:application/json; name=\"" + key.to_s + "\"\r\n\r\n") parts << StringPart.new(value.to_s + "\r\n") end end parts << StringPart.new("--" + boundary + "--\r\n") end body_stream = MultipartStream.new(parts, block) url = URI.parse(to_url) headers = { 'Content-Length' => body_stream.size.to_s, 'Content-Type' => 'application/json' }.merge(params[:headers]) req = Net::HTTP::Put.new(url.path, headers) Log.debug "HEADERS: " + headers.inspect Log.debug "HOST: " + url.host Log.debug "PORT: " + url.port.to_s Log.debug "PATH: " + url.path req.content_length = body_stream.size req.content_type = 'multipart/form-data; boundary=' + boundary unless parts.empty? req.body_stream = body_stream http = Net::HTTP.new(url.host, url.port) http.use_ssl = true if url.scheme == "https" res = http.request(req) res end