download_file!

in lib/omnibus/download_helpers.rb [48:118]


      def download_file!(from_url, to_path, download_options = {})
        options = download_options.dup

        options.merge!(download_headers)
        options[:read_timeout] = Omnibus::Config.fetcher_read_timeout

        fetcher_retries ||= Omnibus::Config.fetcher_retries

        reported_total = 0

        
        
        
        
        options[:enable_progress_bar] = true unless options.key?(:enable_progress_bar)
        enable_progress_bar = options.delete(:enable_progress_bar) && Omnibus::Config.fetcher_progress_bar

        if enable_progress_bar
          progress_bar = ProgressBar.create(
            output: $stdout,
            format: "%e %B %p%% (%r KB/sec)",
            rate_scale: ->(rate) { rate / 1024 }
          )

          options[:content_length_proc] = ->(total) do
            reported_total = total
            progress_bar.total = total
          end
          options[:progress_proc] = ->(step) do
            downloaded_amount = reported_total ? [step, reported_total].min : step
            progress_bar.progress = downloaded_amount
          end
        end

        if RUBY_VERSION.to_f < 2.7
          file = open(from_url, options)
        else
          file = URI.open(from_url, options)
        end

        
        debug_data = {
          "content_type": file.content_type,
          "metadata": file.metas,
          "status": file.status,
          "redirected_uri": file.base_uri,
        }

        
        
        file.close
        FileUtils.cp(file.path, to_path)
        file.unlink

        debug_data
      rescue SocketError,
             Errno::ECONNREFUSED,
             Errno::ECONNRESET,
             Errno::ENETUNREACH,
             Timeout::Error,
             OpenURI::HTTPError => e
        if fetcher_retries != 0
          log.info(log_key) { "Retrying failed download due to #{e} (#{fetcher_retries} retries left)..." }
          fetcher_retries -= 1
          retry
        else
          log.error(log_key) { "Download failed - #{e.class}!" }
          raise
        end
      end