SEARCH-116 - Temporary push

This commit is contained in:
Keerthi Niranjan 2017-08-14 10:47:01 +05:30 committed by Keerthi Niranjan
parent ebb50ae344
commit f6ae42f854
3 changed files with 124 additions and 4 deletions

View File

@ -2,23 +2,115 @@
const electron = require('electron');
const app = electron.app;
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const archiver = require('archiver');
const zipArchive = archiver('zip');
const extract = require('extract-zip');
const isDevEnv = require('../utils/misc.js').isDevEnv;
const userData = path.join(app.getPath('userData'));
const INDEX_DATA_FOLDER = isDevEnv ? './data' : path.join(userData, 'data');
const INDEX_DATA_FOLDER = isDevEnv ? './msgsjson' : path.join(userData, 'data');
class Crypto {
constructor() {
this.indexDataFolder = INDEX_DATA_FOLDER;
this.decipher = crypto.createDecipher('aes256', 'temp');
this.cipher = crypto.createCipher('aes256', "temp");
this.dump = path.join(__dirname, '..', '..');
this.encryptedIndex = 'encryptedIndex.enc';
this.zipErrored = false;
}
encryption() {
console.log(this.indexDataFolder)
let self = this;
return new Promise(function (resolve, reject) {
let output = fs.createWriteStream(`${self.dump}/content.zip`);
output.on('close', function () {
const input = fs.createReadStream(`${self.dump}/content.zip`);
const outPutEncryption = fs.createWriteStream(self.encryptedIndex);
input.pipe(self.cipher).pipe(outPutEncryption).on('finish', function (err, res) {
if (err) {
reject(new Error(err));
}
if (!self.zipErrored) {
fs.unlinkSync(`${self.dump}/content.zip`);
Crypto.deleteFolderRecursive(self.indexDataFolder)
.then(function () {
resolve(res);
})
.catch(function (error) {
console.log(error)
});
}
});
});
zipArchive.pipe(output);
zipArchive.directory(self.indexDataFolder, true);
zipArchive.finalize(function (err) {
if (err) {
self.zipErrored = true;
reject(new Error(err));
}
});
});
}
decryption() {
console.log(this.indexDataFolder)
let self = this;
return new Promise(function (resolve, reject) {
const input = fs.createReadStream(self.encryptedIndex);
const output = fs.createWriteStream(`${self.dump}/decrypted.zip`);
function unzip() {
let temp = path.join(__dirname, '..', '..');
extract(`${self.dump}/decrypted.zip`, {dir: temp}, function (err) {
if (err) reject(err);
fs.unlink(`${self.dump}/decrypted.zip`, function () {
resolve('success')
});
})
}
input.pipe(self.decipher).pipe(output).on('finish', function () {
var readStream = fs.createReadStream(`${self.dump}/decrypted.zip`);
readStream
.on('data', function (data) {
if (!data) reject("error reading zip");
unzip();
})
.on('error', function (error) {
console.log('Error:', error.message);
});
});
});
}
static deleteFolderRecursive(pt) {
return new Promise(function (resolve, reject) {
if (fs.existsSync(pt)) {
fs.readdirSync(pt).forEach(function (file) {
var curPath = pt + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) {
Crypto.deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
resolve(fs.rmdirSync(pt));
} else {
reject('no file');
}
});
}
}

View File

@ -15,6 +15,8 @@ const path = require('path');
const AppDirectory = require('appdirectory');
const dirs = new AppDirectory('Symphony');
const Crypto = require('./cryptoLib/index');
require('electron-dl')();
// used to check if a url was opened when the app was already open
@ -56,6 +58,16 @@ var symphonyAutoLauncher = new AutoLaunch({
path: process.execPath,
});
let crypto = new Crypto();
crypto.decryption()
.then(function () {
console.log('success')
})
.catch(function (err) {
console.log(err)
});
/**
* This method will be called when Electron has finished
* initialization and is ready to create browser windows.
@ -75,6 +87,20 @@ app.on('activate', function () {
}
});
app.on('will-quit', function (e) {
e.preventDefault();
crypto.encryption()
.then(function (err, res) {
if (err) {
throw new Error(err);
}
app.exit();
})
.catch(function (err) {
electron.dialog.showErrorBox('error', err);
});
});
// adds 'symphony' as a protocol
// in the system. plist file in macOS
// and registry keys in windows

View File

@ -97,7 +97,9 @@
"electron-squirrel-startup": "^1.0.0",
"filesize": "^3.5.10",
"keymirror": "0.1.1",
"winreg": "^1.2.3"
"winreg": "^1.2.3",
"archiver": "latest",
"extract-zip": "latest"
},
"optionalDependencies": {
"screen-snippet": "git+https://github.com/symphonyoss/ScreenSnippet.git#v1.0.1"