SEARCH-197

- Error handling for compression and decompression
This commit is contained in:
Keerthi Niranjan 2017-10-24 16:37:56 +05:30
parent af985b6d46
commit 4bae7ab60e
4 changed files with 67 additions and 22 deletions

View File

@ -103,7 +103,7 @@
</div> </div>
<script> <script>
var search = new ssf.Search("7078106230763", "k2rbHBd5CgU3/PKLFUKUYkmuhCiL2Gw2bnsD7MdaZrU="); var search = new ssf.Search("16149077034436", "RSDFOiuVqF3WmY1pC4MP145qPOpUP6A4if27WG9y3fY=");
var buttonEl = document.getElementById('search'); var buttonEl = document.getElementById('search');
var merge = document.getElementById('merge'); var merge = document.getElementById('merge');
var buttonIndex = document.getElementById('index'); var buttonIndex = document.getElementById('index');

View File

@ -17,38 +17,65 @@ const devPath = path.join(__dirname, '..', '..', 'library', winArchPath);
const lz4Path = isDevEnv ? devPath : productionPath; const lz4Path = isDevEnv ? devPath : productionPath;
function compression(pathToFolder, outputPath, cb) { /**
* Using the child process to execute the tar and lz4
* compression and the final output of this function
* will be compressed file with ext: .tar.lz4
* @param pathToFolder
* @param outputPath
* @param callback
*/
function compression(pathToFolder, outputPath, callback) {
if (isMac) { if (isMac) {
child.exec(`cd "${DATA_FOLDER_PATH}" && tar cvf - "${pathToFolder}" | lz4 > "${outputPath}.tar.lz4"`, (error, stdout) => { child.exec(`cd "${DATA_FOLDER_PATH}" && tar cvf - "${pathToFolder}" | lz4 > "${outputPath}.tar.lz4"`, (error, stdout, stderr) => {
if (error) { if (error) {
return cb(new Error(error)); return callback(new Error(error), null);
} }
return cb(null, stdout); return callback(null, {
stderr: stderr.toString().trim(),
stdout: stdout.toString().trim()
});
}) })
} else { } else {
child.exec(`cd "${DATA_FOLDER_PATH}" && "${libraryFolderPath}\\tar-win.exe" cvf - "${pathToFolder}" | "${lz4Path}" > "${outputPath}.tar.lz4"`, (error, stdout) => { child.exec(`cd "${DATA_FOLDER_PATH}" && "${libraryFolderPath}\\tar-win.exe" cvf - "${pathToFolder}" | "${lz4Path}" > "${outputPath}.tar.lz4"`, (error, stdout, stderr) => {
if (error) { if (error) {
return cb(new Error(error)); return callback(new Error(error), null);
} }
return cb(null, stdout); return callback(null, {
stderr: stderr.toString().trim(),
stdout: stdout.toString().trim()
});
}) })
} }
} }
function deCompression(pathName, cb) { /**
* This function decompress the file
* and the ext should be .tar.lz4
* the output will be the user index folder
* @param pathName
* @param callback
*/
function deCompression(pathName, callback) {
if (isMac) { if (isMac) {
child.exec(`cd "${DATA_FOLDER_PATH}" && lz4 -d "${pathName}" | tar -xf - `, (error, stdout) => { child.exec(`cd "${DATA_FOLDER_PATH}" && lz4 -d "${pathName}" | tar -xf - `, (error, stdout, stderr) => {
if (error) { if (error) {
return cb(new Error(error)); return callback(new Error(error), null);
} }
return cb(null, stdout); return callback(null, {
stderr: stderr.toString().trim(),
stdout: stdout.toString().trim()
});
}) })
} else { } else {
child.exec(`cd "${DATA_FOLDER_PATH}" && "${lz4Path}" -d "${pathName}" | "${libraryFolderPath}\\tar-win.exe" xf - `, (error, stdout) => { child.exec(`cd "${DATA_FOLDER_PATH}" && "${lz4Path}" -d "${pathName}" | "${libraryFolderPath}\\tar-win.exe" xf - `, (error, stdout, stderr) => {
if (error) { if (error) {
return cb(new Error(error)); return callback(new Error(error), null);
} }
return cb(null, stdout); return callback(null, {
stderr: stderr.toString().trim(),
stdout: stdout.toString().trim()
});
}) })
} }
} }

View File

@ -6,6 +6,8 @@ const fs = require('fs');
const lz4 = require('../compressionLib'); const lz4 = require('../compressionLib');
const isDevEnv = require('../utils/misc.js').isDevEnv; const isDevEnv = require('../utils/misc.js').isDevEnv;
const crypto = require('./crypto'); const crypto = require('./crypto');
const log = require('../log.js');
const logLevels = require('../enums/logLevels.js');
const userData = path.join(app.getPath('userData')); const userData = path.join(app.getPath('userData'));
const DATA_FOLDER = isDevEnv ? './data' : path.join(userData, 'data'); const DATA_FOLDER = isDevEnv ? './data' : path.join(userData, 'data');
@ -25,23 +27,27 @@ class Crypto {
} }
/** /**
* Creates a zip of the data folder and encrypting * Compressing the user index folder and
* removing the data folder and the dump files * encrypting it
* @returns {Promise} * @returns {Promise}
*/ */
encryption() { encryption() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!fs.existsSync(this.indexDataFolder)){ if (!fs.existsSync(this.indexDataFolder)){
// will be handling after implementing in client app log.send(logLevels.ERROR, 'user index folder not found');
reject(); reject();
return; return;
} }
lz4.compression(`data/${this.permanentIndexFolderName}`, `${this.permanentIndexFolderName}`, (error) => { lz4.compression(`data/${this.permanentIndexFolderName}`, `${this.permanentIndexFolderName}`, (error, response) => {
if (error) { if (error) {
log.send(logLevels.ERROR, 'lz4 compression error: ' + error);
reject(error); reject(error);
return;
} }
log.send(logLevels.WARN, 'compression stderr, ' + response.stderr);
const input = fs.createReadStream(`${this.dump}/${this.permanentIndexFolderName}.tar.lz4`); const input = fs.createReadStream(`${this.dump}/${this.permanentIndexFolderName}.tar.lz4`);
const outputEncryption = fs.createWriteStream(this.encryptedIndex); const outputEncryption = fs.createWriteStream(this.encryptedIndex);
let config = { let config = {
@ -53,7 +59,9 @@ class Crypto {
encryptionProcess.on('finish', (err) => { encryptionProcess.on('finish', (err) => {
if (err) { if (err) {
log.send(logLevels.ERROR, 'encryption error: ' + err);
reject(new Error(err)); reject(new Error(err));
return;
} }
fs.unlinkSync(`${this.dump}/${this.permanentIndexFolderName}.tar.lz4`); fs.unlinkSync(`${this.dump}/${this.permanentIndexFolderName}.tar.lz4`);
resolve('Success'); resolve('Success');
@ -71,7 +79,7 @@ class Crypto {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!fs.existsSync(this.encryptedIndex)){ if (!fs.existsSync(this.encryptedIndex)){
// will be handling after implementing in client app log.send(logLevels.ERROR, 'encrypted file not found');
reject(); reject();
return; return;
} }
@ -88,12 +96,18 @@ class Crypto {
decryptionProcess.on('finish', () => { decryptionProcess.on('finish', () => {
if (!fs.existsSync(`${this.dump}/decrypted.tar.lz4`)){ if (!fs.existsSync(`${this.dump}/decrypted.tar.lz4`)){
// will be handling after implementing in client app log.send(logLevels.ERROR, 'decrypted.tar.lz4 file not found');
reject(); reject();
return; return;
} }
lz4.deCompression(`${this.dump}/decrypted.tar.lz4`,() => { lz4.deCompression(`${this.dump}/decrypted.tar.lz4`,(error, response) => {
if (error) {
log.send(logLevels.ERROR, 'lz4 deCompression error, ' + error);
// no return, need to unlink if error
}
log.send(logLevels.WARN, 'deCompression stderr, ' + response.stderr);
fs.unlink(`${this.dump}/decrypted.tar.lz4`, () => { fs.unlink(`${this.dump}/decrypted.tar.lz4`, () => {
resolve('success'); resolve('success');
}); });

View File

@ -67,6 +67,10 @@ class Search {
this.decryptAndInit(); this.decryptAndInit();
} }
/**
* Decrypting the existing user .enc file
* and initialing the library
*/
decryptAndInit() { decryptAndInit() {
this.crypto.decryption().then(() => { this.crypto.decryption().then(() => {
console.timeEnd('Decrypting'); console.timeEnd('Decrypting');