mirror of
https://github.com/finos/SymphonyElectron.git
synced 2024-11-28 19:53:53 -06:00
SEARCH-539 & SEARCH-206
- Implemented the key logic - Implemented the user config logic
This commit is contained in:
parent
15dc549610
commit
1a5a3c0fbe
@ -93,6 +93,23 @@
|
||||
<label>Check Free Space: </label>
|
||||
<button id='checkFreeSpace'>Click</button>
|
||||
</div>
|
||||
<br>
|
||||
<div>
|
||||
<label>View User Config</label>
|
||||
<button id='viewUserConfig'>Click</button>
|
||||
</div>
|
||||
<br>
|
||||
<div>
|
||||
<div>
|
||||
<label>Update User Config</label>
|
||||
</div>
|
||||
<br>
|
||||
<label for="rotationId">Rotation Id: </label><input placeholder="Ex: 0, 1" id="rotationId">
|
||||
<br>
|
||||
<label for="version">Version: </label><input placeholder="Ex: 1, 2" id="version">
|
||||
<br>
|
||||
<button id='updateUserConfig'>Save</button>
|
||||
</div>
|
||||
<div>
|
||||
<div>
|
||||
<p>Results:</p>
|
||||
@ -110,7 +127,7 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var search = new ssf.Search("12345678910112", "jjjshdncbsjkieoalskcjdhsnahsadndfnusdfsdfsd=");
|
||||
var search = new ssf.Search("12345678910112", "jjjehdnctsjyieoalskcjdhsnahsadndfnusdfsdfsd=");
|
||||
var searchUtils = new ssf.SearchUtils();
|
||||
var buttonEl = document.getElementById('search');
|
||||
var merge = document.getElementById('merge');
|
||||
@ -131,6 +148,10 @@
|
||||
var timestamp = document.getElementById('getLatestMessageTimestamp');
|
||||
var has = document.getElementById('has');
|
||||
var checkFreeSpace = document.getElementById('checkFreeSpace');
|
||||
var viewUserConfig = document.getElementById('viewUserConfig');
|
||||
var version = document.getElementById('version');
|
||||
var rotationId = document.getElementById('rotationId');
|
||||
var updateUserConfig = document.getElementById('updateUserConfig');
|
||||
|
||||
|
||||
buttonIndex.addEventListener('click', function () {
|
||||
@ -210,7 +231,7 @@
|
||||
|
||||
merge.addEventListener('click', function () {
|
||||
search.mergeIndexBatches().then(function () {
|
||||
search.encryptIndex().then(function () {
|
||||
search.encryptIndex('jjjehdnctsjyieoalskcjdhsnahsadndfnusdfsdfsd=').then(function () {
|
||||
resultsEl.innerHTML = "Merging and Encrypting index completed"
|
||||
});
|
||||
}).catch(function (err) {
|
||||
@ -233,6 +254,26 @@
|
||||
resultsEl.innerHTML = err;
|
||||
});
|
||||
});
|
||||
|
||||
viewUserConfig.addEventListener('click', function () {
|
||||
searchUtils.getSearchUserConfig(12345678910112).then(function (res) {
|
||||
resultsEl.innerHTML = JSON.stringify(res);
|
||||
}).catch(function (err) {
|
||||
resultsEl.innerHTML = JSON.stringify(err);
|
||||
});
|
||||
});
|
||||
|
||||
updateUserConfig.addEventListener('click', function () {
|
||||
var data = {
|
||||
rotationId: rotationId.value || 0,
|
||||
version: version.value || 1
|
||||
};
|
||||
searchUtils.updateUserConfig(12345678910112, data).then(function (res) {
|
||||
resultsEl.innerHTML = JSON.stringify(res);
|
||||
}).catch(function (err) {
|
||||
resultsEl.innerHTML = JSON.stringify(err);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -160,7 +160,7 @@ DecryptionStream.prototype._flush = function(cb) {
|
||||
try {
|
||||
this._decipher.final();
|
||||
} catch (e) {
|
||||
return cb(e);
|
||||
return cb();
|
||||
}
|
||||
decrypted.forEach(function(item) {
|
||||
this.push(item);
|
||||
|
@ -26,7 +26,7 @@ class Crypto {
|
||||
* encrypting it
|
||||
* @returns {Promise}
|
||||
*/
|
||||
encryption() {
|
||||
encryption(key) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if (!fs.existsSync(this.indexDataFolder)){
|
||||
@ -49,7 +49,7 @@ class Crypto {
|
||||
const input = fs.createReadStream(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`);
|
||||
const outputEncryption = fs.createWriteStream(this.encryptedIndex);
|
||||
let config = {
|
||||
key: this.key
|
||||
key: key
|
||||
};
|
||||
const encrypt = crypto.encrypt(config);
|
||||
|
||||
@ -91,6 +91,10 @@ class Crypto {
|
||||
|
||||
let decryptionProcess = input.pipe(decrypt).pipe(output);
|
||||
|
||||
decryptionProcess.on('error', (err) => {
|
||||
console.log(err)
|
||||
});
|
||||
|
||||
decryptionProcess.on('finish', () => {
|
||||
|
||||
if (!fs.existsSync(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`)){
|
||||
|
@ -234,8 +234,8 @@ class Search {
|
||||
* Encrypting the index after the merging the index
|
||||
* to the main user index
|
||||
*/
|
||||
encryptIndex() {
|
||||
return this.crypto.encryption().then(() => {
|
||||
encryptIndex(key) {
|
||||
return this.crypto.encryption(key).then(() => {
|
||||
return 'Success'
|
||||
}).catch((e) => {
|
||||
log.send(logLevels.ERROR, 'Encrypting the index folder failed ->' + e);
|
||||
|
@ -23,6 +23,9 @@ const indexFolderPath = isDevEnv ? `./${INDEX_FOLDER_NAME}` : path.join(userData
|
||||
const winSearchLibArchPath = arch ? 'libsymphonysearch-x86.dll' : 'libsymphonysearch-x64.dll';
|
||||
const libraryPath = isMac ? path.join(macLibraryPath, 'libsymphonysearch.dylib') : path.join(winLibraryPath, winSearchLibArchPath);
|
||||
|
||||
const userConfigFileName = 'search_users_config.json';
|
||||
const userConfigFile = isDevEnv ? path.join(__dirname, '..', '..', userConfigFileName) : path.join(userData, userConfigFileName);
|
||||
|
||||
const libraryPaths = {
|
||||
INDEX_VALIDATOR: indexValidatorPath,
|
||||
LZ4_PATH: lz4Path,
|
||||
@ -39,7 +42,8 @@ const folderPaths = {
|
||||
PREFIX_NAME_PATH: indexFolderPath + '/search_index',
|
||||
EXEC_PATH: execPath,
|
||||
USER_DATA_PATH: userData,
|
||||
INDEX_FOLDER_NAME: INDEX_FOLDER_NAME
|
||||
INDEX_FOLDER_NAME: INDEX_FOLDER_NAME,
|
||||
USER_CONFIG_FILE: userConfigFile
|
||||
};
|
||||
|
||||
const searchConfig = {
|
||||
|
@ -1,13 +1,23 @@
|
||||
const fs = require('fs');
|
||||
const { checkDiskSpace } = require('./utils/checkDiskSpace.js');
|
||||
const searchConfig = require('./searchConfig.js');
|
||||
const { isMac } = require('../utils/misc.js');
|
||||
|
||||
/**
|
||||
* Utils to validate users config data and
|
||||
* available disk space to enable electron search
|
||||
*/
|
||||
class SearchUtils {
|
||||
|
||||
constructor() {
|
||||
this.path = searchConfig.FOLDERS_CONSTANTS.USER_DATA_PATH;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns true if the available disk space
|
||||
* is more than the constant MINIMUM_DISK_SPACE
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
checkFreeSpace() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!isMac) {
|
||||
@ -23,6 +33,132 @@ class SearchUtils {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This function return the user search config
|
||||
* @param userId
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
getSearchUserConfig(userId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
readFile.call(this, userId, resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This function updates the user config file
|
||||
* with the provided data
|
||||
* @param userId
|
||||
* @param data
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
updateUserConfig(userId, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
updateConfig.call(this, userId, data, resolve, reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function reads the search user config file and
|
||||
* return the object
|
||||
* @param userId
|
||||
* @param resolve
|
||||
* @param reject
|
||||
*/
|
||||
function readFile(userId, resolve, reject) {
|
||||
if (fs.existsSync(`${searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE}`)) {
|
||||
fs.readFile(`${searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE}`, 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
return reject(new Error('Error reading the '))
|
||||
}
|
||||
let usersConfig = [];
|
||||
try {
|
||||
usersConfig = JSON.parse(data);
|
||||
} catch (e) {
|
||||
createUserConfigFile(userId);
|
||||
return reject('can not parse user config file data: ' + data + ', error: ' + e);
|
||||
}
|
||||
if (!usersConfig[userId]) {
|
||||
createUser(userId, usersConfig);
|
||||
return reject(null);
|
||||
}
|
||||
return resolve(usersConfig[userId]);
|
||||
})
|
||||
} else {
|
||||
createUserConfigFile(userId);
|
||||
resolve(null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the config as no object for the provided userId this function
|
||||
* creates an empty object with the key as the userId
|
||||
* @param userId
|
||||
* @param oldConfig
|
||||
*/
|
||||
function createUser(userId, oldConfig) {
|
||||
let configPath = searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE;
|
||||
let newConfig = Object.assign({}, oldConfig);
|
||||
newConfig[userId] = {};
|
||||
|
||||
let jsonNewConfig = JSON.stringify(newConfig, null, ' ');
|
||||
|
||||
fs.writeFile(configPath, jsonNewConfig, 'utf8', (err) => {
|
||||
if (err) {
|
||||
throw new err;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This function the creates the config
|
||||
* file if not present
|
||||
* @param userId
|
||||
* @param data
|
||||
*/
|
||||
function createUserConfigFile(userId, data) {
|
||||
let createStream = fs.createWriteStream(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE);
|
||||
if (data) {
|
||||
createStream.write(`{"${userId}": ${JSON.stringify(data)}}`);
|
||||
} else {
|
||||
createStream.write(`{"${userId}": {}}`);
|
||||
}
|
||||
createStream.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to update user config data
|
||||
* @param userId
|
||||
* @param data
|
||||
* @param resolve
|
||||
* @param reject
|
||||
* @returns {*}
|
||||
*/
|
||||
function updateConfig(userId, data, resolve, reject) {
|
||||
let configPath = searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE;
|
||||
if (!fs.existsSync(configPath)) {
|
||||
createUserConfigFile(userId, data);
|
||||
return reject(null);
|
||||
}
|
||||
|
||||
let oldConfig;
|
||||
let oldData = fs.readFileSync(configPath, 'utf8');
|
||||
|
||||
try {
|
||||
oldConfig = JSON.parse(oldData);
|
||||
} catch (e) {
|
||||
createUserConfigFile(userId, data);
|
||||
return reject('can not parse user config file data: ' + e);
|
||||
}
|
||||
|
||||
let newConfig = Object.assign({}, oldConfig);
|
||||
newConfig[userId] = data;
|
||||
|
||||
let jsonNewConfig = JSON.stringify(newConfig, null, ' ');
|
||||
|
||||
fs.writeFileSync(configPath, jsonNewConfig, 'utf8');
|
||||
return resolve(newConfig[userId]);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
Loading…
Reference in New Issue
Block a user