Advertisement
Guest User

Untitled

a guest
Aug 25th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. Content-Type: multipart/form-data; boundary=301405
  2.  
  3. RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')
  4.  
  5. RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb'), :content_type => 'multipart/related'
  6.  
  7. files = { File.new("myfile1","rb"), File.new("myfile2","rb")}
  8. mpost = Mutipart("main", "<my xml main part content which refers to file names>",files )
  9. mpost.post("our_url","post")
  10.  
  11. require 'net/http'
  12. require 'uri'
  13. require 'pp'
  14. require 'mime/types'
  15.  
  16. class Multipart
  17.  
  18. def initialize( main_part_id, main_part_content, file_names )
  19. @file_names = file_names
  20. @main_part_id = main_part_id
  21. @main_part_content = main_part_content
  22. end
  23.  
  24. def post( to_url, method = :post )
  25. boundary = "###-------#{Time.new}-----####"
  26.  
  27. parts = []
  28. streams = []
  29. # write main part first
  30. parts << StringPart.new( "--" + boundary + "rn")
  31. parts << StringPart.new("Content-Disposition: name="#{@main_part_id}";"rn" +
  32. "Content-ID: #{@main_part_id}rnrn"+
  33. "Content-Type: application/xmlrnrn" +
  34. @main_part_content + "rnrn")
  35. parts << StringPart.new( "rn--" + boundary + "rn")
  36.  
  37. @file_names.each do |param_name, filestream|
  38. raise 'mutlipartsend: empty file object' if filestream.blank?
  39.  
  40. filename= filestream.respond_to?(:original_path) ? filestream.original_path : filestream.path
  41. ctype = filestream.respond_to?(:content_type) ? filestream.content_type: nil
  42. fsize = filestream.respond_to?(:lstat) ? filestream.lstat.size : filestream.size
  43.  
  44.  
  45. if !ctype
  46. begin
  47. pos = filename.rindex('/') # if filename is a path
  48. fname = filename[pos + 1, filename.length - pos]
  49. mm = MIME::Types.type_for(fname)
  50. ctype = mm.first.content_type if !mm.blank?
  51. rescue Exception => e
  52. p e.message
  53. end
  54. end
  55. if !ctype
  56. ctype= 'application/binary'
  57. p "mutlipartsend: failed to determine contenttype for #{filename}. using application/binary"
  58. end
  59.  
  60.  
  61. parts << StringPart.new("Content-Disposition: name="" + param_name.to_s + ""; filename="" + filename + ""rn" +
  62. "Content-Type: #{ctype}rnrn")
  63. #"Content-Type: application/binaryrnrn")
  64. begin
  65. stream = File.open(filestream.path,"rb")
  66. streams << stream
  67. parts << StreamPart.new(stream, fsize)
  68. parts << StringPart.new( "rn--" + boundary + "rn" )
  69. rescue Exception => e
  70. p 'failed to load filestream '+ filestream.path
  71. p e.message
  72. raise 'failed to load filestream ' + e.message
  73. end
  74.  
  75. end
  76.  
  77. post_stream = MultipartStream.new( parts )
  78.  
  79. url = URI.parse( to_url )
  80. req = method == :post ? Net::HTTP::Post.new(url.path) : Net::HTTP::Put.new(url.path)
  81. req.content_length = post_stream.size
  82. req.content_type = 'multipart/mixed; boundary=' + boundary
  83. req["myheader1"] = 'header1'
  84. req["myheader2"] = 'header2'
  85.  
  86. req.body_stream = post_stream
  87. res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
  88.  
  89. streams.each do |stream|
  90. stream.close();
  91. end
  92.  
  93. res
  94. end
  95.  
  96. end
  97.  
  98. class StreamPart
  99. def initialize( stream, size )
  100. @stream, @size = stream, size
  101. end
  102.  
  103. def size
  104. @size
  105. end
  106.  
  107. def read( offset, how_much )
  108. @stream.read( how_much )
  109. end
  110. end
  111.  
  112. class StringPart
  113. def initialize ( str )
  114. @str = str
  115. end
  116.  
  117. def size
  118. @str.length
  119. end
  120.  
  121. def read ( offset, how_much )
  122. @str[offset, how_much]
  123. end
  124. end
  125.  
  126. class MultipartStream
  127. def initialize( parts )
  128. @parts = parts
  129. @part_no = 0;
  130. @part_offset = 0;
  131. end
  132.  
  133.  
  134.  
  135. def size
  136. total = 0
  137. @parts.each do |part|
  138. total += part.size
  139. end
  140. total
  141. end
  142.  
  143. def read ( how_much )
  144. if @part_no >= @parts.size
  145. return nil;
  146. end
  147.  
  148. how_much_current_part = @parts[@part_no].size - @part_offset
  149.  
  150. how_much_current_part = if how_much_current_part > how_much
  151. how_much
  152. else
  153. how_much_current_part
  154. end
  155.  
  156. how_much_next_part = how_much - how_much_current_part
  157.  
  158. current_part = @parts[@part_no].read(@part_offset, how_much_current_part )
  159.  
  160. if how_much_next_part > 0
  161. @part_no += 1
  162. @part_changed=true
  163. @part_offset = 0
  164. next_part = read( how_much_next_part )
  165. current_part + if next_part
  166. next_part
  167. else
  168. ''
  169. end
  170. else
  171. @part_offset += how_much_current_part
  172. current_part
  173. end
  174. end
  175.  
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement