lbry-desktop/ui/util/diskspace.js

66 lines
2 KiB
JavaScript
Raw Normal View History

2022-02-18 23:46:55 +01:00
const { exec } = require('child_process');
export const diskSpaceLinux = (path) => {
return new Promise((resolve, reject) => {
exec(`df ${path}`, (error, stdout, stderr) => {
if (error) {
return reject(error);
2022-02-18 23:46:55 +01:00
}
if (stderr) {
return reject(new Error(stderr));
2022-02-18 23:46:55 +01:00
}
// Sample df command output:
// Filesystem 1K-blocks Used Available Use% Mounted on
// C:\ 185087700 120552556 64535144 66% /mnt/c
const dfResult = stdout.split('\n')[1].split(/\s+/);
resolve({
2022-06-05 18:10:12 +02:00
total: Number(dfResult[1]),
free: Number(dfResult[3]),
});
2022-02-18 23:46:55 +01:00
});
});
};
export const diskSpaceMac = (path) => {
// Escape spaces in path to prevent errors.
// Example:
// "/Users/username/Library/Application Support/LBRY" gets updated to
// "/Users/username/Library/Application\\ Support/LBRY"
const escapedPath = path.replace(/(\s+)/g, '\\$1');
return diskSpaceLinux(escapedPath);
};
export const diskSpaceWindows = (path) => {
return new Promise((resolve, reject) => {
exec(`wmic logicaldisk get size,freespace,caption`, (error, stdout, stderr) => {
if (error) {
return reject(error);
}
if (stderr) {
return reject(new Error(stderr));
}
// Drive used in the path (ie, C:, D:, etc.)
const pathDrive = path.split(':')[0] + ':';
// Sample outout:
// Caption FreeSpace Size
// C: 66218471424 189529804800
// D:
// E: 536829952 536854528
const stdoutLines = stdout.split('\n');
// Find the drive used in the path.
const driveLine = stdoutLines.find((line) => line.startsWith(pathDrive));
// Parse the values in each column by filtering out the
// empty spaces.
// eslint-disable-next-line no-unused-vars
const [drive, freeSpace, totalSize] = driveLine.split(' ').filter((x) => x);
resolve({
2022-06-05 18:10:12 +02:00
total: Math.floor(Number(totalSize) / 1024),
free: Math.floor(Number(freeSpace) / 1024),
});
});
});
};