klarrimore-routengn 0.23 → 0.24
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/http/multipart.rb +121 -79
- data/lib/routengn.rb +5 -0
- data/lib/routengn/models/record.rb +1 -0
- data/lib/routengn/uploader.rb +5 -3
- metadata +1 -1
data/lib/http/multipart.rb
CHANGED
@@ -1,80 +1,122 @@
|
|
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
|
-
|
78
|
-
|
79
|
-
|
1
|
+
module RouteNGN
|
2
|
+
module HTTP
|
3
|
+
class Multipart
|
4
|
+
|
5
|
+
def initialize( file_names )
|
6
|
+
@file_names = file_names
|
7
|
+
end
|
8
|
+
|
9
|
+
def post( to_url )
|
10
|
+
boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ'
|
11
|
+
|
12
|
+
parts = []
|
13
|
+
streams = []
|
14
|
+
@file_names.each do |param_name, filepath|
|
15
|
+
pos = filepath.rindex('/')
|
16
|
+
filename = filepath[pos + 1, filepath.length - pos]
|
17
|
+
parts << StringPart.new ( "--" + boundary + "\r\n" +
|
18
|
+
"Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" +
|
19
|
+
"Content-Type: video/x-msvideo\r\n\r\n")
|
20
|
+
stream = File.open(filepath, "rb")
|
21
|
+
streams << stream
|
22
|
+
parts << StreamPart.new (stream, File.size(filepath))
|
23
|
+
end
|
24
|
+
parts << StringPart.new ( "\r\n--" + boundary + "--\r\n" )
|
25
|
+
|
26
|
+
post_stream = MultipartStream.new( parts )
|
27
|
+
|
28
|
+
url = URI.parse( to_url )
|
29
|
+
req = Net::HTTP::Post.new(url.path)
|
30
|
+
req.content_length = post_stream.size
|
31
|
+
req.content_type = 'multipart/form-data; boundary=' + boundary
|
32
|
+
req.body_stream = post_stream
|
33
|
+
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
|
34
|
+
|
35
|
+
streams.each do |stream|
|
36
|
+
stream.close();
|
37
|
+
end
|
38
|
+
|
39
|
+
res
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
class StreamPart
|
45
|
+
def initialize( stream, size )
|
46
|
+
@stream, @size = stream, size
|
47
|
+
end
|
48
|
+
|
49
|
+
def size
|
50
|
+
@size
|
51
|
+
end
|
52
|
+
|
53
|
+
def read ( offset, how_much )
|
54
|
+
@stream.read ( how_much )
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class StringPart
|
59
|
+
def initialize ( str )
|
60
|
+
@str = str
|
61
|
+
end
|
62
|
+
|
63
|
+
def size
|
64
|
+
@str.length
|
65
|
+
end
|
66
|
+
|
67
|
+
def read ( offset, how_much )
|
68
|
+
@str[offset, how_much]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class MultipartStream
|
73
|
+
def initialize( parts )
|
74
|
+
@parts = parts
|
75
|
+
@part_no = 0;
|
76
|
+
@part_offset = 0;
|
77
|
+
end
|
78
|
+
|
79
|
+
def size
|
80
|
+
total = 0
|
81
|
+
@parts.each do |part|
|
82
|
+
total += part.size
|
83
|
+
end
|
84
|
+
total
|
85
|
+
end
|
86
|
+
|
87
|
+
def read ( how_much )
|
88
|
+
|
89
|
+
if @part_no >= @parts.size
|
90
|
+
return nil;
|
91
|
+
end
|
92
|
+
|
93
|
+
how_much_current_part = @parts[@part_no].size - @part_offset
|
94
|
+
|
95
|
+
how_much_current_part = if how_much_current_part > how_much
|
96
|
+
how_much
|
97
|
+
else
|
98
|
+
how_much_current_part
|
99
|
+
end
|
100
|
+
|
101
|
+
how_much_next_part = how_much - how_much_current_part
|
102
|
+
|
103
|
+
current_part = @parts[@part_no].read(@part_offset, how_much_current_part )
|
104
|
+
|
105
|
+
if how_much_next_part > 0
|
106
|
+
@part_no += 1
|
107
|
+
@part_offset = 0
|
108
|
+
next_part = read ( how_much_next_part )
|
109
|
+
current_part + if next_part
|
110
|
+
next_part
|
111
|
+
else
|
112
|
+
''
|
113
|
+
end
|
114
|
+
else
|
115
|
+
@part_offset += how_much_current_part
|
116
|
+
current_part
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
80
121
|
end
|
122
|
+
|
data/lib/routengn.rb
CHANGED
@@ -62,6 +62,11 @@ module RouteNGN
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
define_method :upload do |*args|
|
66
|
+
uri, data, headers = args
|
67
|
+
raw = @connection.access_token.post uri, data, headers
|
68
|
+
end
|
69
|
+
|
65
70
|
private
|
66
71
|
def check_connection!
|
67
72
|
raise ConnectionException.new unless @connection
|
data/lib/routengn/uploader.rb
CHANGED
@@ -8,9 +8,11 @@ module RouteNGN
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module ClassMethods
|
11
|
-
def upload
|
12
|
-
|
13
|
-
puts
|
11
|
+
def upload(file, params = {})
|
12
|
+
data, headers = Multipart::Post.prepare_query("title" => 'title', "uploaded_data" => File.new(file))
|
13
|
+
puts data.inspect
|
14
|
+
puts headers.inspect
|
15
|
+
RouteNGN.connection.access_token.post("/upload#{base_url}", data, headers)
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|