Sha256: 118619e356081dd609c65a9170ba3079e7b1f3728fc7feb5dc6dc2e7b3bb929d

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

require 'pe_build/version'
require 'pe_build/idempotent'

require 'open-uri'
require 'progressbar'

class PEBuild::Transfer::OpenURI

  class DownloadFailed < Vagrant::Errors::VagrantError
    error_key(:download_failed, 'pebuild.transfer.open_uri')
  end

  # @param uri [URI]    The http(s) URI to the file to copy
  # @param dst [String] The path to destination of the copied file
  def initialize(uri, dst)
    @uri, @dst = uri, dst

    @logger = Log4r::Logger.new('vagrant::pe_build::transfer::open_uri')
  end

  include PEBuild::Idempotent

  def copy
    idempotent(@dst) do
      tmpfile = download_file
      FileUtils.mv(tmpfile, @dst)
    end
  rescue ::OpenURI::HTTPError => e
    raise DownloadFailed, :uri => @uri, :msg => e.message
  end

  HEADERS = {'User-Agent' => "Vagrant/PEBuild (v#{PEBuild::VERSION})"}

  private

  # Open a open-uri file handle for the given URL
  #
  # @return [IO]
  def download_file
    progress = nil

    content_length_proc = lambda do |length|
      if length and length > 0
        progress = ProgressBar.new('Fetching file', length)
        progress.file_transfer_mode
      end
    end

    progress_proc = lambda do |size|
      progress.set(size) if progress
    end

    options = HEADERS.merge({
      :content_length_proc => content_length_proc,
      :progress_proc       => progress_proc,
    })

    @logger.info "Fetching file from #{@uri}"

    @uri.open(options)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vagrant-pe_build-0.4.2 lib/pe_build/transfer/open_uri.rb
vagrant-pe_build-0.4.1 lib/pe_build/transfer/open_uri.rb
vagrant-pe_build-0.4.0 lib/pe_build/transfer/open_uri.rb
vagrant-pe_build-0.3.0 lib/pe_build/transfer/open_uri.rb