multipart-post icon indicating copy to clipboard operation
multipart-post copied to clipboard

Recognize #size on an IO, not just #length?

Open jrochkind opened this issue 7 months ago • 1 comments

Ruby's lack of clear standard interface for IO bytes again.

I am trying to use with a non-File IO, specifically a [Down](https://github.com/janko/down)::ChunkedIO.

It has a #size method to return bytesize, but not a #length method.

Here in this gem, we look for #length but not #size, and if not found assume it's something (not sure what this was targetting) with a local_path method and try to use File.size to look up it's path.

But a Down::ChunkedIO does not have a local_path either, so of course we raise.

StringIO, which was perhaps being targetted, has a #length as well as a #size. (I think they are synonyms, although not entirely sure if length can depend on encoding?)

Any chance of checking for size in addition to length here, which will, I have tested, make it work with a DownIO::Chunked obj too.

i have also filed an issue on Down asking if they would consider implemetning #length. Ruby's lack of clear established standard contract/apI for non-File IO leaves us guessing how to make things compatible! But in this case, it looks to me like multipart-post will work fine with Down::ChunkedIO if only this simple issue were resolved.

jrochkind avatar Jun 10 '25 16:06 jrochkind

file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path)

Looks pretty janky to me. IOW, I'm open to something better.

ioquatix avatar Jun 14 '25 04:06 ioquatix