SymphonyElectron/js/search/utils/checkDiskSpace.js

45 lines
1.6 KiB
JavaScript
Raw Normal View History

const { exec } = require('child_process');
const { isMac } = require('../../utils/misc');
2017-12-29 06:06:40 -06:00
const searchConfig = require('../searchConfig.js');
2017-12-29 06:06:40 -06:00
function checkDiskSpace(path, resolve, reject) {
if (!path) {
2017-12-29 06:06:40 -06:00
reject(new Error("Please provide path"));
return;
}
if (isMac) {
exec("df -k '" + path.replace(/'/g,"'\\''") + "'", (error, stdout, stderr) => {
if (error) {
if (stderr.indexOf("No such file or directory") !== -1) {
2017-12-29 06:06:40 -06:00
return reject(new Error("No such file or directory : " + error))
}
2017-12-29 06:06:40 -06:00
return reject(new Error("Error : " + error));
}
let data = stdout.trim().split("\n");
let disk_info_str = data[data.length - 1].replace( /[\s\n\r]+/g,' ');
let freeSpace = disk_info_str.split(' ');
2017-12-29 06:06:40 -06:00
let space = freeSpace[3] * 1024;
return resolve(space >= searchConfig.MINIMUM_DISK_SPACE);
});
} else {
exec(`fsutil volume diskfree ${path}`, (error, stdout, stderr) => {
if (error) {
if (stderr.indexOf("No such file or directory") !== -1) {
2017-12-29 06:06:40 -06:00
return reject(new Error("No such file or directory : " + error));
}
2017-12-29 06:06:40 -06:00
return reject(new Error("Error : " + error));
}
let data = stdout.trim().split("\n");
let disk_info_str = data[data.length - 1].split(':');
2017-12-29 06:06:40 -06:00
return resolve(disk_info_str[1] >= searchConfig.MINIMUM_DISK_SPACE);
});
}
}
module.exports = {
checkDiskSpace: checkDiskSpace
};