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,25 +4011,30 @@ jQuery.PrivateBin = (function($, sjcl, RawDeflate) {
// encrypt data // encrypt data
try { try {
Uploader.setData('data', plainText); // start promisesat the same time and wait thereafter
let settingData = [];
settingData.push(Uploader.setData('data', plainText));
if (nickname.length > 0) {
settingData(Uploader.setData('nickname', nickname));
}
await Promise.all(settingData);
} catch (e) { } catch (e) {
Alert.showError(e); Alert.showError(e);
} }
if (nickname.length > 0) {
Uploader.setData('nickname', nickname);
}
Uploader.run(); Uploader.run();
}; };
/** /**
* 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();
}; };
/** /**