Class: DropboxApi::ChunkedUploader

Inherits:
Object
  • Object
show all
Includes:
OptionsValidator
Defined in:
lib/dropbox_api/chunked_uploader.rb

Instance Method Summary collapse

Methods included from OptionsValidator

#validate_options

Constructor Details

#initialize(client, path, i_stream, options = {}) ⇒ ChunkedUploader

Returns a new instance of ChunkedUploader.


5
6
7
8
9
10
11
12
# File 'lib/dropbox_api/chunked_uploader.rb', line 5

def initialize(client, path, i_stream, options = {})
  @chunk_size = options.delete(:chunk_size) || 4 * 1024 * 1024 # 4 MiB

  @client = client
  @i_stream = i_stream

  init_commit_info(path, options)
end

Instance Method Details

#finishObject


31
32
33
# File 'lib/dropbox_api/chunked_uploader.rb', line 31

def finish
  @client.upload_session_finish @cursor, @commit_info
end

#startObject


14
15
16
17
18
19
# File 'lib/dropbox_api/chunked_uploader.rb', line 14

def start
  chunk = @i_stream.read @chunk_size
  chunk = "" if chunk.nil?

  @cursor = @client.upload_session_start chunk
end

#uploadObject


21
22
23
24
25
26
27
28
29
# File 'lib/dropbox_api/chunked_uploader.rb', line 21

def upload
  loop do
    chunk = @i_stream.read @chunk_size
    break if chunk.nil?

    @client.upload_session_append_v2 @cursor, chunk
    break if chunk.size < @chunk_size
  end
end