Skip to content
  • Janko Marohnić's avatar
    32b29429
    Restore UploadedFile compatibility with IO.copy_stream · 32b29429
    Janko Marohnić authored
    In https://github.com/rails/rails/pull/28676 the `#to_path` method was
    added to `ActionDispatch::Http::UploadedFile`. This broke usage with
    `IO.copy_stream`:
    
      source = ActionDispatch::Http::UploadedFile.new(...)
      IO.copy_stream(source, destination)
      # ~> TypeError: can't convert ActionDispatch::Http::UploadedFile to IO (ActionDispatch::Http::UploadedFile#to_io gives Tempfile)
    
    Normally `IO.copy_stream` just calls `#read` on the source object.
    However, when `#to_path` is defined, `IO.copy_stream` calls `#to_io` in
    order to retrieve the raw `File` object. In that case it trips up,
    because `ActionDispatch::Http::UploadedFile#to_io` returned a `Tempfile`
    object, which is not an `IO` subclass.
    
    We fix this by having `#to_io` return an actual `File` object.
    32b29429
    Restore UploadedFile compatibility with IO.copy_stream
    Janko Marohnić authored
    In https://github.com/rails/rails/pull/28676 the `#to_path` method was
    added to `ActionDispatch::Http::UploadedFile`. This broke usage with
    `IO.copy_stream`:
    
      source = ActionDispatch::Http::UploadedFile.new(...)
      IO.copy_stream(source, destination)
      # ~> TypeError: can't convert ActionDispatch::Http::UploadedFile to IO (ActionDispatch::Http::UploadedFile#to_io gives Tempfile)
    
    Normally `IO.copy_stream` just calls `#read` on the source object.
    However, when `#to_path` is defined, `IO.copy_stream` calls `#to_io` in
    order to retrieve the raw `File` object. In that case it trips up,
    because `ActionDispatch::Http::UploadedFile#to_io` returned a `Tempfile`
    object, which is not an `IO` subclass.
    
    We fix this by having `#to_io` return an actual `File` object.
Loading