SEARCH-197

- Removed archiver
- Removed extract-zip
- Implemented new compression algorithm (lz4)
This commit is contained in:
Keerthi Niranjan
2017-10-17 12:11:13 +05:30
parent ab963f4567
commit a515ab3c35
58 changed files with 17082 additions and 56 deletions

View File

@@ -3,8 +3,7 @@ const electron = require('electron');
const app = electron.app;
const path = require('path');
const fs = require('fs');
const archiver = require('archiver');
const extract = require('extract-zip');
const lz4 = require('../compressionLib');
const isDevEnv = require('../utils/misc.js').isDevEnv;
const crypto = require('./crypto');
@@ -21,10 +20,8 @@ class Crypto {
this.permanentIndexFolderName = 'search_index_' + userId + '_' + INDEX_VERSION;
this.dump = TEMPORARY_PATH;
this.key = key;
this.extractToPath = `${TEMPORARY_PATH}/data/${this.permanentIndexFolderName}`;
this.encryptedIndex = `${TEMPORARY_PATH}/${this.permanentIndexFolderName}.enc`;
this.dataFolder = DATA_FOLDER;
this.zipErrored = false;
}
/**
@@ -41,19 +38,11 @@ class Crypto {
return;
}
const zipArchive = archiver('zip');
let output = fs.createWriteStream(`${this.dump}/${this.permanentIndexFolderName}.zip`);
zipArchive.on('end', () => {
if (!fs.existsSync(`${this.dump}/${this.permanentIndexFolderName}.zip`)){
// will be handling after implementing in client app
reject();
return;
lz4.compression(`${this.indexDataFolder}`, `${this.dump}/${this.permanentIndexFolderName}`, (error) => {
if (error) {
reject(error);
}
const input = fs.createReadStream(`${this.dump}/${this.permanentIndexFolderName}.zip`);
const input = fs.createReadStream(`${this.dump}/${this.permanentIndexFolderName}.tar.lz4`);
const outputEncryption = fs.createWriteStream(this.encryptedIndex);
let config = {
key: this.key
@@ -64,23 +53,10 @@ class Crypto {
if (err) {
reject(new Error(err));
}
if (!this.zipErrored) {
fs.unlinkSync(`${this.dump}/${this.permanentIndexFolderName}.zip`);
resolve('Success');
}
fs.unlinkSync(`${this.dump}/${this.permanentIndexFolderName}.tar.lz4`);
resolve('Success');
});
});
zipArchive.pipe(output);
zipArchive.directory(this.indexDataFolder + '/', false);
zipArchive.finalize((err) => {
if (err) {
this.zipErrored = true;
reject(new Error(err));
}
});
});
}
@@ -99,7 +75,7 @@ class Crypto {
}
const input = fs.createReadStream(this.encryptedIndex);
const output = fs.createWriteStream(`${this.dump}/decrypted.zip`);
const output = fs.createWriteStream(`${this.dump}/decrypted.tar.lz4`);
let config = {
key: this.key
};
@@ -107,35 +83,18 @@ class Crypto {
input.pipe(decrypt).pipe(output).on('finish', () => {
if (!fs.existsSync(`${this.dump}/decrypted.zip`)){
if (!fs.existsSync(`${this.dump}/decrypted.tar.lz4`)){
// will be handling after implementing in client app
reject();
return;
}
let readStream = fs.createReadStream(`${this.dump}/decrypted.zip`);
readStream
.on('data', (data) => {
if (!data) {
reject(new Error("error reading zip"));
}
extractZip();
})
.on('error', (error) => {
reject(new Error(error.message));
});
});
let extractZip = () => {
extract(`${this.dump}/decrypted.zip`, {dir: `${this.extractToPath}`}, (err) => {
if (err) {
reject(new Error(err));
}
fs.unlink(`${this.dump}/decrypted.zip`, () => {
lz4.deCompression(`${this.dump}/decrypted.tar.lz4`,() => {
fs.unlink(`${this.dump}/decrypted.tar.lz4`, () => {
resolve('success');
});
})
}
});
});
}