Sha256: a4e649e6e489e563c62a5ffe903e5d76fffdb84f8653f99f34f8063864cd5863

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'open-uri'
require 'net/http'
require 'tempfile'
require 'fileutils'

module Reaver
  extend self

  def download(url, name)
    dest = name
    url = URI(url)
    raise Error, "url was invalid" if !url.respond_to?(:open)

    options = {}
    options['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6556.192 Safari/537.36'

    downloaded_file = ''
    Whirly.start do
      Whirly.status = "downloading #{dest}"
      downloaded_file = URI.open(url, options)

      # open-uri will return a StringIO instead of a Tempfile if the filesize
      # is less than 10 KB, so we patch this behaviour by converting it into
      # a Tempfile.
      if downloaded_file.is_a?(StringIO)
        tempfile = Tempfile.new('open-uri', binmode: true)
        IO.copy_stream(downloaded_file, tempfile.path)
        downloaded_file = tempfile
        FileUtils.mv downloaded_file.path, dest
      else
        IO.copy_stream(downloaded_file, dest)
      end

      downloaded_file
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reaver-0.16.0 lib/reaver/download.rb