Class: Etna::Clients::Metis::MetisUploadWorkflow::Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/clients/metis/workflows/metis_upload_workflow.rb

Direct Known Subclasses

StreamingIOUpload

Constant Summary collapse

INITIAL_BLOB_SIZE =
2 ** 10
MAX_BLOB_SIZE =
2 ** 22
ZERO_HASH =
'd41d8cd98f00b204e9800998ecf8427e'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file: nil, next_blob_size: nil, current_byte_position: nil) ⇒ Upload

Returns a new instance of Upload.



111
112
113
114
115
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 111

def initialize(source_file: nil, next_blob_size: nil, current_byte_position: nil)
  self.source_file = source_file
  self.next_blob_size = [file_size, INITIAL_BLOB_SIZE].min
  self.current_byte_position = 0
end

Instance Attribute Details

#current_byte_positionObject

Returns the value of attribute current_byte_position.



109
110
111
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 109

def current_byte_position
  @current_byte_position
end

#next_blob_sizeObject

Returns the value of attribute next_blob_size.



109
110
111
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 109

def next_blob_size
  @next_blob_size
end

#source_fileObject

Returns the value of attribute source_file.



109
110
111
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 109

def source_file
  @source_file
end

Instance Method Details

#advance_position!Object



121
122
123
124
125
126
127
128
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 121

def advance_position!
  self.current_byte_position = self.current_byte_position + self.next_blob_size
  self.next_blob_size = [
                            MAX_BLOB_SIZE,
                            # in fact we should stop when we hit the end of the file
                            file_size - current_byte_position
                        ].min
end

#complete?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 130

def complete?
  current_byte_position >= file_size
end

#file_sizeObject



117
118
119
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 117

def file_size
  ::File.size(source_file)
end

#next_blob_bytesObject



145
146
147
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 145

def next_blob_bytes
  IO.binread(source_file, next_blob_size, current_byte_position)
end

#next_blob_hashObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 134

def next_blob_hash
  bytes = next_blob_bytes
  if bytes.empty?
    return ZERO_HASH
  end

  $digest_mutx.synchronize do
    return Digest::MD5.hexdigest(bytes)
  end
end

#resume_from!(upload_response) ⇒ Object



149
150
151
152
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 149

def resume_from!(upload_response)
  self.current_byte_position = upload_response.current_byte_position
  self.next_blob_size = upload_response.next_blob_size
end