Class: VagrantVbguest::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vbguest/download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, options = nil) ⇒ Download

Returns a new instance of Download.



5
6
7
8
9
10
11
12
13
# File 'lib/vagrant-vbguest/download.rb', line 5

def initialize(source, destination, options=nil)
  @downloader = nil
  @source = source
  @destination = destination
  if File.directory?(destination)
    @destination = File.join(destination, "vbguest_download_#{Time.now.to_i.to_s}")
  end
  @ui = options[:ui]
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



3
4
5
# File 'lib/vagrant-vbguest/download.rb', line 3

def destination
  @destination
end

#downloaderObject (readonly)

Returns the value of attribute downloader.



3
4
5
# File 'lib/vagrant-vbguest/download.rb', line 3

def downloader
  @downloader
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/vagrant-vbguest/download.rb', line 3

def source
  @source
end

Instance Method Details

#cleanupObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-vbguest/download.rb', line 23

def cleanup
  if destination && File.exist?(destination)
    @ui.info I18n.t("vagrant_vbguest.download.cleaning")
    # Unlinking the downloaded file might crash on Windows
    # see: https://github.com/dotless-de/vagrant-vbguest/issues/189
    begin
      # Even if delete failed on Windows, we still can clean this file to save disk space
      File.open(destination,'wb') do |f|
        f.write('')
        f.close()
      end
      File.unlink(destination)
    rescue Errno::EACCES => e
      @ui.warn I18n.t("vagrant_vbguest.download.cleaning_failed", message: e.message, destination: destination)
    end
  end
end

#download!Object



15
16
17
18
19
20
21
# File 'lib/vagrant-vbguest/download.rb', line 15

def download!
  downloader_options = {}
  downloader_options[:ui] = @ui
  @ui.info(I18n.t("vagrant_vbguest.download.started", :source => @source))
  @downloader = Vagrant::Util::Downloader.new(@source, @destination, downloader_options)
  @downloader.download!
end