class ChefCore::FileFetcher

Public Class Methods

download_file(url, local_path) click to toggle source
# File lib/chef_core/file_fetcher.rb, line 39
def download_file(url, local_path)
  temp_path = "#{local_path}.downloading"
  file = open(temp_path, "wb")
  ChefCore::Log.debug "Downloading: #{temp_path}"
  Net::HTTP.start(url.host) do |http|
    begin
      http.request_get(url.path) do |resp|
        resp.read_body do |segment|
          file.write(segment)
        end
      end
    rescue e
      @error = true
      raise
    ensure
      file.close
      # If any failures occurred, don't risk keeping
      # an incomplete download that we'll see as 'cached'
      if @error
        FileUtils.rm_f(temp_path)
      else
        FileUtils.mv(temp_path, local_path)
      end
    end
  end
end
fetch(cache_path, path) click to toggle source

Simple fetcher of an http(s) url. Returns the local path of the downloaded file.

# File lib/chef_core/file_fetcher.rb, line 26
def fetch(cache_path, path)
  FileUtils.mkdir_p(cache_path)
  url = URI.parse(path)
  name = File.basename(url.path)
  local_path = File.join(cache_path, name)

  # TODO header check for size or checksum?
  return local_path if File.exist?(local_path)

  download_file(url, local_path)
  local_path
end