Tempfile#size patch was broken on ruby 3.3, also fix rubocop

This commit is contained in:
Vasily Fedoseyev
2024-05-09 23:17:06 +03:00
parent 109b82ad19
commit b638a435e1

View File

@@ -1,10 +1,18 @@
# frozen_string_literal: true
# Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying # Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
# and Tempfile conversion. # and Tempfile conversion.
module IOStream module IOStream
# Returns a Tempfile containing the contents of the readable object. # Returns a Tempfile containing the contents of the readable object.
def to_tempfile(object) def to_tempfile(object)
return object.to_tempfile if object.respond_to?(:to_tempfile) return object.to_tempfile if object.respond_to?(:to_tempfile)
name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream") name = if object.respond_to?(:original_filename)
object.original_filename
elsif object.respond_to?(:path)
object.path
else
"stream"
end
tempfile = Tempfile.new(["ppc-iostream", File.extname(name)]) tempfile = Tempfile.new(["ppc-iostream", File.extname(name)])
tempfile.binmode tempfile.binmode
stream_to(object, tempfile) stream_to(object, tempfile)
@@ -13,32 +21,15 @@ module IOStream
# Copies one read-able object from one place to another in blocks, obviating the need to load # Copies one read-able object from one place to another in blocks, obviating the need to load
# the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed # the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
# in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination. # in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
def stream_to object, path_or_file, in_blocks_of = 8192 def stream_to(object, path_or_file, in_blocks_of = 8192)
dstio = case path_or_file dstio = case path_or_file
when String then File.new(path_or_file, "wb+") when String then File.new(path_or_file, "wb+")
when IO, Tempfile then path_or_file when IO, Tempfile then path_or_file
end end
buffer = "" buffer = +""
object.rewind object.rewind
while object.read(in_blocks_of, buffer) do dstio.write(buffer) while object.read(in_blocks_of, buffer)
dstio.write(buffer)
end
dstio.rewind dstio.rewind
dstio dstio
end end
end end
# Corrects a bug in Windows when asking for Tempfile size.
if defined?(Tempfile) && RUBY_PLATFORM !~ /java/
class Tempfile
def size
if @tmpfile
@tmpfile.fsync
@tmpfile.flush
@tmpfile.stat.size
else
0
end
end
end
end