Class: BrickFTP::RESTfulAPI::UploadFile

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/brick_ftp/restful_api/upload_file.rb

Overview

Overview of uploading

Uploading files using the REST API is done in 3 stages:

  1. Start a new upload by sending a request to REST API to indicate intent to upload a file.
  2. Upload the file to the URL(s) provided by the REST API, possibly in parts via multiple uploads.
  3. Complete the upload by notifying the REST API that the file upload has completed.

Constant Summary collapse

CHUNK_SIZE_RANGE =
(5_242_880..5_368_709_120).freeze

Instance Method Summary collapse

Methods included from Command

included, #initialize

Instance Method Details

#call(path, data, chunk_size: nil) ⇒ BrickFTP::Types::File

At this point, you are to send a PUT request to the returned upload_uri with the file data, along with the headers and parameters provided to you from BrickFTP.

The upload_uri link is signed by BrickFTP and must be used within 15 minutes. You will receive an HTTP 200 response with no body upon successful upload.

Should you wish to upload the file in multiple parts (required if the file size exceeds 5 GB) you will need to request an additional upload URL for the next part.

Parameters:

  • path (String)

    Full path of the file or folder. Maximum of 550 characters.

  • data (IO)
  • chunk_size (Integer, nil) (defaults to: nil)

    the chunk sizes are required to be between 5 MB and 5 GB. This option is ignored if data is StringIO.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brick_ftp/restful_api/upload_file.rb', line 37

def call(path, data, chunk_size: nil)
  chunk_size = adjust_chunk_size(data, chunk_size)
  validate_range_of_chunk_size!(chunk_size)

  upload = StartUpload.new(client).call(path)
  chunk_io = BrickFTP::Utils::ChunkIO.new(data, chunk_size: chunk_size)

  rest = data.size
  chunk_io.each do |chunk|
    rest -= client.upload_file(upload.http_method, upload.upload_uri, chunk)
    break if !chunk_size || rest <= 0

    upload = ContinueUpload.new(client).call(path, ref: upload.ref, part: upload.part_number + 1)
  end
  CompleteUpload.new(client).call(path, ref: upload.ref)
end