From 37ff8c5d6e61f62e72082ee5d3a8ea3b3ef1e6a6 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 9 Jul 2020 00:13:28 +0100 Subject: [PATCH 1/6] Add total disk space to space api --- EUS.js | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/EUS.js b/EUS.js index 462105c..402fa60 100644 --- a/EUS.js +++ b/EUS.js @@ -3,7 +3,8 @@ const fs = require("fs"), chalk = require("chalk"), busboy = require("connect-busboy"), randomstring = require("randomstring"), - getSize = require('get-folder-size'), + getSize = require("get-folder-size"), + diskUsage = require("diskusage") emoji = require("../misc/emoji_list.json"); // Defines the function of this module @@ -268,19 +269,31 @@ function handleAPI(req, res) { } // Getting space is required if (spaceaa == 1) { - jsonaa["space"] = {}; + jsonaa["space"] = { + usage: {} + }; // Get the space used on the disk getSize(__dirname + BASE_PATH + "/i", (err, size) => { if (err) throw err; // Calculate in different units the space taken up on disk let sizeOfFolder = (size / 1024 / 1024); - jsonaa["space"]["mb"] = sizeOfFolder; + jsonaa["space"]["usage"]["mb"] = sizeOfFolder; sizeOfFolder = (size / 1024 / 1024 / 1024); - jsonaa["space"]["gb"] = sizeOfFolder; + jsonaa["space"]["usage"]["gb"] = sizeOfFolder; sizeOfFolder = (size / 1024 / 1024 / 1024).toFixed(2); - jsonaa["space"]["string"] = `${sizeOfFolder} GB`; + jsonaa["space"]["usage"]["string"] = spaceToLowest(size, true); // Send the json to the requesting client d = new Date(); endTime = d.getTime(); + diskUsage.check(__dirname, (err, data) => { + if (err) throw err; + jsonaa["space"]["total"] = { + value: spaceToLowest(data["total"], false), + mbvalue: (data["total"] / 1024 / 1024), + gbvalue: (data["total"] / 1024 / 1024 / 1024), + stringValue: spaceToLowest(data["total"], true).split(" ")[1].toLowerCase(), + string: spaceToLowest(data["total"], true) + }; + }); global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); return res.end(JSON.stringify(jsonaa)); }); @@ -314,4 +327,22 @@ function handleAPI(req, res) { } } +const spaceValues = ["B", "KB", "MB", "GB", "TB", "PB", "EB"]; // Futureproofing:tm: + +function spaceToLowest(spaceValue, includeStringValue) { + // Converts space values to lower values e.g MB, GB, TB etc depending on the size of the number + let i1 = 1; + // Loop through until value is at it's lowest + for (let i = 0; i < i1; i++) { + if (spaceValue >= 1024) { + spaceValue = spaceValue / 1024; + } + + if (spaceValue >= 1024) i1++; + } + + if (includeStringValue) return `${spaceValue.toFixed(2)} ${spaceValues[i1]}`; + else return spaceValue; +} + module.exports.MOD_FUNC = MODULE_FUNCTION; \ No newline at end of file From fcf31e8f03d647971868903320587b2d14dd739b Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 9 Jul 2020 00:22:22 +0100 Subject: [PATCH 2/6] Update README.md to reflect new dependencies --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b712a90..2e084bc 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ EUS has extra dependencies other than those of [Revolution](https://github.com/t - [connect-busboy](https://www.npmjs.com/package/connect-busboy) - [randomstring](https://www.npmjs.com/package/randomstring) - [get-folder-size](https://www.npmjs.com/package/get-folder-size) + - [diskusage](https://www.npmjs.com/package/diskusage) Install the dependencies and then simply drop the EUS.js into a Revolution instance's modules folder **(If you still have [example_request_handler.js](https://github.com/tgpethan/Revolution/blob/master/modules/example_request_handler.js) be sure to delete it!)** From ee896da2aaa39d6ba3a10fc8e56c78d474feabb3 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 9 Jul 2020 00:36:13 +0100 Subject: [PATCH 3/6] Remove a now unneeded calculation --- EUS.js | 1 - 1 file changed, 1 deletion(-) diff --git a/EUS.js b/EUS.js index 402fa60..67c5788 100644 --- a/EUS.js +++ b/EUS.js @@ -280,7 +280,6 @@ function handleAPI(req, res) { jsonaa["space"]["usage"]["mb"] = sizeOfFolder; sizeOfFolder = (size / 1024 / 1024 / 1024); jsonaa["space"]["usage"]["gb"] = sizeOfFolder; - sizeOfFolder = (size / 1024 / 1024 / 1024).toFixed(2); jsonaa["space"]["usage"]["string"] = spaceToLowest(size, true); // Send the json to the requesting client d = new Date(); endTime = d.getTime(); From 378438e4aa368c59eecd5083da938c8d9e85a48e Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 9 Jul 2020 00:37:28 +0100 Subject: [PATCH 4/6] Move time getting to correct place --- EUS.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EUS.js b/EUS.js index 67c5788..42b0e0d 100644 --- a/EUS.js +++ b/EUS.js @@ -281,8 +281,6 @@ function handleAPI(req, res) { sizeOfFolder = (size / 1024 / 1024 / 1024); jsonaa["space"]["usage"]["gb"] = sizeOfFolder; jsonaa["space"]["usage"]["string"] = spaceToLowest(size, true); - // Send the json to the requesting client - d = new Date(); endTime = d.getTime(); diskUsage.check(__dirname, (err, data) => { if (err) throw err; jsonaa["space"]["total"] = { @@ -293,6 +291,8 @@ function handleAPI(req, res) { string: spaceToLowest(data["total"], true) }; }); + // Send the json to the requesting client + d = new Date(); endTime = d.getTime(); global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); return res.end(JSON.stringify(jsonaa)); }); From 2ffaa4aacc060baf9950da712a961f4e05c4246e Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 9 Jul 2020 00:38:33 +0100 Subject: [PATCH 5/6] Literally add a single comment --- EUS.js | 1 + 1 file changed, 1 insertion(+) diff --git a/EUS.js b/EUS.js index 42b0e0d..0c9028c 100644 --- a/EUS.js +++ b/EUS.js @@ -281,6 +281,7 @@ function handleAPI(req, res) { sizeOfFolder = (size / 1024 / 1024 / 1024); jsonaa["space"]["usage"]["gb"] = sizeOfFolder; jsonaa["space"]["usage"]["string"] = spaceToLowest(size, true); + // Get total disk space diskUsage.check(__dirname, (err, data) => { if (err) throw err; jsonaa["space"]["total"] = { From da098138c34216366ead089ea97c33f8b3aea53e Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 9 Jul 2020 00:41:54 +0100 Subject: [PATCH 6/6] Update quick-setup.sh so that it will now work with these changes --- quick-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quick-setup.sh b/quick-setup.sh index 6ce78a8..9c4c87a 100755 --- a/quick-setup.sh +++ b/quick-setup.sh @@ -5,4 +5,4 @@ npm i wget https://raw.githubusercontent.com/tgpethan/EUS/master/EUS.js -P modules/ rm modules/example_request_handler.js cp config/config.example.json config/config.json -npm i connect-busboy randomstring +npm i connect-busboy randomstring diskusage get-folder-size