Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/multipart/post/parts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ class FilePart
# @param io [IO]
# @param headers [Hash]
def initialize(boundary, name, io, headers = {})
file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path)
file_length = io.length if io.respond_to?(:length)
file_length ||= io.size if io.respond_to?(:size)

if !file_length && io.respond_to?(:local_path)
path = io.local_path
file_length = File.size(path) if path && File.file?(path)
end

unless file_length
raise ArgumentError, "Unable to determine the length of file part #{name.to_s.inspect}!"
end

@head = build_head(boundary, name, io.original_filename, io.content_type, file_length,
io.respond_to?(:opts) ? io.opts.merge(headers) : headers)
@foot = "\r\n"
Expand Down
12 changes: 6 additions & 6 deletions lib/multipart/post/upload_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class UploadIO
# Create an upload IO suitable for including in the params hash of a
# Net::HTTP::Post::Multipart.
#
# Can take two forms. The first accepts a filename and content type, and
# opens the file for reading (to be closed by finalizer).
# Can take two forms. The first accepts a filename (a String or Pathname) and
# content type, and opens the file for reading (to be closed by finalizer).
#
# The second accepts an already-open IO, but also requires a third argument,
# the filename from which it was opened (particularly useful/recommended if
Expand All @@ -26,13 +26,13 @@ class UploadIO
def initialize(filename_or_io, content_type, filename = nil, opts = {})
io = filename_or_io
local_path = ""
if io.respond_to? :read
# in Ruby 1.9.2, StringIOs no longer respond to path
# (since they respond to :length, so we don't need their local path, see parts.rb:41)
# Pathname#read always reads from the start of the file, so open it like a filename.
if io.respond_to?(:read) && !(defined?(::Pathname) && io.is_a?(::Pathname))
# IOs without a path, like StringIO, are sized by FilePart using #length or #size.
local_path = filename_or_io.respond_to?(:path) ? filename_or_io.path : "local.path"
else
io = File.open(filename_or_io)
local_path = filename_or_io
local_path = io.path
end
filename ||= local_path

Expand Down
79 changes: 79 additions & 0 deletions spec/multipart/post/parts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'multipart/post/composite_read_io'
require 'stringio'
require 'tempfile'
require 'pathname'

module AssertPartLength
def assert_part_length(part)
Expand Down Expand Up @@ -82,6 +83,84 @@ def content_type; 'application/data'; end
part = Parts::FilePart.new("boundary", "afile", UploadIO.new(TEMP_FILE, "text/plain"), { "Content-ID" => "id" })
expect(part.to_io.read).to match(/Content-ID: id/)
end

it "test_io_with_only_size" do
io = Class.new do
def initialize(string)
@io = StringIO.new(string)
end

def read(*args)
@io.read(*args)
end

def size
@io.size
end
end.new("1234567890")

assert_part_length Parts::FilePart.new("boundary", "afile", UploadIO.new(io, "text/plain", "afile.txt"))
end

it "test_file_deleted_after_open" do
file = File.open(TEMP_FILE)
File.delete(TEMP_FILE)
assert_part_length Parts::FilePart.new("boundary", "afile", UploadIO.new(file, "text/plain"))
ensure
file&.close
end

it "test_io_with_nil_size_and_path" do
io = Class.new do
attr :path

def initialize(path)
@path = path
@io = StringIO.new(File.read(path))
end

def read(*args)
@io.read(*args)
end

def size
nil
end
end.new(TEMP_FILE)

assert_part_length Parts::FilePart.new("boundary", "afile", UploadIO.new(io, "text/plain"))
end

it "test_pathname" do
io = UploadIO.new(Pathname.new(TEMP_FILE), "text/plain")
part = Parts::FilePart.new("boundary", "afile", io)

body = String.new
while chunk = part.to_io.read(4)
body << chunk
break if body.bytesize > part.length
end

expect(io.original_filename).to be == TEMP_FILE
expect(io.local_path).to be == TEMP_FILE
expect(body.bytesize).to be == part.length
end

it "test_io_with_unknown_size" do
io = Class.new do
def read(*args)
nil
end

def size
nil
end
end.new

expect do
Parts::FilePart.new("boundary", "afile", UploadIO.new(io, "text/plain", "afile.txt"))
end.to raise_error(ArgumentError, /afile/)
end
end

RSpec.describe Parts::ParamPart do
Expand Down