-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Upload to nginx with crypto AES #2924
Description
I have implemented an nginx approach to upload files but i want to add encryption prior to uploading now.
My jquery upload initialization is as follows:
$('#fileupload').fileupload({
maxRetries: 15,
retryTimeout: 1000,
dataType: 'text',
maxFileSize: 50 * 1024 * 1024 * 1024,
//upload 5 MB at a time
maxChunkSize: 5 * 1024 * 1024,
//acceptFileTypes: /(.|/)(gif|jpe?g|png)$/i,
multipart: false,
beforeSend: function(e, files, index, xhr, handler, callback) {
var chrome, context, device, file, filename, filesize, ios, sessionID;
// Retrieve the file that is about to be sent to nginx
file = files.files[0];
// Collect some basic file information
filename = file.name;
filesize = file.size;
// Grab the context (table row) for this upload
context = files.context[0];
encryptFiles(files.files, function () {
console.log(files.files)
files.files = encrypted;
//data.submit();
});
// Set uploadedBytes on the context to ensure that if this upload was
// resumed, it will continue from where it left off.
$(context).attr("uploadedBytes", files.uploadedBytes);
// Set the required headers for the nginx upload module
e.setRequestHeader("Session-ID", sessionID);
var ext = false;
if (filename.lastIndexOf(".") == (filename.length-4) || filename.lastIndexOf(".") == (filename.length-5) ) {
ext = true;
}
if (!ext) {
e.setRequestHeader("Content-Type", "text/plain");
}
e.setRequestHeader("X-Requested-With", "XMLHttpRequest");
device = navigator.userAgent.toLowerCase();
ios = device.match(/(iphone|ipod|ipad)/);
chrome = device.match(/crios/);
if (ios && !chrome) {
e.setRequestHeader("Cache-Control", "no-cache");
}
}
});
I have tried to use as you said somewhere in the add option
add: function (e, data) {
encryptFiles(data.files, function () {
data.submit();
});
}
but the template doesnt work. It doesnt read the context and upload doesnt proceed because the context which is the tr of the table is 0
my encryptFiles function is :
var encryptFiles = function (files, callback) {
var reader = new FileReader();
var file = files[0];
var blob = file.slice(0, file.size);
var key = "testkey";
reader.readAsBinaryString(blob);
reader.onload = fileonload;
function fileonload(event) {
var result = event.target.result;
var encrypted = CryptoJS.AES.encrypt(result, CryptoJS.enc.Base64.parse(key));
callback();
}
}
encryption is as normal and when i alert the encrypted value i get the base64 thing.
I dont know how to proceed from there.
How can i pass the encrypted data to the library so that it sends the encrypted data instead of the unencrypted when file was added.