implement pause/resume for chunk upload

This commit is contained in:
j 2013-04-17 10:01:57 +00:00
parent 4bff07f054
commit 7738d13679

View file

@ -30,10 +30,14 @@ pandora.chunkupload = function(options) {
chunkURL,
file = options.file,
maxRetry = -1,
nextChunkId,
paused = false,
retries = 0,
request,
that = Ox.Element();
options.data = options.data || {};
initUpload();
function done() {
@ -143,7 +147,12 @@ pandora.chunkupload = function(options) {
// reset retry counter
retries = 0;
// start uploading next chunk
if (paused) {
nextChunkId = chunkId + 1;
that.triggerEvent('paused', {next: nextChunkId});
} else {
uploadChunk(chunkId + 1);
}
} else {
// failed to upload, try again in 5 second
retries++;
@ -153,7 +162,12 @@ pandora.chunkupload = function(options) {
done();
} else {
setTimeout(function() {
if (paused) {
nextChunkId = chunkId;
that.triggerEvent('paused', {next: nextChunkId});
} else {
uploadChunk(chunkId);
}
}, 5000);
}
}
@ -167,7 +181,12 @@ pandora.chunkupload = function(options) {
done();
} else {
setTimeout(function() {
if (paused) {
nextChunkId = chunkId;
that.triggerEvent('paused', {next: nextChunkId});
} else {
uploadChunk(chunkId);
}
}, 3000);
}
}, false);
@ -200,6 +219,21 @@ pandora.chunkupload = function(options) {
request.abort();
request = null;
}
return that;
};
that.pause = function() {
paused = true;
return that;
};
that.resume = function() {
if (paused) {
paused = false;
if (nextChunkId) {
uploadChunk(nextChunkId);
nextChunkId = null;
}
}
return that;
};
return that;