2017-11-09 08:50:59 -06:00
|
|
|
const child = require('child_process');
|
2017-10-17 10:45:27 -05:00
|
|
|
const path = require('path');
|
2017-11-09 08:50:59 -06:00
|
|
|
const isMac = require('../utils/misc.js').isMac;
|
2017-10-17 10:45:27 -05:00
|
|
|
const isDevEnv = require('../utils/misc.js').isDevEnv;
|
2017-11-27 06:51:30 -06:00
|
|
|
const searchConfig = require('../search/searchConfig.js');
|
|
|
|
const ROOT_PATH = isDevEnv ? path.join(__dirname, '..', '..') : searchConfig.FOLDERS_CONSTANTS.USER_DATA_PATH;
|
2017-10-18 05:31:51 -05:00
|
|
|
|
2017-10-24 06:07:56 -05:00
|
|
|
/**
|
|
|
|
* Using the child process to execute the tar and lz4
|
|
|
|
* compression and the final output of this function
|
|
|
|
* will be compressed file with ext: .tar.lz4
|
|
|
|
* @param pathToFolder
|
|
|
|
* @param outputPath
|
|
|
|
* @param callback
|
|
|
|
*/
|
|
|
|
function compression(pathToFolder, outputPath, callback) {
|
2017-10-17 01:41:13 -05:00
|
|
|
if (isMac) {
|
2017-11-27 06:51:30 -06:00
|
|
|
child.exec(`cd "${ROOT_PATH}" && tar cf - "${pathToFolder}" | "${searchConfig.LIBRARY_CONSTANTS.MAC_LIBRARY_FOLDER}/lz4.exec" > "${outputPath}.tar.lz4"`, (error, stdout, stderr) => {
|
2017-10-17 01:41:13 -05:00
|
|
|
if (error) {
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(new Error(error), null);
|
2017-10-17 01:41:13 -05:00
|
|
|
}
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(null, {
|
|
|
|
stderr: stderr.toString().trim(),
|
|
|
|
stdout: stdout.toString().trim()
|
|
|
|
});
|
2017-10-17 01:41:13 -05:00
|
|
|
})
|
|
|
|
} else {
|
2017-11-27 06:51:30 -06:00
|
|
|
child.exec(`cd "${ROOT_PATH}" && "${searchConfig.LIBRARY_CONSTANTS.WIN_LIBRARY_FOLDER}\\tar-win.exe" cf - "${pathToFolder}" | "${searchConfig.LIBRARY_CONSTANTS.LZ4_PATH}" > "${outputPath}.tar.lz4"`, (error, stdout, stderr) => {
|
2017-10-17 01:41:13 -05:00
|
|
|
if (error) {
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(new Error(error), null);
|
2017-10-17 01:41:13 -05:00
|
|
|
}
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(null, {
|
|
|
|
stderr: stderr.toString().trim(),
|
|
|
|
stdout: stdout.toString().trim()
|
|
|
|
});
|
2017-10-17 01:41:13 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 06:07:56 -05:00
|
|
|
/**
|
|
|
|
* This function decompress the file
|
|
|
|
* and the ext should be .tar.lz4
|
|
|
|
* the output will be the user index folder
|
|
|
|
* @param pathName
|
|
|
|
* @param callback
|
|
|
|
*/
|
|
|
|
function deCompression(pathName, callback) {
|
2017-10-17 01:41:13 -05:00
|
|
|
if (isMac) {
|
2017-11-27 06:51:30 -06:00
|
|
|
child.exec(`cd "${ROOT_PATH}" && "${searchConfig.LIBRARY_CONSTANTS.MAC_LIBRARY_FOLDER}/lz4.exec" -d "${pathName}" | tar -xf - `, (error, stdout, stderr) => {
|
2017-10-17 01:41:13 -05:00
|
|
|
if (error) {
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(new Error(error), null);
|
2017-10-17 01:41:13 -05:00
|
|
|
}
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(null, {
|
|
|
|
stderr: stderr.toString().trim(),
|
|
|
|
stdout: stdout.toString().trim()
|
|
|
|
});
|
2017-10-17 01:41:13 -05:00
|
|
|
})
|
|
|
|
} else {
|
2017-11-27 06:51:30 -06:00
|
|
|
child.exec(`cd "${ROOT_PATH}" && "${searchConfig.LIBRARY_CONSTANTS.LZ4_PATH}" -d "${pathName}" | "${searchConfig.LIBRARY_CONSTANTS.WIN_LIBRARY_FOLDER}\\tar-win.exe" xf - `, (error, stdout, stderr) => {
|
2017-10-17 01:41:13 -05:00
|
|
|
if (error) {
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(new Error(error), null);
|
2017-10-17 01:41:13 -05:00
|
|
|
}
|
2017-10-24 06:07:56 -05:00
|
|
|
return callback(null, {
|
|
|
|
stderr: stderr.toString().trim(),
|
|
|
|
stdout: stdout.toString().trim()
|
|
|
|
});
|
2017-10-17 01:41:13 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
compression,
|
|
|
|
deCompression
|
|
|
|
};
|