From 4127e0a7072d510c3cfb7d106d3b9401d9dba617 Mon Sep 17 00:00:00 2001 From: Karol Putra Date: Wed, 2 Sep 2020 21:45:08 +0200 Subject: [PATCH 1/2] Added availableSpaceWatchDog Every minute it check free disk space. It uses new module - diskspace - works on Windows Linux and Mac. If the partition where Rambox is installed has < 1 GB available space: - a warning dialog appears with info about: -- remaining space -- rambox/electron installation path -- OK button which quits Rambox No matter how the dialog is dismissed, it will quit Rambox. --- electron/main.js | 40 +++++++++++++++++++++++++++++++++++++++- package.json | 1 + 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/electron/main.js b/electron/main.js index 74ad0c5d..43183f95 100644 --- a/electron/main.js +++ b/electron/main.js @@ -15,6 +15,10 @@ const updater = require('./updater'); var fs = require("fs"); const path = require('path'); +// Disk usage: +const disk = require('diskusage'); +const appPath = app.getAppPath(); + if ( isDev ) app.getVersion = function() { return require('../package.json').version; }; // FOR DEV ONLY, BECAUSE IN DEV RETURNS ELECTRON'S VERSION // Initial Config @@ -250,6 +254,40 @@ function updateBadge(title) { if ( messageCount > 0 && !mainWindow.isFocused() && !config.get('dont_disturb') && config.get('flash_frame') ) mainWindow.flashFrame(true); } +function formatBytes(bytes, decimals = 2) { + if (bytes === 0) return '0 Bytes'; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; +} + +async function availableSpaceWatchDog() { + // optionally render this information also in rambox window + try { + const { available } = await disk.check(appPath); + if (available < 1073741824) { // 1 GB + const options = { + type: 'warning', + buttons: ['OK, quit'], + defaultId: 0, + title: `Running out of disk space! - Rambox shutting down`, + detail: `You've got just ${formatBytes(available)} space left.\n\nRambox has been frozen to prevent settings corruption.\n\nOnce you quit this dialog, Rambox will shutdown.\n\n1 GB of avalable disk space is required.\nFree up space on partition where Rambox is installed then start the app again.\n\nRambox path: \n${appPath}`, + message: `Running out of disk space! - Rambox shutting down`, + }; + + dialog.showMessageBoxSync(null, options); + app.quit(); + } + } catch (err) { + console.error(err) + } +} + ipcMain.on('setBadge', function(event, messageCount, value) { mainWindow.setOverlayIcon(nativeImage.createFromDataURL(value), messageCount.toString()); }); @@ -474,8 +512,8 @@ if ( config.get('disable_gpu') ) app.disableHardwareAcceleration(); // initialization and is ready to create browser windows. app.on('ready', function() { config.get('master_password') ? createMasterPasswordWindow() : createWindow(); + setInterval(availableSpaceWatchDog, 1000 * 60); }); - // Quit when all windows are closed. app.on('window-all-closed', function () { // On OS X it is common for applications and their menu bar diff --git a/package.json b/package.json index 36d955e7..3eeda1db 100644 --- a/package.json +++ b/package.json @@ -209,6 +209,7 @@ "auth0-js": "9.13.2", "auto-launch-patched": "5.0.2", "crypto": "1.0.1", + "diskusage": "1.1.3", "electron-contextmenu-wrapper": "2.0.0", "electron-is-dev": "0.3.0", "electron-log": "2.2.17", From 072997e59c1319038de5035202e9cf66906a003c Mon Sep 17 00:00:00 2001 From: Karol Putra Date: Wed, 2 Sep 2020 21:45:08 +0200 Subject: [PATCH 2/2] Added availableSpaceWatchDog. Every minute it checks if available disk space < 1 GB. It requires new module - diskspace - works on Windows Linux and Mac. Tested only on Linux. If the partition where Rambox is running from has < 1 GB available space: - a warning dialog appears and Rambox freezes (synchronous dialog). Dialog has info about: -- remaining space -- rambox/electron installation path -- instruction to free up more space and that the app will exit upon dialog dismissal -- OK button which quits Rambox No matter how the dialog is dismissed, Rambox quits. --- electron/main.js | 40 +++++++++++++++++++++++++++++++++++++++- package.json | 1 + 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/electron/main.js b/electron/main.js index 74ad0c5d..43183f95 100644 --- a/electron/main.js +++ b/electron/main.js @@ -15,6 +15,10 @@ const updater = require('./updater'); var fs = require("fs"); const path = require('path'); +// Disk usage: +const disk = require('diskusage'); +const appPath = app.getAppPath(); + if ( isDev ) app.getVersion = function() { return require('../package.json').version; }; // FOR DEV ONLY, BECAUSE IN DEV RETURNS ELECTRON'S VERSION // Initial Config @@ -250,6 +254,40 @@ function updateBadge(title) { if ( messageCount > 0 && !mainWindow.isFocused() && !config.get('dont_disturb') && config.get('flash_frame') ) mainWindow.flashFrame(true); } +function formatBytes(bytes, decimals = 2) { + if (bytes === 0) return '0 Bytes'; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; +} + +async function availableSpaceWatchDog() { + // optionally render this information also in rambox window + try { + const { available } = await disk.check(appPath); + if (available < 1073741824) { // 1 GB + const options = { + type: 'warning', + buttons: ['OK, quit'], + defaultId: 0, + title: `Running out of disk space! - Rambox shutting down`, + detail: `You've got just ${formatBytes(available)} space left.\n\nRambox has been frozen to prevent settings corruption.\n\nOnce you quit this dialog, Rambox will shutdown.\n\n1 GB of avalable disk space is required.\nFree up space on partition where Rambox is installed then start the app again.\n\nRambox path: \n${appPath}`, + message: `Running out of disk space! - Rambox shutting down`, + }; + + dialog.showMessageBoxSync(null, options); + app.quit(); + } + } catch (err) { + console.error(err) + } +} + ipcMain.on('setBadge', function(event, messageCount, value) { mainWindow.setOverlayIcon(nativeImage.createFromDataURL(value), messageCount.toString()); }); @@ -474,8 +512,8 @@ if ( config.get('disable_gpu') ) app.disableHardwareAcceleration(); // initialization and is ready to create browser windows. app.on('ready', function() { config.get('master_password') ? createMasterPasswordWindow() : createWindow(); + setInterval(availableSpaceWatchDog, 1000 * 60); }); - // Quit when all windows are closed. app.on('window-all-closed', function () { // On OS X it is common for applications and their menu bar diff --git a/package.json b/package.json index 36d955e7..3eeda1db 100644 --- a/package.json +++ b/package.json @@ -209,6 +209,7 @@ "auth0-js": "9.13.2", "auto-launch-patched": "5.0.2", "crypto": "1.0.1", + "diskusage": "1.1.3", "electron-contextmenu-wrapper": "2.0.0", "electron-is-dev": "0.3.0", "electron-log": "2.2.17",