SEARCH-116-GCM Changed the path from data to user folder

This commit is contained in:
Keerthi Niranjan 2017-08-18 17:28:30 +05:30 committed by Keerthi Niranjan
parent 52d6a9e867
commit 4053d22dfc
2 changed files with 62 additions and 19 deletions

View File

@ -10,13 +10,20 @@ const isDevEnv = require('../utils/misc.js').isDevEnv;
const crypto = require('./crypto');
const userData = path.join(app.getPath('userData'));
const INDEX_DATA_FOLDER = isDevEnv ? './data' : path.join(userData, 'data');
const INDEX_DATA_FOLDER = isDevEnv ? './data/search_index' : path.join(userData, 'data/search_index');
const TEMPORARY_PATH = isDevEnv ? path.join(__dirname, '..', '..') : userData;
class Crypto {
// TODO: Need to pass key for encrypting and decrypting
constructor() {
this.indexDataFolder = INDEX_DATA_FOLDER;
// will be handling after implementing in client app
let userId = 'user_data';
let INDEX_VERSION = 'v1';
// will be handling after implementing in client app
this.indexDataFolder = INDEX_DATA_FOLDER + '_' + userId + '_' + INDEX_VERSION;
this.dump = TEMPORARY_PATH;
this.key = "XrwVgWR4czB1a9scwvgRUNbXiN3W0oWq7oUBenyq7bo="; // temporary only
this.encryptedIndex = 'encryptedIndex.enc';
@ -31,9 +38,22 @@ class Crypto {
encryption() {
return new Promise((resolve, reject) => {
if (!fs.existsSync(this.indexDataFolder)){
// will be handling after implementing in client app
reject();
return;
}
let output = fs.createWriteStream(`${this.dump}/content.zip`);
output.on('close', () => {
zipArchive.on('end', () => {
if (!fs.existsSync(`${this.dump}/content.zip`)){
// will be handling after implementing in client app
reject();
return;
}
const input = fs.createReadStream(`${this.dump}/content.zip`);
const outputEncryption = fs.createWriteStream(this.encryptedIndex);
@ -61,7 +81,7 @@ class Crypto {
zipArchive.pipe(output);
zipArchive.directory(this.indexDataFolder, true);
zipArchive.directory(this.indexDataFolder);
zipArchive.finalize((err) => {
if (err) {
@ -79,6 +99,13 @@ class Crypto {
*/
decryption() {
return new Promise((resolve, reject) => {
if (!fs.existsSync(this.encryptedIndex)){
// will be handling after implementing in client app
reject();
return;
}
const input = fs.createReadStream(this.encryptedIndex);
const output = fs.createWriteStream(`${this.dump}/decrypted.zip`);
let config = {
@ -87,20 +114,27 @@ class Crypto {
const decrypt = crypto.decrypt(config);
input.pipe(decrypt).pipe(output).on('finish', () => {
if (!fs.existsSync(`${this.dump}/decrypted.zip`)){
// 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"));
}
zip();
extractZip();
})
.on('error', (error) => {
reject(new Error(error.message));
});
});
let zip = () => {
let extractZip = () => {
extract(`${this.dump}/decrypted.zip`, {dir: TEMPORARY_PATH}, (err) => {
if (err) {
reject(new Error(err));

View File

@ -14,11 +14,12 @@ const childProcess = require('child_process');
const path = require('path');
const AppDirectory = require('appdirectory');
const dirs = new AppDirectory('Symphony');
const Crypto = require('./cryptoLib/index');
const Crypto = require('./cryptoLib');
const crypto = new Crypto();
require('electron-dl')();
// used to check if a url was opened when the app was already open
let isAppAlreadyOpen = false;
@ -58,14 +59,17 @@ var symphonyAutoLauncher = new AutoLaunch({
path: process.execPath,
});
let crypto = new Crypto();
/**
* This is for demo purpose only
* will be removing this after implementing
* in the client-app
*/
crypto.decryption()
.then(function () {
console.log('success')
// will be handling after implementing client app
})
.catch(function (err) {
console.log(err)
.catch(function () {
// will be handling after implementing client app
});
/**
@ -89,15 +93,20 @@ app.on('activate', function () {
app.on('will-quit', function (e) {
e.preventDefault();
/**
* This is for demo purpose only
* will be removing this after implementing
* in client-app
*/
crypto.encryption()
.then(function (err, res) {
if (err) {
throw new Error(err);
}
.then(function () {
// will be handling after implementing in client app
app.exit();
})
.catch(function (err) {
electron.dialog.showErrorBox('error', err);
.catch(function () {
// will be handling after implementing client app
app.exit();
});
});