class Pantry::Communication::FileService::ReceivingFile

Receiving-side version of UploadInfo Can be configured with a completion block that will be executed once the file has been fully received and checksum verified.

Attributes

checksum[R]
file_size[R]
sender_uuid[RW]
uploaded_path[R]

Location of the tempfile containing the contents of the uploaded file

Public Class Methods

new(file_size, checksum, chunk_size, pipeline_size) click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 80
def initialize(file_size, checksum, chunk_size, pipeline_size)
  super()
  @file_uuid = SecureRandom.uuid
  @file_size = file_size
  @checksum  = checksum

  @chunk_size    = chunk_size
  @pipeline_size = pipeline_size

  @uploaded_file = Tempfile.new(file_uuid)
  @uploaded_path = @uploaded_file.path

  @next_requested_file_offset = 0
  @current_pipeline_size      = 0

  @chunk_count      = (@file_size.to_f / @chunk_size.to_f).ceil
  @requested_chunks = 0
  @received_chunks  = 0
end

Public Instance Methods

chunks_to_fetch(&block) click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 104
def chunks_to_fetch(&block)
  chunks_to_fill_pipeline = [
    (@pipeline_size - @current_pipeline_size),
    @chunk_count - @requested_chunks
  ].min

  chunks_to_fill_pipeline.times do
    block.call(@next_requested_file_offset, @chunk_size)

    @next_requested_file_offset += @chunk_size
    @current_pipeline_size += 1
    @requested_chunks      += 1
  end
end
complete?() click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 145
def complete?
  @uploaded_file.closed?
end
Also aliased as: finished?
finished!() click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 131
def finished!
  @uploaded_file.close

  if @completion_block && valid?
    begin
      @completion_block.call
    rescue => ex
      Pantry.logger.debug("[Receive File] Error running completion block #{ex.inspect}")
    end
  end

  super
end
finished?()
Alias for: complete?
on_complete(&block) click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 100
def on_complete(&block)
  @completion_block = block
end
remove() click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 156
def remove
  @uploaded_file.unlink
end
valid?() click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 150
def valid?
  return @is_valid if defined?(@is_valid)
  uploaded_checksum = Pantry.file_checksum(@uploaded_file.path)
  @is_valid = (uploaded_checksum == @checksum)
end
write_chunk(offset, size, data) click to toggle source
# File lib/pantry/communication/file_service/file_progress.rb, line 119
def write_chunk(offset, size, data)
  @current_pipeline_size -= 1
  @received_chunks       += 1

  @uploaded_file.seek(offset)
  @uploaded_file.write(data)

  if @received_chunks == @chunk_count
    @uploaded_file.close
  end
end