class DropboxClient::ChunkedUploader

ChunkedUploader is responsible for uploading a large file to Dropbox in smaller chunks. This allows large files to be uploaded and makes allows recovery during failure.

Attributes

client[RW]
file_obj[RW]
offset[RW]
total_size[RW]
upload_id[RW]

Public Class Methods

new(client, file_obj, total_size) click to toggle source
# File lib/dropbox_sdk.rb, line 837
def initialize(client, file_obj, total_size)
  @client = client
  @file_obj = file_obj
  @total_size = total_size
  @upload_id = nil
  @offset = 0
end

Public Instance Methods

finish(to_path, overwrite=false, parent_rev=nil) click to toggle source

Completes a file upload

Args:

  • to_path: The directory path to upload the file to. If the destination directory does not yet exist, it will be created.

  • overwrite: Whether to overwrite an existing file at the given path. [default is False] If overwrite is False and a file already exists there, Dropbox will rename the upload to make sure it doesn't overwrite anything. You must check the returned metadata to know what this new name is. This field should only be True if your intent is to potentially clobber changes to a file that you don't know about.

  • parent_rev: The rev field from the 'parent' of this upload. If your intent is to update the file at the given path, you should pass the parent_rev parameter set to the rev value from the most recent metadata you have of the existing file at that path. If the server has a more recent version of the file at the specified path, it will automatically rename your uploaded file, spinning off a conflict. Using this parameter effectively causes the overwrite parameter to be ignored. The file will always be overwritten if you send the most-recent parent_rev, and it will never be overwritten you send a less-recent one.

Returns:

# File lib/dropbox_sdk.rb, line 910
def finish(to_path, overwrite=false, parent_rev=nil)
  response = @client.commit_chunked_upload(to_path, @upload_id, overwrite, parent_rev)
  Dropbox::parse_response(response)
end
upload(chunk_size=4*1024*1024) click to toggle source

Uploads data from this ChunkedUploader's #file_obj in chunks, until an error occurs. Throws an exception when an error occurs, and can be called again to resume the upload.

Args:

  • chunk_size: The chunk size for each individual upload. Defaults to 4MB.

# File lib/dropbox_sdk.rb, line 851
def upload(chunk_size=4*1024*1024)
  last_chunk = nil

  while @offset < @total_size
    if not last_chunk
      last_chunk = @file_obj.read(chunk_size)
    end

    resp = {}
    begin
      resp = Dropbox::parse_response(@client.partial_chunked_upload(last_chunk, @upload_id, @offset))
      last_chunk = nil
    rescue SocketError => e
      raise e
    rescue SystemCallError => e
      raise e
    rescue DropboxError => e
      raise e if e.http_response.nil? or e.http_response.code[0] == '5'
      begin
        resp = JSON.parse(e.http_response.body)
        raise DropboxError.new('server response does not have offset key') unless resp.has_key? 'offset'
      rescue JSON::ParserError
        raise DropboxError.new("Unable to parse JSON response: #{e.http_response.body}")
      end
    end

    if resp.has_key? 'offset' and resp['offset'] > @offset
      @offset += (resp['offset'] - @offset) if resp['offset']
      last_chunk = nil
    end
    @upload_id = resp['upload_id'] if resp['upload_id']
  end
end