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
116
117
# 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 = next_blob_size
  self.current_byte_position = current_byte_position
  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



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

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)


132
133
134
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 132

def complete?
  current_byte_position >= file_size
end

#file_sizeObject



119
120
121
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 119

def file_size
  ::File.size(source_file)
end

#next_blob_bytesObject



147
148
149
# File 'lib/etna/clients/metis/workflows/metis_upload_workflow.rb', line 147

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

#next_blob_hashObject



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

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



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

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