class FasterS3::Part

Attributes

extra_bytes[RW]
file_path[RW]
index[RW]
part_length[RW]

Public Class Methods

new(file_path, index, part_length, extra_bytes) click to toggle source
# File lib/faster_s3/part.rb, line 4
def initialize(file_path, index, part_length, extra_bytes)
  self.file_path = file_path
  self.index = index
  self.part_length = part_length
  self.extra_bytes = extra_bytes
end

Public Instance Methods

byte_range() click to toggle source
# File lib/faster_s3/part.rb, line 15
def byte_range
  start = index * part_length
  end_pos = start + part_length + extra_bytes

  (start + existing_size)...end_pos
end
download(s3_object) click to toggle source
# File lib/faster_s3/part.rb, line 26
def download(s3_object)
  return if byte_range.min == byte_range.max
  File.open(part_path, 'ab') do |file|
    s3_object.read(range: byte_range) do |chunk|
      file.write(chunk)
    end
  end
end
existing_size() click to toggle source
# File lib/faster_s3/part.rb, line 22
def existing_size
  File.exists?(part_path) ? File.size(part_path) : 0
end
part_path() click to toggle source
# File lib/faster_s3/part.rb, line 11
def part_path
  "#{file_path}.part.#{index}"
end