Class: Proxy::Omaha::HttpDownload

Inherits:
Object
  • Object
show all
Includes:
Log, HttpShared
Defined in:
lib/smart_proxy_omaha/http_download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HttpShared

#connection_factory

Constructor Details

#initialize(src, dst) ⇒ HttpDownload

Returns a new instance of HttpDownload.



13
14
15
16
17
# File 'lib/smart_proxy_omaha/http_download.rb', line 13

def initialize(src, dst)
  @src = src
  @dst = dst
  @tmp = Tempfile.new('download', File.dirname(dst))
end

Instance Attribute Details

#dstObject

Returns the value of attribute dst.



11
12
13
# File 'lib/smart_proxy_omaha/http_download.rb', line 11

def dst
  @dst
end

#http_responseObject

Returns the value of attribute http_response.



11
12
13
# File 'lib/smart_proxy_omaha/http_download.rb', line 11

def http_response
  @http_response
end

#resultObject

Returns the value of attribute result.



11
12
13
# File 'lib/smart_proxy_omaha/http_download.rb', line 11

def result
  @result
end

#srcObject

Returns the value of attribute src.



11
12
13
# File 'lib/smart_proxy_omaha/http_download.rb', line 11

def src
  @src
end

#tmpObject

Returns the value of attribute tmp.



11
12
13
# File 'lib/smart_proxy_omaha/http_download.rb', line 11

def tmp
  @tmp
end

Instance Method Details

#finishObject



57
58
59
60
# File 'lib/smart_proxy_omaha/http_download.rb', line 57

def finish
  File.rename(tmp, dst)
  true
end

#joinObject



49
50
51
# File 'lib/smart_proxy_omaha/http_download.rb', line 49

def join
  @task.join
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/smart_proxy_omaha/http_download.rb', line 27

def run
  logger.info "#{filename}: Downloading #{src} to #{dst}."
  unless download
    logger.error "#{filename} failed to download."
    return false
  end
  logger.info "#{filename}: Finished downloading #{dst}."
  unless valid?
    logger.error "#{filename} is not valid. Deleting corrupt file."
    File.unlink(tmp)
    return false
  end
  # no DIGESTS file is provided for update.gz
  # so we need to generate our own based on the
  # http headers
  write_digest if filename == 'update.gz'
  finish
ensure
  tmp.unlink
  true
end

#startObject



19
20
21
22
23
24
25
# File 'lib/smart_proxy_omaha/http_download.rb', line 19

def start
  @task = Thread.new do
    @result = run
  end
  @task.abort_on_exception = true
  @task
end

#valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/smart_proxy_omaha/http_download.rb', line 53

def valid?
  verifier.valid?
end

#write_digestObject



62
63
64
65
# File 'lib/smart_proxy_omaha/http_download.rb', line 62

def write_digest
  hexdigest = Digest.hexencode(Base64.decode64(verifier.local_md5))
  File.open("#{dst}.DIGESTS", 'w') { |file| file.write("#{hexdigest}  #{filename}\n") }
end