Class: BrickFTP::API::FileOperation::Upload

Inherits:
Base
  • Object
show all
Defined in:
lib/brick_ftp/api/file_operation/upload.rb

Instance Attribute Summary

Attributes inherited from Base

#properties

Class Method Summary collapse

Methods inherited from Base

all, #as_json, #delete_property, #destroy, find, inherited, #initialize, #read_property, #to_json, #update, #write_property

Constructor Details

This class inherits a constructor from BrickFTP::API::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BrickFTP::API::Base

Class Method Details

.create(path:, source:, chunk_size: nil) ⇒ BrickFTP::API::FileOperation::Upload

Upload file.

Parameters:

  • path (String) —

    Remote file path.

  • source (IO) —

    Source data (not path) to upload.

  • chunk_size (Integer) (defaults to: nil) —

    Size of chunk to multi-part upload.

Returns:

See Also:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/brick_ftp/api/file_operation/upload.rb', line 42

def self.create(path:, source:, chunk_size: nil)
  api_client = BrickFTP::HTTPClient.new
  chunk_io = BrickFTP::Utils::ChunkIO.new(source, chunk_size: chunk_size)

  ref = nil
  params_for_request_upload_url = { action: 'put' }
  upload_info = {}
  chunk_io.each.with_index(1) do |chunk, part|
    params_for_request_upload_url.update(part: part, ref: ref) if part > 1
    upload_info = api_client.post(api_path_for(:create, path: path), params: params_for_request_upload_url)
    ref = upload_info['ref']

    upload_uri = URI.parse(upload_info['upload_uri'])
    upload_client = BrickFTP::HTTPClient.new(upload_uri.host)
    upload_client.put(upload_info['upload_uri'], params: chunk)
  end

  uploaded_info = api_client.post(api_path_for(:create, path: path), params: { action: 'end', ref: ref })

  new(upload_info.merge(uploaded_info).symbolize_keys)
end