use recursion in the new reencryptCredential promise functions

This commit is contained in:
binsky 2023-04-23 18:56:31 +02:00
parent 776ffe6ea1
commit e7fd74c32a

View file

@ -259,12 +259,22 @@
return; return;
} }
// add the double total progress value of the files count to be able to separate the decryption step and the re-encryption / update / upload phase this.total = this.parent.plain_credential.files.length;
this.total = this.parent.plain_credential.files.length * 2;
this.current = 0; this.current = 0;
for (let i = 0; i < this.parent.plain_credential.files.length; i++) { const files_workload = function () {
const _file = this.parent.plain_credential.files[i]; const check_next_callback = function () {
this.current++;
this.call_progress(new progress_datatype(this.current, this.total, 'files'));
if (this.current === this.total) {
this.call_then('All files has been updated');
} else {
setTimeout(files_workload.bind(this), 1);
}
};
const _file = this.parent.plain_credential.files[this.current];
/* jshint ignore:start */ /* jshint ignore:start */
FileService.getFile(_file).then((function (fileData) { FileService.getFile(_file).then((function (fileData) {
try { try {
@ -272,25 +282,18 @@
fileData.filename = EncryptService.decryptString(fileData.filename, this.parent.old_password); fileData.filename = EncryptService.decryptString(fileData.filename, this.parent.old_password);
fileData.file_data = EncryptService.decryptString(fileData.file_data, this.parent.old_password); fileData.file_data = EncryptService.decryptString(fileData.file_data, this.parent.old_password);
// increase due to successful decryption
this.current++;
this.call_progress(new progress_datatype(this.current, this.total, 'files'));
FileService.updateFile(fileData, this.parent.new_password).then((function () { FileService.updateFile(fileData, this.parent.new_password).then((function () {
// increase due to successful re-encryption / update / upload check_next_callback.bind(this)();
this.current++;
this.call_progress(new progress_datatype(this.current, this.total, 'files'));
if (this.current === this.total) {
this.call_then('All files has been updated');
}
}).bind(this)); }).bind(this));
} catch (e) { } catch (e) {
console.error(e); console.error(e);
console.error('Failed to re-encrypt file. It seems to be corrupt.', _file); console.error('Failed to re-encrypt file. It seems to be corrupt.', _file);
check_next_callback.bind(this)();
} }
}).bind(this)); }).bind(this));
/* jshint ignore:end */ /* jshint ignore:end */
} };
setTimeout(files_workload.bind(this), 1);
}; };
var promise_custom_field_files_update = function () { var promise_custom_field_files_update = function () {
@ -300,17 +303,28 @@
return; return;
} }
// add the double total progress value of the custom fields count to be able to separate the decryption step and the re-encryption / update / upload phase this.total = this.parent.plain_credential.custom_fields.length;
this.total = this.parent.plain_credential.custom_fields.length * 2; console.log("total custom_field_files_update = " + this.total);
this.current = 0; this.current = 0;
for (let i = 0; i < this.parent.plain_credential.custom_fields.length; i++) { const custom_field_workload = function () {
const custom_field = this.parent.plain_credential.custom_fields[i]; const check_next_callback = function () {
if (custom_field.field_type !== 'file') { this.current++;
continue; this.call_progress(new progress_datatype(this.current, this.total, 'custom_field_files'));
if (this.current === this.total) {
this.call_then('All custom field files has been updated');
} else {
setTimeout(custom_field_workload.bind(this), 1);
}
};
if (this.parent.plain_credential.custom_fields[this.current].field_type !== 'file') {
check_next_callback.bind(this)();
return;
} }
const _file = custom_field.value; const _file = this.parent.plain_credential.custom_fields[this.current].value;
/* jshint ignore:start */ /* jshint ignore:start */
FileService.getFile(_file).then((function (fileData) { FileService.getFile(_file).then((function (fileData) {
try { try {
@ -318,25 +332,18 @@
fileData.filename = EncryptService.decryptString(fileData.filename, this.parent.old_password); fileData.filename = EncryptService.decryptString(fileData.filename, this.parent.old_password);
fileData.file_data = EncryptService.decryptString(fileData.file_data, this.parent.old_password); fileData.file_data = EncryptService.decryptString(fileData.file_data, this.parent.old_password);
// increase due to successful decryption
this.current++;
this.call_progress(new progress_datatype(this.current, this.total, 'custom_field_files'));
FileService.updateFile(fileData, this.parent.new_password).then((function () { FileService.updateFile(fileData, this.parent.new_password).then((function () {
// increase due to successful re-encryption / update / upload check_next_callback.bind(this)();
this.current++;
this.call_progress(new progress_datatype(this.current, this.total, 'custom_field_files'));
if (this.current === this.total) {
this.call_then('All files has been updated');
}
}).bind(this)); }).bind(this));
} catch (e) { } catch (e) {
console.error(e); console.error(e);
console.error('Failed to re-encrypt custom field file. It seems to be corrupt.', _file); console.error('Failed to re-encrypt custom field file. It seems to be corrupt.', _file);
check_next_callback.bind(this)();
} }
}).bind(this)); }).bind(this));
/* jshint ignore:end */ /* jshint ignore:end */
} };
setTimeout(custom_field_workload.bind(this), 1);
}; };
var promise_revisions_update = function () { var promise_revisions_update = function () {