Class: DropboxClient::ChunkedUploader
- Inherits:
-
Object
- Object
- DropboxClient::ChunkedUploader
- Defined in:
- lib/dropbox_sdk_v2.rb
Overview
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.
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#file_obj ⇒ Object
Returns the value of attribute file_obj.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#total_size ⇒ Object
Returns the value of attribute total_size.
-
#upload_id ⇒ Object
Returns the value of attribute upload_id.
Instance Method Summary collapse
-
#finish(to_path, overwrite = false, parent_rev = nil) ⇒ Object
Completes a file upload.
-
#initialize(client, file_obj, total_size) ⇒ ChunkedUploader
constructor
A new instance of ChunkedUploader.
-
#upload(chunk_size = 4*1024*1024) ⇒ Object
Uploads data from this ChunkedUploader’s file_obj in chunks, until an error occurs.
Constructor Details
#initialize(client, file_obj, total_size) ⇒ ChunkedUploader
Returns a new instance of ChunkedUploader.
847 848 849 850 851 852 853 |
# File 'lib/dropbox_sdk_v2.rb', line 847 def initialize(client, file_obj, total_size) @client = client @file_obj = file_obj @total_size = total_size @upload_id = nil @offset = 0 end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
845 846 847 |
# File 'lib/dropbox_sdk_v2.rb', line 845 def client @client end |
#file_obj ⇒ Object
Returns the value of attribute file_obj.
845 846 847 |
# File 'lib/dropbox_sdk_v2.rb', line 845 def file_obj @file_obj end |
#offset ⇒ Object
Returns the value of attribute offset.
845 846 847 |
# File 'lib/dropbox_sdk_v2.rb', line 845 def offset @offset end |
#total_size ⇒ Object
Returns the value of attribute total_size.
845 846 847 |
# File 'lib/dropbox_sdk_v2.rb', line 845 def total_size @total_size end |
#upload_id ⇒ Object
Returns the value of attribute upload_id.
845 846 847 |
# File 'lib/dropbox_sdk_v2.rb', line 845 def upload_id @upload_id end |
Instance Method Details
#finish(to_path, overwrite = false, parent_rev = nil) ⇒ Object
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:
-
A Hash with the metadata of file just uploaded. For a detailed description of what this call returns, visit: www.dropbox.com/developers/reference/api#metadata
920 921 922 923 |
# File 'lib/dropbox_sdk_v2.rb', line 920 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) ⇒ Object
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.
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'lib/dropbox_sdk_v2.rb', line 861 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 |