def download( url, filepath, checksum='', est_size=nil )
checksum = checksum.to_s.strip
est_size = nil if est_size == 0
download_complete = nil
if interface
interface.preparing_to_download( File.basename( filepath ), url, est_size )
end
progress_total = est_size ? est_size : 100000000
pbar = Console::ProgressBar.new( "Status", progress_total, STDOUT )
pbar.bar_mark = "="
pbar.format = "%-6s %3d%% %s %s"
pbar.file_transfer_mode if est_size
progress_proc = proc { |posit| pbar.set(posit) }
STDOUT.sync = true
begin
local_file = File.open( filepath, 'w' )
remote_file = open( url, :progress_proc => progress_proc )
local_file << remote_file.read
rescue
pbar.halt
download_complete = nil
raise
else
pbar.finish
download_complete = filepath
ensure
remote_file.close unless remote_file.nil?
local_file.close unless local_file.nil?
STDOUT.sync = false
end
unless checksum.empty?
raise ChecksumError if compute_checksum(filepath) != checksum
end
if interface
if checksum.empty?
interface.lacks_checksum( compute_checksum(filepath), :md5 )
end
unless est_size
interface.lacks_size( File.size(filepath) )
end
end
if download_complete
if interface
interface.downloaded( filepath )
end
end
return download_complete
end