From 9e22a6da9f26f46f27b0eb535bfb887db13efa83 Mon Sep 17 00:00:00 2001 From: "William T. Nelson" <35801+wtn@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:11:47 -0500 Subject: [PATCH] Size file parts from the IO before falling back to its path. Co-Authored-By: Claude Opus 5.5 --- lib/multipart/post/parts.rb | 13 ++++- lib/multipart/post/upload_io.rb | 12 ++--- spec/multipart/post/parts_spec.rb | 79 +++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 7 deletions(-) diff --git a/lib/multipart/post/parts.rb b/lib/multipart/post/parts.rb index 059143a..a672ba3 100644 --- a/lib/multipart/post/parts.rb +++ b/lib/multipart/post/parts.rb @@ -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" diff --git a/lib/multipart/post/upload_io.rb b/lib/multipart/post/upload_io.rb index 181374d..2d0a3e2 100644 --- a/lib/multipart/post/upload_io.rb +++ b/lib/multipart/post/upload_io.rb @@ -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 @@ -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 diff --git a/spec/multipart/post/parts_spec.rb b/spec/multipart/post/parts_spec.rb index e5dcaac..2f5efcc 100644 --- a/spec/multipart/post/parts_spec.rb +++ b/spec/multipart/post/parts_spec.rb @@ -14,6 +14,7 @@ require 'multipart/post/composite_read_io' require 'stringio' require 'tempfile' +require 'pathname' module AssertPartLength def assert_part_length(part) @@ -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