lib/down.rb in down-1.0.4 vs lib/down.rb in down-1.0.5
- old
+ new
@@ -1,8 +1,9 @@
require "open-uri"
require "tempfile"
require "uri"
+require "fileutils"
module Down
class Error < StandardError; end
class TooLarge < Error; end
class NotFound < Error; end
@@ -31,11 +32,10 @@
# 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
- open_uri_file.delete if open_uri_file.respond_to?(:delete)
downloaded_file.extend DownloadedFile
downloaded_file
rescue => error
@@ -43,11 +43,16 @@
raise Down::NotFound, error.message
end
def copy_to_tempfile(basename, io)
tempfile = Tempfile.new(["down", File.extname(basename)], binmode: true)
- IO.copy_stream(io, tempfile.path)
- io.rewind
+ if io.is_a?(OpenURI::Meta) && io.is_a?(Tempfile)
+ FileUtils.mv io.path, tempfile.path
+ else
+ IO.copy_stream(io, tempfile.path)
+ io.rewind
+ end
+ tempfile.open
tempfile
end
module DownloadedFile
def original_filename