Skip to content

Commit

Permalink
Merge pull request #55 from srijs/feature-preprocess
Browse files Browse the repository at this point in the history
Add feature to preprocess chunks prior to testing & sending.
  • Loading branch information
steffentchr committed Mar 18, 2013
2 parents ac07050 + dc9e8ec commit 6057edc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.*)
Expand Down
22 changes: 18 additions & 4 deletions resumable.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var Resumable = function(opts){
throttleProgressCallbacks:0.5,
query:{},
headers:{},
preprocess:null,
method:'multipart',
prioritizeFirstAndLastChunk:false,
target:'/',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit 6057edc

Please sign in to comment.