Merge pull request #12 from tgpethan/total-disk-space

Add total disk space to space api
This commit is contained in:
the-basic-geek 2020-07-09 10:54:09 +01:00 committed by GitHub
commit 5738f48cf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 7 deletions

43
EUS.js
View File

@ -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,17 +269,29 @@ 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;
sizeOfFolder = (size / 1024 / 1024 / 1024).toFixed(2);
jsonaa["space"]["string"] = `${sizeOfFolder} GB`;
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"] = {
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)
};
});
// 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`);
@ -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;

View File

@ -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!)**

View File

@ -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