Guest User

Untitled

a guest
Feb 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. class Params
  2.  
  3. attr_reader :data, :content_type, :boundary
  4.  
  5. def initialize(params)
  6. @data = ''
  7. @content_type = 'application/x-www-form-urlencoded'
  8. @boundary = MD5.md5('duckies').to_s
  9. parse(params)
  10. end
  11.  
  12. def parse(params)
  13. @data = params.collect { |k, v|
  14. k = k.to_s
  15. if v.respond_to?(:read)
  16. q = file_to_multipart(k, v.path, v.read)
  17. v.close
  18. else
  19. q = to_multipart(k, v)
  20. end
  21. "--" + @boundary + "\r\n" + q
  22. }.join("") + '--' + @boundary + "--"
  23. @content_type = "multipart/form-data, boundary=" + @boundary + " "
  24. end
  25.  
  26. protected
  27.  
  28. # These methods don't use CGI::escape intentionally
  29.  
  30. def to_multipart(name, value)
  31. return "Content-Disposition: form-data; name=\"#{name}\"\r\n\r\n#{value}\r\n"
  32. end
  33.  
  34. # only does JPEGS!
  35. def file_to_multipart(name, file, content)
  36. return "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{file}\"\r\n" +
  37. "Content-Transfer-Encoding: binary\r\n" +
  38. "Content-Type: image/jpeg\r\n\r\n" + content + "\r\n"
  39. end
  40.  
  41. end
Add Comment
Please, Sign In to add comment