Add disk space functions for windows and mac.
This commit is contained in:
parent
ca748fd16a
commit
53425d8fe2
2 changed files with 67 additions and 18 deletions
|
@ -17,7 +17,7 @@ import startSandbox from './startSandbox';
|
|||
import installDevtools from './installDevtools';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { diskSpaceLinux } from '../ui/util/diskspace';
|
||||
import { diskSpaceLinux, diskSpaceWindows, diskSpaceMac } from '../ui/util/diskspace';
|
||||
|
||||
const { download } = require('electron-dl');
|
||||
const remote = require('@electron/remote/main');
|
||||
|
@ -299,14 +299,21 @@ app.on('before-quit', () => {
|
|||
ipcMain.on('get-disk-space', async (event) => {
|
||||
try {
|
||||
const { data_dir } = await Lbry.settings_get();
|
||||
// mac error for this is 'df /Users/username/Library/Application no such..'
|
||||
// due to not escaping the ' ' in /`Application Support`
|
||||
if (os.platform() === 'linux') {
|
||||
const stdout = await diskSpaceLinux(data_dir);
|
||||
const dfResult = stdout.split('\n')[1].split(/\s+/);
|
||||
const total_and_available = { total: dfResult[1], free: dfResult[3]};
|
||||
rendererWindow.webContents.send('send-disk-space', { diskSpace: total_and_available });
|
||||
let diskSpace = undefined;
|
||||
switch (os.platform()) {
|
||||
case 'linux':
|
||||
diskSpace = await diskSpaceLinux(data_dir);
|
||||
break;
|
||||
case 'darwin':
|
||||
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) {
|
||||
rendererWindow.webContents.send('send-disk-space', { error: e.message || e });
|
||||
console.log('Failed to start LbryFirst', e);
|
||||
|
|
|
@ -4,20 +4,62 @@ export const diskSpaceLinux = (path) => {
|
|||
return new Promise((resolve, reject) => {
|
||||
exec(`df ${path}`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.log(`error: ${error.message}`);
|
||||
return reject();
|
||||
return reject(error);
|
||||
}
|
||||
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 diskSpaceWindows = (path) => {
|
||||
// new Promise((resolve, reject) => {
|
||||
//
|
||||
// });
|
||||
// }
|
||||
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({
|
||||
total: totalSize,
|
||||
free: freeSpace,
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue