Class: Backup::Backblaze::UploadFile

Inherits:
Object
  • Object
show all
Extended by:
ApiImporter
Defined in:
lib/backup/backblaze/upload_file.rb

Overview

calculates sha1 and uploads file Of course, this entire class is an atomic failure, because the underlying file could change at any point.

dst can contain / for namespaces

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApiImporter

import_endpoint

Constructor Details

#initialize(account:, src:, bucket_id:, dst:, url_token: nil, content_type: nil) ⇒ UploadFile

Returns a new instance of UploadFile.



13
14
15
16
17
18
19
20
# File 'lib/backup/backblaze/upload_file.rb', line 13

def initialize account:, src:, bucket_id:, dst:, url_token: nil, content_type: nil
  @account = 
  @src = src
  @dst = dst
  @content_type = content_type
  @bucket_id = bucket_id
  @url_token = url_token
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



22
23
24
# File 'lib/backup/backblaze/upload_file.rb', line 22

def 
  @account
end

#bucket_idObject (readonly)

Returns the value of attribute bucket_id.



22
23
24
# File 'lib/backup/backblaze/upload_file.rb', line 22

def bucket_id
  @bucket_id
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



22
23
24
# File 'lib/backup/backblaze/upload_file.rb', line 22

def content_type
  @content_type
end

#dstObject (readonly)

Returns the value of attribute dst.



22
23
24
# File 'lib/backup/backblaze/upload_file.rb', line 22

def dst
  @dst
end

#srcObject (readonly)

Returns the value of attribute src.



22
23
24
# File 'lib/backup/backblaze/upload_file.rb', line 22

def src
  @src
end

Instance Method Details

#b2_authorize_account(retries:, backoff:) ⇒ Object

needed for retry logic



72
73
74
# File 'lib/backup/backblaze/upload_file.rb', line 72

def (retries:, backoff:)
  . retries: retries, backoff: backoff
end

#callObject



93
94
95
96
97
# File 'lib/backup/backblaze/upload_file.rb', line 93

def call
  Backup::Logger.info "uploading '#{dst}' of #{src.size}"
  url_token # not necessary, but makes the flow of control more obvious in the logs
  b2_upload_file
end

#content_dispositionObject

No idea what has to be in here



47
48
# File 'lib/backup/backblaze/upload_file.rb', line 47

def content_disposition
end

#content_lengthObject



50
51
52
# File 'lib/backup/backblaze/upload_file.rb', line 50

def content_length
  File.size src
end

#headersObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/backup/backblaze/upload_file.rb', line 28

def headers
  # headers all have to be strings, otherwise excon & Net::HTTP choke :-|
  {
    'X-Bz-File-Name'                     => (URI.encode dst.encode 'UTF-8'),
    'X-Bz-Content-Sha1'                  => sha1_digest,
    'Content-Length'                     => content_length.to_s,
    'Content-Type'                       => content_type,

    # optional
    'X-Bz-Info-src_last_modified_millis' => last_modified_millis.to_s,
    'X-Bz-Info-b2-content-disposition'   => content_disposition,
  }.merge(TEST_HEADERS).select{|k,v| v}
end

#last_modified_millisObject



62
63
64
65
66
67
# File 'lib/backup/backblaze/upload_file.rb', line 62

def last_modified_millis
  @last_modified_millis ||= begin
    time = File.lstat(src).mtime
    time.tv_sec * 1000 + time.tv_usec / 1000
  end
end

#sha1Object



54
55
56
# File 'lib/backup/backblaze/upload_file.rb', line 54

def sha1
  @sha1 = Digest::SHA1.file src
end

#sha1_digestObject



58
59
60
# File 'lib/backup/backblaze/upload_file.rb', line 58

def sha1_digest
  @sha1_digest = sha1.hexdigest
end

#url_tokenObject



24
25
26
# File 'lib/backup/backblaze/upload_file.rb', line 24

def url_token
  @url_token or b2_get_upload_url
end