Class: Pantry::Communication::FileService::ReceivingFile

Inherits:
UploadInfo
  • Object
show all
Defined in:
lib/pantry/communication/file_service/file_progress.rb

Overview

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.

Instance Attribute Summary collapse

Attributes inherited from UploadInfo

#file_uuid, #receiver_uuid

Instance Method Summary collapse

Methods inherited from UploadInfo

#wait_for_finish

Constructor Details

#initialize(file_size, checksum, chunk_size, pipeline_size) ⇒ ReceivingFile

Returns a new instance of ReceivingFile.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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

Instance Attribute Details

#checksumObject (readonly)

Returns the value of attribute checksum.



77
78
79
# File 'lib/pantry/communication/file_service/file_progress.rb', line 77

def checksum
  @checksum
end

#file_sizeObject (readonly)

Returns the value of attribute file_size.



77
78
79
# File 'lib/pantry/communication/file_service/file_progress.rb', line 77

def file_size
  @file_size
end

#sender_uuidObject

Returns the value of attribute sender_uuid.



78
79
80
# File 'lib/pantry/communication/file_service/file_progress.rb', line 78

def sender_uuid
  @sender_uuid
end

#uploaded_pathObject (readonly)

Location of the tempfile containing the contents of the uploaded file



75
76
77
# File 'lib/pantry/communication/file_service/file_progress.rb', line 75

def uploaded_path
  @uploaded_path
end

Instance Method Details

#chunks_to_fetch(&block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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?Boolean Also known as: finished?

Returns:

  • (Boolean)


145
146
147
# File 'lib/pantry/communication/file_service/file_progress.rb', line 145

def complete?
  @uploaded_file.closed?
end

#finished!Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# 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

#on_complete(&block) ⇒ Object



100
101
102
# File 'lib/pantry/communication/file_service/file_progress.rb', line 100

def on_complete(&block)
  @completion_block = block
end

#removeObject



156
157
158
# File 'lib/pantry/communication/file_service/file_progress.rb', line 156

def remove
  @uploaded_file.unlink
end

#valid?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# 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