Module: Linecook::Downloader

Included in:
AmiPacker, Image, Packer
Defined in:
lib/linecook-gem/util/downloader.rb

Constant Summary collapse

LOCK_WAIT_TIMEOUT =
180

Instance Method Summary collapse

Instance Method Details

#download(url, path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/linecook-gem/util/downloader.rb', line 12

def download(url, path)
  FileUtils.mkdir_p(File.dirname(path))
  cryptfile = "#{File.basename(path)}-encrypted-#{SecureRandom.hex(4)}"
  File.open(path, 'w') do |f|
    pbar = ProgressBar.create(title: File.basename(path), total: nil)
    IO.copy_stream(open(url,
                        content_length_proc: lambda do|t|
                          pbar.total = t if t && 0 < t
                        end,
                        progress_proc: lambda do|s|
                          pbar.progress = s
                        end), f)
  end
end

#unzip(source, dest: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/linecook-gem/util/downloader.rb', line 27

def unzip(source, dest: nil)
  puts "Extracting #{source}..."
  dest ||= File.dirname(source)
  Zip::File.open(source) do |zip_file|
    zip_file.each do |f|
      file_path = File.join(dest, f.name)
      FileUtils.mkdir_p(File.dirname(file_path))
      zip_file.extract(f, file_path)
    end
  end
end