Add disk space functions for windows and mac.

This commit is contained in:
Franco Montenegro 2022-03-01 15:38:53 -03:00 committed by jessopb
parent ca748fd16a
commit 53425d8fe2
2 changed files with 67 additions and 18 deletions

View file

@ -17,7 +17,7 @@ import startSandbox from './startSandbox';
import installDevtools from './installDevtools'; import installDevtools from './installDevtools';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { diskSpaceLinux } from '../ui/util/diskspace'; import { diskSpaceLinux, diskSpaceWindows, diskSpaceMac } from '../ui/util/diskspace';
const { download } = require('electron-dl'); const { download } = require('electron-dl');
const remote = require('@electron/remote/main'); const remote = require('@electron/remote/main');
@ -299,14 +299,21 @@ app.on('before-quit', () => {
ipcMain.on('get-disk-space', async (event) => { ipcMain.on('get-disk-space', async (event) => {
try { try {
const { data_dir } = await Lbry.settings_get(); const { data_dir } = await Lbry.settings_get();
// mac error for this is 'df /Users/username/Library/Application no such..' let diskSpace = undefined;
// due to not escaping the ' ' in /`Application Support` switch (os.platform()) {
if (os.platform() === 'linux') { case 'linux':
const stdout = await diskSpaceLinux(data_dir); diskSpace = await diskSpaceLinux(data_dir);
const dfResult = stdout.split('\n')[1].split(/\s+/); break;
const total_and_available = { total: dfResult[1], free: dfResult[3]}; case 'darwin':
rendererWindow.webContents.send('send-disk-space', { diskSpace: total_and_available }); diskSpace = await diskSpaceMac(data_dir);
break;
case 'win32':
diskSpace = await diskSpaceWindows(data_dir);
break;
default:
throw new Error('unknown platform');
} }
rendererWindow.webContents.send('send-disk-space', { diskSpace });
} catch (e) { } catch (e) {
rendererWindow.webContents.send('send-disk-space', { error: e.message || e }); rendererWindow.webContents.send('send-disk-space', { error: e.message || e });
console.log('Failed to start LbryFirst', e); console.log('Failed to start LbryFirst', e);

View file

@ -4,20 +4,62 @@ export const diskSpaceLinux = (path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`df ${path}`, (error, stdout, stderr) => { exec(`df ${path}`, (error, stdout, stderr) => {
if (error) { if (error) {
console.log(`error: ${error.message}`); return reject(error);
return reject();
} }
if (stderr) { if (stderr) {
return reject(); return reject(new Error(stderr));
} }
resolve(stdout); // 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({
total: dfResult[1],
free: dfResult[3],
});
}); });
}); });
}; };
// to implement export const diskSpaceMac = (path) => {
// export diskSpaceWindows = (path) => { // Escape spaces in path to prevent errors.
// new Promise((resolve, reject) => { // 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({
total: totalSize,
free: freeSpace,
});
});
});
};