diff --git a/README.md b/README.md index 6a8d30fc..2cb256ad 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ Available configuration options are: * `method` Method to use when POSTing chunks to the server (`multipart` or `octet`) (Default: `multipart`) * `prioritizeFirstAndLastChunk` Prioritize first and last chunks of all files. This can be handy if you can determine if a file is valid for your service from only the first or last chunk. For example, photo or video meta data is usually located in the first part of a file, making it easy to test support from only the first chunk. (Default: `false`) * `testChunks` Make a GET request to the server for each chunks to see if it already exists. If implemented on the server-side, this will allow for upload resumes even after a browser crash or even a computer restart. (Default: `true`) +* `preprocess` Optional function to process each chunk before testing & sending. Function is passed the chunk as parameter, and should call the `preprocessFinished` method on the chunk when finished. (Default: `null`) * `generateUniqueIdentifier` Override the function that generates unique identifiers for each file. (Default: `null`) * `maxFiles` Indicates how many files can be uploaded in a single session. Valid values are any positive integer and `undefined` for no limit. (Default: `undefined`) * `maxFilesErrorCallback` A function which displays the *please upload n file(s) at a time* message. (Default: displays an alert box with the message *Please n one file(s) at a time.*) diff --git a/resumable.js b/resumable.js index e1802b82..a186a176 100644 --- a/resumable.js +++ b/resumable.js @@ -37,6 +37,7 @@ var Resumable = function(opts){ throttleProgressCallbacks:0.5, query:{}, headers:{}, + preprocess:null, method:'multipart', prioritizeFirstAndLastChunk:false, target:'/', @@ -251,6 +252,7 @@ var Resumable = function(opts){ $.lastProgressCallback = (new Date); $.tested = false; $.retries = 0; + $.preprocessState = 0; // 0 = unprocessed, 1 = processing, 2 = finished // Computed properties $.loaded = 0; @@ -304,8 +306,20 @@ var Resumable = function(opts){ $.xhr.send(null); } + $.preprocessFinished = function(){ + $.preprocessState = 2; + $.send(); + } + // send() uploads the actual data in a POST call $.send = function(){ + if(typeof $.resumableObj.opts.preprocess === 'function') { + switch($.preprocessState) { + case 0: $.resumableObj.opts.preprocess($); $.preprocessState = 1; return; + case 1: return; + case 2: break; + } + } if($.resumableObj.opts.testChunks && !$.tested) { $.test(); return; @@ -440,12 +454,12 @@ var Resumable = function(opts){ // metadata and determine if there's even a point in continuing. if ($.opts.prioritizeFirstAndLastChunk) { $h.each($.files, function(file){ - if(file.chunks.length && file.chunks[0].status()=='pending') { + if(file.chunks.length && file.chunks[0].status()=='pending' && file.chunks[0].preprocessState === 0) { file.chunks[0].send(); found = true; return(false); } - if(file.chunks.length>1 && file.chunks[file.chunks.length-1].status()=='pending') { + if(file.chunks.length>1 && file.chunks[file.chunks.length-1].status()=='pending' && file.chunks[0].preprocessState === 0) { file.chunks[file.chunks.length-1].send(); found = true; return(false); @@ -457,7 +471,7 @@ var Resumable = function(opts){ // Now, simply look for the next, best thing to upload $h.each($.files, function(file){ $h.each(file.chunks, function(chunk){ - if(chunk.status()=='pending') { + if(chunk.status()=='pending' && chunk.preprocessState === 0) { chunk.send(); found = true; return(false); @@ -472,7 +486,7 @@ var Resumable = function(opts){ outstanding = false; $h.each(file.chunks, function(chunk){ var status = chunk.status(); - if(status=='pending' || status=='uploading') { + if(status=='pending' || status=='uploading' || chunk.preprocessState === 1) { outstanding = true; return(false); }