SEARCH-563

- Deleting the tmp files
This commit is contained in:
Keerthi Niranjan 2018-01-09 13:37:03 +05:30
parent f4ff287547
commit 77ac2102d4
2 changed files with 34 additions and 4 deletions

View File

@ -10,6 +10,9 @@ let Transform = stream.Transform;
let util = require('util');
let crypto = require('crypto');
const log = require('../log.js');
const logLevels = require('../enums/logLevels.js');
let KEY_LENGTH = 32; // bytes
let GCM_NONCE_LENGTH = 12; //bytes
let GCM_MAC_LENGTH = 16; //bytes
@ -151,7 +154,8 @@ DecryptionStream.prototype._transform = function(chunk, enc, cb) {
DecryptionStream.prototype._flush = function(cb) {
let mac = pullOutMac(this._cipherTextChunks);
if (!mac) {
return this.emit('error', new Error('Decryption failed: bad cipher text.'));
log.send(logLevels.ERROR, 'Crypto: Decryption failed: bad cipher text.');
return cb();
}
this._decipher.setAuthTag(mac);
let decrypted = this._cipherTextChunks.map(function(item) {
@ -160,6 +164,7 @@ DecryptionStream.prototype._flush = function(cb) {
try {
this._decipher.final();
} catch (e) {
log.send(logLevels.ERROR, 'Crypto: Decryption Failed: ' + e);
return cb();
}
decrypted.forEach(function(item) {

View File

@ -56,17 +56,32 @@ class Crypto {
let config = {
key: key
};
const encrypt = crypto.encrypt(config);
let encrypt;
try {
encrypt = crypto.encrypt(config);
} catch (e) {
log.send(logLevels.ERROR, 'Error decrypting : ' + e);
if (fs.existsSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`)) {
fs.unlinkSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`);
}
reject();
return;
}
let encryptionProcess = input.pipe(encrypt).pipe(outputEncryption);
encryptionProcess.on('finish', (err) => {
if (err) {
log.send(logLevels.ERROR, 'Crypto: Error while encrypting the compressed file: ' + err);
if (fs.existsSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`)) {
fs.unlinkSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`);
}
reject(new Error(err));
return;
}
fs.unlinkSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`);
if (fs.existsSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`)) {
fs.unlinkSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`);
}
resolve('Success');
});
});
@ -92,7 +107,17 @@ class Crypto {
let config = {
key: this.key
};
const decrypt = crypto.decrypt(config);
let decrypt;
try {
decrypt = crypto.decrypt(config);
} catch (e) {
log.send(logLevels.ERROR, 'Error decrypting : ' + e);
if (fs.existsSync(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`)) {
fs.unlinkSync(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`);
}
reject();
return;
}
let decryptionProcess = input.pipe(decrypt).pipe(output);