2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-18 22:11:42 +03:00
|
|
|
require "fileutils"
|
2018-09-03 19:39:07 +01:00
|
|
|
require "cask/cache"
|
|
|
|
require "cask/quarantine"
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2018-09-06 08:29:14 +02:00
|
|
|
module Cask
|
2020-08-24 22:53:08 +02:00
|
|
|
# A download corresponding to a {Cask}.
|
|
|
|
#
|
|
|
|
# @api private
|
2016-09-24 13:52:43 +02:00
|
|
|
class Download
|
2020-11-19 18:12:16 +01:00
|
|
|
include Context
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
attr_reader :cask
|
|
|
|
|
2020-11-19 18:12:16 +01:00
|
|
|
def initialize(cask, quarantine: nil)
|
2016-09-24 13:52:43 +02:00
|
|
|
@cask = cask
|
2018-08-31 13:16:11 +00:00
|
|
|
@quarantine = quarantine
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
|
2020-11-19 18:12:16 +01:00
|
|
|
def fetch(verify_download_integrity: true)
|
|
|
|
downloaded_path = begin
|
|
|
|
downloader.fetch
|
|
|
|
downloader.cached_location
|
|
|
|
rescue => e
|
|
|
|
error = CaskError.new("Download failed on Cask '#{cask}' with message: #{e}")
|
|
|
|
error.set_backtrace e.backtrace
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
quarantine(downloaded_path)
|
|
|
|
self.verify_download_integrity(downloaded_path) if verify_download_integrity
|
2016-09-24 13:52:43 +02:00
|
|
|
downloaded_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def downloader
|
2018-08-04 10:13:42 +02:00
|
|
|
@downloader ||= begin
|
|
|
|
strategy = DownloadStrategyDetector.detect(cask.url.to_s, cask.url.using)
|
|
|
|
strategy.new(cask.url.to_s, cask.token, cask.version, cache: Cache.path, **cask.url.specs)
|
2016-10-14 20:11:33 +02:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
|
2020-12-04 00:07:02 +01:00
|
|
|
def time_file_size
|
|
|
|
downloader.resolved_time_file_size
|
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def clear_cache
|
2020-11-19 18:12:16 +01:00
|
|
|
downloader.clear_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_download
|
|
|
|
downloader.cached_location
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
|
|
|
|
2020-11-19 18:12:16 +01:00
|
|
|
def verify_download_integrity(fn)
|
|
|
|
if @cask.sha256 == :no_check
|
2020-11-19 19:44:23 +01:00
|
|
|
opoo "No checksum defined for cask '#{@cask}', skipping verification."
|
2020-11-19 18:12:16 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2020-11-19 19:44:23 +01:00
|
|
|
ohai "Verifying checksum for cask '#{@cask}'." if verbose?
|
|
|
|
fn.verify_checksum(@cask.sha256)
|
2020-11-19 18:12:16 +01:00
|
|
|
rescue ChecksumMissingError
|
2020-11-19 19:44:23 +01:00
|
|
|
opoo <<~EOS
|
|
|
|
Cannot verify integrity of '#{fn.basename}'.
|
|
|
|
No checksum was provided for this cask.
|
|
|
|
For your reference, the checksum is:
|
|
|
|
sha256 "#{fn.sha256}"
|
|
|
|
EOS
|
2020-11-19 18:12:16 +01:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2018-08-31 13:16:11 +00:00
|
|
|
|
2020-11-19 18:12:16 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def quarantine(path)
|
2018-09-07 15:37:31 +00:00
|
|
|
return if @quarantine.nil?
|
2018-08-31 13:16:11 +00:00
|
|
|
return unless Quarantine.available?
|
|
|
|
|
2018-09-08 14:00:44 +00:00
|
|
|
if @quarantine
|
2020-11-19 18:12:16 +01:00
|
|
|
Quarantine.cask!(cask: @cask, download_path: path)
|
2018-09-08 14:00:44 +00:00
|
|
|
else
|
2020-11-19 18:12:16 +01:00
|
|
|
Quarantine.release!(download_path: path)
|
2018-09-08 14:00:44 +00:00
|
|
|
end
|
2018-08-31 13:16:11 +00:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|