lib/down.rb in down-1.0.2 vs lib/down.rb in down-1.0.3
- old
+ new
@@ -23,27 +23,28 @@
},
read_timeout: options[:timeout],
redirect: false,
)
- # open-uri will return a StringIO instead of a Tempfile if the filesize
- # is less than 10 KB, so if it happens we convert it back to Tempfile.
- if downloaded_file.is_a?(StringIO)
- stringio = downloaded_file
- downloaded_file = copy_to_tempfile("open-uri", stringio)
- OpenURI::Meta.init downloaded_file, stringio
- end
+ # open-uri will return a StringIO instead of a Tempfile if the filesize is
+ # less than 10 KB, so if it happens we convert it back to Tempfile. We want
+ # to do this with a Tempfile as well, because open-uri doesn't preserve the
+ # file extension, so we want to run it against #copy_to_tempfile which
+ # does.
+ open_uri_file = downloaded_file
+ downloaded_file = copy_to_tempfile(URI(url).path, open_uri_file)
+ OpenURI::Meta.init downloaded_file, open_uri_file
downloaded_file.extend DownloadedFile
downloaded_file
rescue => error
raise if error.is_a?(Down::Error)
raise Down::NotFound, error.message
end
def copy_to_tempfile(basename, io)
- tempfile = Tempfile.new(basename, binmode: true)
+ tempfile = Tempfile.new(["down", File.extname(basename)], binmode: true)
IO.copy_stream(io, tempfile.path)
io.rewind
tempfile
end