Adjust functions using Uploader.setData to handle promises

This commit is contained in:
rugk 2018-10-08 21:03:10 +02:00
parent 94a352e7f5
commit 746debf586
No known key found for this signature in database
GPG Key ID: 05D40A636AFAB34D

View File

@ -3764,14 +3764,15 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) {
* encrypts and sets the data * encrypts and sets the data
* *
* @name Uploader.setData * @name Uploader.setData
* @async
* @function * @function
* @param {string} index * @param {string} index
* @param {mixed} element * @param {mixed} element
*/ */
me.setData = function(index, element) me.setData = async function(index, element)
{ {
checkCryptParameters(); checkCryptParameters();
data[index] = CryptTool.cipher(symmetricKey, password, element); data[index] = await CryptTool.cipher(symmetricKey, password, element);
}; };
/** /**
@ -3922,34 +3923,40 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) {
function encryptAttachments(callback) { function encryptAttachments(callback) {
var file = AttachmentViewer.getAttachmentData(); var file = AttachmentViewer.getAttachmentData();
let encryptAttachmentPromise, encryptAttachmentNamePromise;
if (typeof file !== 'undefined' && file !== null) { if (typeof file !== 'undefined' && file !== null) {
var fileName = AttachmentViewer.getFile().name; var fileName = AttachmentViewer.getFile().name;
Uploader.setData('attachment', file); // run concurrently to encrypt everything
Uploader.setData('attachmentname', fileName); encryptAttachmentPromise = Uploader.setData('attachment', file);
encryptAttachmentNamePromise = Uploader.setData('attachmentname', fileName);
// run callback
return callback();
} else if (AttachmentViewer.hasAttachment()) { } else if (AttachmentViewer.hasAttachment()) {
// fall back to cloned part // fall back to cloned part
var attachment = AttachmentViewer.getAttachment(); var attachment = AttachmentViewer.getAttachment();
Uploader.setData('attachment', attachment[0]); encryptAttachmentPromise = Uploader.setData('attachment', attachment[0]);
Uploader.setData('attachmentname', attachment[1]); encryptAttachmentNamePromise = Uploader.setData('attachmentname', attachment[1]);
return callback();
} else { } else {
// if there are no attachments, this is of course still successful // if there are no attachments, this is of course still successful
return callback(); return callback();
} }
// TODO: change this callback to also use Promises instead,
// this here just waits
Promise.all([encryptAttachmentPromise, encryptAttachmentNamePromise]).then(() => {
// run callback
return callback();
});
} }
/** /**
* send a reply in a discussion * send a reply in a discussion
* *
* @async
* @name PasteEncrypter.sendComment * @name PasteEncrypter.sendComment
* @function * @function
*/ */
me.sendComment = function() me.sendComment = async function()
{ {
Alert.hideMessages(); Alert.hideMessages();
Alert.setCustomHandler(DiscussionViewer.handleNotification); Alert.setCustomHandler(DiscussionViewer.handleNotification);
@ -4004,13 +4011,17 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) {
// encrypt data // encrypt data
try { try {
Uploader.setData('data', plainText); // start promisesat the same time and wait thereafter
} catch (e) { let settingData = [];
Alert.showError(e); settingData.push(Uploader.setData('data', plainText));
}
if (nickname.length > 0) { if (nickname.length > 0) {
Uploader.setData('nickname', nickname); settingData(Uploader.setData('nickname', nickname));
}
await Promise.all(settingData);
} catch (e) {
Alert.showError(e);
} }
Uploader.run(); Uploader.run();
@ -4019,10 +4030,11 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) {
/** /**
* sends a new paste to server * sends a new paste to server
* *
* @async
* @name PasteEncrypter.sendPaste * @name PasteEncrypter.sendPaste
* @function * @function
*/ */
me.sendPaste = function() me.sendPaste = async function()
{ {
// hide previous (error) messages // hide previous (error) messages
Controller.hideStatusMessages(); Controller.hideStatusMessages();
@ -4075,20 +4087,20 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) {
PasteViewer.setText(plainText); PasteViewer.setText(plainText);
PasteViewer.setFormat(format); PasteViewer.setFormat(format);
// encrypt cipher data
try {
Uploader.setData('data', plainText);
} catch (e) {
Alert.showError(e);
}
// encrypt attachments // encrypt attachments
encryptAttachments( const encryptAttachmentsPromise = encryptAttachments(
function () { function () {
// send data // TODO: remove, is not needed anymore as we use Promises
Uploader.run();
} }
); );
// encrypt plain text
const encryptDataPromise = Uploader.setData('data', plainText);
await Promise.all([encryptAttachmentsPromise, encryptDataPromise]).catch(Alert.showError);
// send data
Uploader.run();
}; };
/** /**