SEARCH-539 & SEARCH-206

- Implemented the key logic
- Implemented the user config logic
This commit is contained in:
Keerthi Niranjan
2017-12-18 10:26:11 +05:30
parent 15dc549610
commit 1a5a3c0fbe
6 changed files with 193 additions and 8 deletions

View File

@@ -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);

View File

@@ -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 = {

View File

@@ -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 = {