Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for chunked uploads #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ OR, if you just want to start playing with the library run...
shares <-- create link to view file
media <-- create streamable link to file
thumbnails <-- get thumbnail of file
chunk <-- upload a chunk of a large file
commit_chunks <-- complete a chunked upload
copyref <-- create copy reference to file
delta <-- get list of delta entries
stream <-- creates readable stream
Expand Down Expand Up @@ -468,6 +470,57 @@ output of `metadata` returns...
"size": "762.5 KB"
}

### chunk(body, [options,] cb)

Uploads large files to Dropbox in multiple chunks. Also has the ability to resume if the upload is interrupted. This allows for uploads larger than the /files and /files_put maximum of 150 MB. Note that chunks must be submitted in order, else the response will be a 400 error that includes the current offset.

client.chunk("some data", {offset: 0, upload_id: null}, function(status, reply){
console.dir(reply);

require('fs').writeFile('koala_small.jpg', reply, function () {
console.log('Thumbnail saved!');
});
})

output of `reply` returns...

{
expires: 'Fri, 25 Oct 2013 04:51:52 +0000',
upload_id: 'nLkA-eBpXUcr07JPq_ShRA',
offset: 4
}

Typical usage:

1. Send your first chunk of your file to `chunk` and receive an upload_id in the reply.
2. Repeatedly submit subsequent chunks using the upload_id to identify the upload in progress and an offset representing the number of bytes transferred so far.
3. After each chunk has been uploaded, the server returns a new offset representing the total amount transferred.
4. After the last chunk, call `commit_chunks` to complete the upload.

### commit_chunks(path, [options,] callback)

Completes an upload initiated by `chunk`. Similar to `put`, but takes an `upload_id` instead of `data`.

client.commit_chunks(filename, {upload_id: 'nLkA-eBpXUcr07JPq_ShRA'}, function(status, reply){
console.dir(reply)
})

output of `reply` returns...

{
"size": "225.4KB",
"rev": "35e97029684fe",
"thumb_exists": false,
"bytes": 230783,
"modified": "Tue, 19 Jul 2011 21:55:38 +0000",
"path": "/foo/hello.txt",
"is_dir": false,
"icon": "page_white_text",
"root": "sandbox",
"mime_type": "text/plain",
"revision": 220823
}

### cpref(path, [options,] callback)

client.cpref("song.mp3", function(status, reply){
Expand Down
54 changes: 54 additions & 0 deletions lib/dbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,60 @@ exports.app = function(config){
})
},

chunk: function(body, args, cb){
if(!cb){
cb = args
args = {}
}

var signature = helpers.sign(options, args)

var url = helpers.url({
hostname: "api-content.dropbox.com",
action: "chunked_upload",
// path: path,
root: "",
query: signature
});

args["method"] = "PUT";
args["headers"] = { "content-length": body.length };
args["url"] = url;

// do not send empty body
if(body.length > 0) args["body"] = body

return request(args, function(e, r, b){
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b))
})
},

commit_chunks: function(path, args, cb){
if(!cb){
cb = args
args = null
}

var signature = helpers.sign(options, args)

var url = helpers.url({
hostname: "api-content.dropbox.com",
action: "commit_chunked_upload",
path: path,
query: signature
})

var args = {
"method": "POST",
"url": url
}

return request(args, function(e, r, b){
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b))
})
},


metadata: function(path, args, cb){
if(!cb){
cb = args
Expand Down
3 changes: 3 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ module.exports = function(config){

// fileops calls desn't want root in path
var rootpath = fileop ? "" : root

// override rootpath if obj.root is specified
if(typeof obj.root !== "undefined" && obj.root !== null) rootpath = obj.root;

// fileops calls desn't want scope in path
var scopepath = fileop ? "" : scope
Expand Down
2 changes: 1 addition & 1 deletion test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("all", function(){
done()
})
})

it("should get metadatq of file", function(done) {
client.metadata("myfirstfile.txt", function(status, reply){
status.should.eql(200)
Expand Down