1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added true cancelation of uploads. This includes during loading, encryption, and final upload.

This commit is contained in:
Uncled1023 2018-02-03 23:39:17 -08:00
parent e27b625dc3
commit 4ba6f2aae2
2 changed files with 73 additions and 38 deletions

View File

@ -66,13 +66,6 @@ self.addEventListener('message', function (e) {
var finalStr = encFinal.toString(CryptoJS.enc.Base64); // to final string var finalStr = encFinal.toString(CryptoJS.enc.Base64); // to final string
prog = Uint8Concat(prog, _base64ToArray(finalStr)); prog = Uint8Concat(prog, _base64ToArray(finalStr));
var objData =
{
cmd: 'progress',
processed: bytes.length - 1,
total: bytes.length - 1
};
// Now package it into a mesage to send home // Now package it into a mesage to send home
var objData = var objData =
{ {

View File

@ -60,12 +60,16 @@ function linkShortenUrl(element, fileID, url) {
}); });
} }
function linkRemove(element, fileID) { function linkRemove(element, fileID, token) {
element.click(function () { element.click(function () {
$('#upload-panel-' + fileID).remove(); if (token.isCancelable()) {
if ($('#upload-links').children().length == 0) { token.cancel();
$("#upload-links").css('display', 'none', 'important'); } else {
$('#upload-action-buttons').hide(); $('#upload-panel-' + fileID).remove();
if ($('#upload-links').children().length == 0) {
$("#upload-links").css('display', 'none', 'important');
$('#upload-action-buttons').hide();
}
} }
return false; return false;
}); });
@ -156,11 +160,22 @@ var dropZone = new Dropzone(document.body, {
// Convert file to blob // Convert file to blob
var blob = file.slice(0, file.size); var blob = file.slice(0, file.size);
// Create a token for this upload
var token = {
callback: null,
cancel: function() {
this.callback();
},
isCancelable: function() {
return this.callback !== null;
}
};
// Create the Upload // Create the Upload
var fileID = createUpload(file.name); var fileID = createUpload(file.name, token);
// Process the file // Process the file
processFile(blob, file.name, file.type, file.size, fileID); processFile(blob, file.name, file.type, file.size, fileID, token);
// Remove this file from the dropzone set // Remove this file from the dropzone set
this.removeFile(file); this.removeFile(file);
@ -169,7 +184,7 @@ var dropZone = new Dropzone(document.body, {
var fileCount = 0; var fileCount = 0;
function createUpload(fileName) { function createUpload(fileName, token) {
// Create the UI element for the new item // Create the UI element for the new item
var fileID = fileCount; var fileID = fileCount;
fileCount++; fileCount++;
@ -183,7 +198,7 @@ function createUpload(fileName) {
itemDiv.find('#upload-link-panel').hide(); itemDiv.find('#upload-link-panel').hide();
// Assign buttons // Assign buttons
linkRemove(itemDiv.find('#upload-close'), fileID); linkRemove(itemDiv.find('#upload-close'), fileID, token);
// Set the info // Set the info
itemDiv.find('#upload-title').html(fileName); itemDiv.find('#upload-title').html(fileName);
@ -194,7 +209,7 @@ function createUpload(fileName) {
return fileID; return fileID;
} }
function processFile(fileBlob, fileName, contentType, contentSize, fileID) { function processFile(fileBlob, fileName, contentType, contentSize, fileID, token) {
// Check the file size // Check the file size
if (contentSize <= maxUploadSize) { if (contentSize <= maxUploadSize) {
@ -205,10 +220,10 @@ function processFile(fileBlob, fileName, contentType, contentSize, fileID) {
if (encrypt) { if (encrypt) {
// Encrypt the file and upload it // Encrypt the file and upload it
encryptFile(fileBlob, fileName, contentType, fileID, uploadFile); encryptFile(fileBlob, fileName, contentType, fileID, uploadFile, token);
} else { } else {
// pass it along // pass it along
uploadFile(fileBlob, null, null, contentType, fileExt, fileID, encrypt); uploadFile(fileBlob, null, null, contentType, fileExt, fileID, encrypt, token);
} }
} }
else { else {
@ -218,7 +233,7 @@ function processFile(fileBlob, fileName, contentType, contentSize, fileID) {
} }
// Function to encrypt a file, overide the file's data attribute with the encrypted value, and then call a callback function if supplied // Function to encrypt a file, overide the file's data attribute with the encrypted value, and then call a callback function if supplied
function encryptFile(blob, fileName, contentType, ID, callback) { function encryptFile(blob, fileName, contentType, ID, callback, token) {
var fileExt = getFileExtension(fileName); var fileExt = getFileExtension(fileName);
// Get session settings // Get session settings
@ -230,10 +245,9 @@ function encryptFile(blob, fileName, contentType, ID, callback) {
// When the file has been loaded, encrypt it // When the file has been loaded, encrypt it
reader.onload = (function (callback) { reader.onload = (function (callback) {
return function (e) { return function (e) {
// Just send straight to server if they don't want to encrypt it // Just send straight to server if they don't want to encrypt it
if (!encrypt) { if (!encrypt) {
callback(e.target.result, null, null, contentType, fileExt, ID, encrypt); callback(e.target.result, null, null, contentType, fileExt, ID, encrypt, token);
} }
else { else {
// Set variables for tracking // Set variables for tracking
@ -262,18 +276,22 @@ function encryptFile(blob, fileName, contentType, ID, callback) {
case 'finish': case 'finish':
if (callback != null) { if (callback != null) {
// Finish // Finish
callback(e.data.buffer, keyStr, ivStr, contentType, fileExt, ID, encrypt); callback(e.data.buffer, keyStr, ivStr, contentType, fileExt, ID, encrypt, token);
} }
break; break;
} }
}); });
worker.onerror = function (err) { worker.onerror = function (err) {
// An error occured uploadFailed(ID, token, err);
setProgress(ID, 100, 'progress-bar-danger', '', 'Error Occured');
$('#upload-panel-' + ID).find('.panel').addClass('panel-danger');
} }
token.callback = function () { // SPECIFY CANCELLATION
worker.terminate(); // terminate the worker process
uploadCanceled(ID, token, null);
};
// Generate the script to include as a blob // Generate the script to include as a blob
var scriptBlob = GenerateBlobURL(aesScriptSrc); var scriptBlob = GenerateBlobURL(aesScriptSrc);
@ -292,18 +310,30 @@ function encryptFile(blob, fileName, contentType, ID, callback) {
}; };
})(callback); })(callback);
reader.onprogress = function (data) { reader.onerror = function (err) {
uploadFailed(ID, token, err);
}
reader.onprogress = function(data) {
if (data.lengthComputable) { if (data.lengthComputable) {
var progress = parseInt(((data.loaded / data.total) * 100), 10); var progress = parseInt(((data.loaded / data.total) * 100), 10);
setProgress(ID, progress, 'progress-bar-success progress-bar-striped active', progress + '%', 'Loading'); setProgress(ID, progress, 'progress-bar-success progress-bar-striped active', progress + '%', 'Loading');
} }
} };
reader.onabort = function() {
uploadCanceled(ID, token, null);
};
token.callback = function () { // SPECIFY CANCELLATION
reader.abort(); // abort request
};
// Start async read // Start async read
reader.readAsArrayBuffer(blob); reader.readAsArrayBuffer(blob);
} }
function uploadFile(data, key, iv, filetype, fileExt, fileID, encrypt) function uploadFile(data, key, iv, filetype, fileExt, fileID, encrypt, token)
{ {
// Set variables for tracking // Set variables for tracking
var startTime = (new Date()).getTime(); var startTime = (new Date()).getTime();
@ -323,15 +353,18 @@ function uploadFile(data, key, iv, filetype, fileExt, fileID, encrypt)
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress.bind(null, fileID, startTime), false); xhr.upload.addEventListener("progress", uploadProgress.bind(null, fileID, startTime), false);
xhr.addEventListener("load", uploadComplete.bind(null, fileID, key, encrypt), false); xhr.addEventListener("load", uploadComplete.bind(null, fileID, key, encrypt, token), false);
xhr.addEventListener("error", uploadFailed.bind(null, fileID), false); xhr.addEventListener("error", uploadFailed.bind(null, fileID, token), false);
xhr.addEventListener("abort", uploadCanceled.bind(null, fileID), false); xhr.addEventListener("abort", uploadCanceled.bind(null, fileID, token), false);
token.callback = function () { // SPECIFY CANCELLATION
xhr.abort(); // abort request
};
xhr.open("POST", uploadFileURL); xhr.open("POST", uploadFileURL);
xhr.send(fd); xhr.send(fd);
} }
function uploadProgress(fileID, startTime, evt) { function uploadProgress(fileID, startTime, evt) {
if (evt.lengthComputable) { if (evt.lengthComputable) {
var curTime = (new Date()).getTime(); var curTime = (new Date()).getTime();
@ -347,7 +380,10 @@ function uploadProgress(fileID, startTime, evt) {
} }
} }
function uploadComplete(fileID, key, encrypt, evt) { function uploadComplete(fileID, key, encrypt, token, evt) {
// Cancel out cancel token
token.callback = null;
obj = JSON.parse(evt.target.responseText); obj = JSON.parse(evt.target.responseText);
if (obj.result != null) { if (obj.result != null) {
var itemDiv = $('#upload-panel-' + fileID); var itemDiv = $('#upload-panel-' + fileID);
@ -398,12 +434,18 @@ function uploadComplete(fileID, key, encrypt, evt) {
} }
} }
function uploadFailed(fileID, evt) { function uploadFailed(fileID, token, evt) {
setProgress(fileID, 100, 'progress-bar-danger', '', 'Upload Failed'); // Cancel out cancel token
token.callback = null;
setProgress(fileID, 100, 'progress-bar-danger', '', 'Upload Failed: ' + parseErrorMessage(evt));
$('#upload-panel-' + fileID).find('.panel').addClass('panel-danger'); $('#upload-panel-' + fileID).find('.panel').addClass('panel-danger');
} }
function uploadCanceled(fileID, evt) { function uploadCanceled(fileID, token, evt) {
// Cancel out cancel token
token.callback = null;
setProgress(fileID, 100, 'progress-bar-warning', '', 'Upload Canceled'); setProgress(fileID, 100, 'progress-bar-warning', '', 'Upload Canceled');
$('#upload-panel-' + fileID).find('.panel').addClass('panel-warning'); $('#upload-panel-' + fileID).find('.panel').addClass('panel-warning');
} }