forked from sfu/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipart.rb
77 lines (72 loc) · 2.57 KB
/
multipart.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Multipart
# From: http://deftcode.com/code/flickr_upload/multipartpost.rb
## Helper class to prepare an HTTP POST request with a file upload
## Mostly taken from
#http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/113774
### WAS:
## Anything that's broken and wrong probably the fault of Bill Stilwell
### NOW:
## Everything wrong is due to [email protected]
require 'rubygems'
require 'mime/types'
require 'net/http'
require 'cgi'
class Param
attr_accessor :k, :v
def initialize( k, v )
@k = k
@v = v
end
def to_multipart
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
# Don't escape mine...
return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n"
end
end
class FileParam
attr_accessor :k, :filename, :content
def initialize( k, filename, content )
@k = k
@filename = filename || "file.csv"
@content = content
end
def to_multipart
#return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n "
# Don't escape mine
return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename).first}\r\n\r\n" + content + "\r\n"
end
end
class MultipartPost
BOUNDARY = AutoHandle.generate('canvas-rules', 15)
HEADER = {"Content-type" => "multipart/form-data, boundary=" + BOUNDARY}
def prepare_query (params, field_priority=[])
fp = []
creds = params.delete :basic_auth
if creds
require 'base64'
puts creds[:username]
puts creds[:password]
puts Base64.encode64("#{creds[:username]}:#{creds[:password]}")
end
def file_param(k, v)
file_data = v.read rescue nil
if file_data
FileParam.new(k, v.path, file_data)
else
Param.new(k,v)
end
end
completed_fields = {}
field_priority.each do |k|
if params.has_key?(k) && !completed_fields.has_key?(k)
fp.push(file_param(k, params[k]))
completed_fields[k] = true
end
end
params.each {|k,v| fp.push(file_param(k, v)) unless completed_fields.has_key?(k) }
query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
return query, HEADER
end
end
end