multipart-post 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/composite_io.rb +13 -6
- data/lib/multipartable.rb +2 -1
- data/lib/parts.rb +11 -4
- metadata +3 -3
data/lib/composite_io.rb
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# crio = CompositeReadIO.new(StringIO.new('one'), StringIO.new('two'), StringIO.new('three'))
|
13
13
|
# puts crio.read # => "onetwothree"
|
14
|
-
#
|
14
|
+
#
|
15
15
|
class CompositeReadIO
|
16
16
|
# Create a new composite-read IO from the arguments, all of which should
|
17
17
|
# respond to #read in a manner consistent with IO.
|
@@ -54,15 +54,21 @@ class UploadIO
|
|
54
54
|
# Net::HTTP::Post::Multipart.
|
55
55
|
#
|
56
56
|
# Can take two forms. The first accepts a filename and content type, and
|
57
|
-
# opens the file for reading (to be closed by finalizer).
|
58
|
-
#
|
59
|
-
#
|
57
|
+
# opens the file for reading (to be closed by finalizer).
|
58
|
+
#
|
59
|
+
# The second accepts an already-open IO, but also requires a third argument,
|
60
|
+
# the filename from which it was opened (particularly useful/recommended if
|
61
|
+
# uploading directly from a form in a framework, which often save the file to
|
62
|
+
# an arbitrarily named RackMultipart file in /tmp).
|
63
|
+
#
|
64
|
+
# Usage:
|
60
65
|
#
|
61
66
|
# UploadIO.new("file.txt", "text/plain")
|
62
67
|
# UploadIO.new(file_io, "text/plain", "file.txt")
|
63
|
-
|
68
|
+
#
|
69
|
+
attr_reader :content_type, :original_filename, :local_path, :io, :opts
|
64
70
|
|
65
|
-
def initialize(filename_or_io, content_type, filename = nil)
|
71
|
+
def initialize(filename_or_io, content_type, filename = nil, opts = {})
|
66
72
|
io = filename_or_io
|
67
73
|
local_path = ""
|
68
74
|
if io.respond_to? :read
|
@@ -79,6 +85,7 @@ class UploadIO
|
|
79
85
|
@original_filename = File.basename(filename)
|
80
86
|
@local_path = local_path
|
81
87
|
@io = io
|
88
|
+
@opts = opts
|
82
89
|
end
|
83
90
|
|
84
91
|
def self.convert!(io, content_type, original_filename, local_path)
|
data/lib/multipartable.rb
CHANGED
@@ -6,7 +6,8 @@ require 'parts'
|
|
6
6
|
parts = params.map {|k,v| Parts::Part.new(boundary, k, v)}
|
7
7
|
parts << Parts::EpiloguePart.new(boundary)
|
8
8
|
ios = parts.map{|p| p.to_io }
|
9
|
-
self.set_content_type("multipart/form-data",
|
9
|
+
self.set_content_type(headers["Content-Type"] || "multipart/form-data",
|
10
|
+
{ "boundary" => boundary })
|
10
11
|
self.content_length = parts.inject(0) {|sum,i| sum + i.length }
|
11
12
|
self.body_stream = CompositeReadIO.new(*ios)
|
12
13
|
end
|
data/lib/parts.rb
CHANGED
@@ -39,19 +39,26 @@ module Parts
|
|
39
39
|
attr_reader :length
|
40
40
|
def initialize(boundary, name, io)
|
41
41
|
file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path)
|
42
|
-
@head = build_head(boundary, name, io.original_filename, io.content_type, file_length
|
42
|
+
@head = build_head(boundary, name, io.original_filename, io.content_type, file_length,
|
43
|
+
io.respond_to?(:opts) ? io.opts : {})
|
43
44
|
@foot = "\r\n"
|
44
45
|
@length = @head.length + file_length + @foot.length
|
45
46
|
@io = CompositeReadIO.new(StringIO.new(@head), io, StringIO.new(@foot))
|
46
47
|
end
|
47
48
|
|
48
|
-
def build_head(boundary, name, filename, type, content_len)
|
49
|
+
def build_head(boundary, name, filename, type, content_len, opts = {})
|
50
|
+
trans_encoding = opts["Content-Transfer-Encoding"] || "binary"
|
51
|
+
content_disposition = opts["Content-Disposition"] || "form-data"
|
52
|
+
|
49
53
|
part = ''
|
50
54
|
part << "--#{boundary}\r\n"
|
51
|
-
part << "Content-Disposition:
|
55
|
+
part << "Content-Disposition: #{content_disposition}; name=\"#{name.to_s}\"; filename=\"#{filename}\"\r\n"
|
52
56
|
part << "Content-Length: #{content_len}\r\n"
|
57
|
+
if content_id = opts["Content-ID"]
|
58
|
+
part << "Content-ID: #{content_id}\r\n"
|
59
|
+
end
|
53
60
|
part << "Content-Type: #{type}\r\n"
|
54
|
-
part << "Content-Transfer-Encoding:
|
61
|
+
part << "Content-Transfer-Encoding: #{trans_encoding}\r\n"
|
55
62
|
part << "\r\n"
|
56
63
|
end
|
57
64
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: multipart-post
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.1.
|
5
|
+
version: 1.1.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nick Sieger
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-07-25 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: "Use with Net::HTTP to do multipart form posts. IO values that have #content_type, #original_filename, and #local_path will be posted as a binary file."
|
@@ -47,7 +47,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
47
|
requirements:
|
48
48
|
- - ">="
|
49
49
|
- !ruby/object:Gem::Version
|
50
|
-
hash:
|
50
|
+
hash: 1793835851156969378
|
51
51
|
segments:
|
52
52
|
- 0
|
53
53
|
version: "0"
|