Asyncify EUS #25

Merged
tgpholly merged 1 commit from asyncify into master 2021-03-16 18:53:07 +00:00

97
EUS.js
View file

@ -4,7 +4,7 @@ const fs = require("fs"),
busboy = require("connect-busboy"), busboy = require("connect-busboy"),
randomstring = require("randomstring"), randomstring = require("randomstring"),
getSize = require("get-folder-size"), getSize = require("get-folder-size"),
diskUsage = require("diskusage"), diskUsage = require("diskusage-ng"),
emoji = require("../misc/emoji_list.json"); emoji = require("../misc/emoji_list.json");
// Defines the function of this module // Defines the function of this module
@ -64,18 +64,10 @@ if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) {
if (validateConfig(eusConfig)) console.log("[EUS] EUS config passed all checks"); if (validateConfig(eusConfig)) console.log("[EUS] EUS config passed all checks");
} }
function cleanURL(url = "") {
return url.split("%20").join(" ").split("%22").join("\"").split("%23").join("#").split("%24").join("$").split("%25").join("%").split("%26").join("&").split("%27").join("'").split("%2B").join("+").split("%2C").join(",").split("%2F").join("/")
.split("%3A").join(":").split("%3B").join(";").split("%3C").join("<").split("%3D").join("=").split("%3E").join(">").split("%3F").join("?")
.split("%40").join("@")
.split("%5B").join("[").split("%5C").join("\\").split("%5D").join("]").split("%5E").join("^")
.split("%60").join("`")
.split("%7B").join("{").split("%7C").join("|").split("%7D").join("}").split("%7E").join("~");
}
// Cache for the file count and space usage, this takes a while to do so it's best to cache the result // Cache for the file count and space usage, this takes a while to do so it's best to cache the result
let cacheIsReady = false; let cacheIsReady = false;
async function cacheFilesAndSpace() { async function cacheFilesAndSpace() {
return new Promise((resolve, reject) => {
const startCacheTime = new Date().getTime(); const startCacheTime = new Date().getTime();
let cachedFilesAndSpace = { let cachedFilesAndSpace = {
files: {} files: {}
@ -89,13 +81,14 @@ async function cacheFilesAndSpace() {
} }
// Read all files from the images directory // Read all files from the images directory
fs.readdir(__dirname + BASE_PATH + "/i", async (err, files) => { fs.readdir(__dirname + BASE_PATH + "/i", async (err, files) => {
if (err) throw err; if (err) reject(err);
else {
// Loop through all files // Loop through all files
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
// Loop through all accepted file types to check for a match // Loop through all accepted file types to check for a match
for (var i1 = 0; i1 < eusConfig.acceptedTypes.length; i1++) { for (var i1 = 0; i1 < eusConfig.acceptedTypes.length; i1++) {
const jsudfg = files[i].split("."); const readFileName = files[i].split(".");
if (`.${jsudfg[jsudfg.length-1]}` == eusConfig.acceptedTypes[i1]) { if (`.${readFileName[readFileName.length-1]}` == eusConfig.acceptedTypes[i1]) {
// There is a match! Add it to the json // There is a match! Add it to the json
cachedFilesAndSpace["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++; cachedFilesAndSpace["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++;
// Also increase the total // Also increase the total
@ -112,28 +105,33 @@ async function cacheFilesAndSpace() {
}; };
// Get the space used on the disk // Get the space used on the disk
getSize(__dirname + BASE_PATH + "/i", async (err, size) => { getSize(__dirname + BASE_PATH + "/i", async (err, size) => {
if (err) throw err; if (err) reject(err);
else {
// Calculate in different units the space taken up on disk // Calculate in different units the space taken up on disk
let sizeOfFolder = (size / 1024 / 1024); let sizeOfFolder = (size / 2048);
cachedFilesAndSpace["space"]["usage"]["mb"] = sizeOfFolder; cachedFilesAndSpace["space"]["usage"]["mb"] = sizeOfFolder;
sizeOfFolder = (size / 1024 / 1024 / 1024); sizeOfFolder = (size / 3072);
cachedFilesAndSpace["space"]["usage"]["gb"] = sizeOfFolder; cachedFilesAndSpace["space"]["usage"]["gb"] = sizeOfFolder;
cachedFilesAndSpace["space"]["usage"]["string"] = spaceToLowest(size, true); cachedFilesAndSpace["space"]["usage"]["string"] = await spaceToLowest(size, true);
// Get total disk space // Get total disk space
diskUsage.check(__dirname, async (err, data) => { diskUsage(__dirname, async (err, data) => {
if (err) throw err; if (err) reject(err);
else {
cachedFilesAndSpace["space"]["total"] = { cachedFilesAndSpace["space"]["total"] = {
value: spaceToLowest(data["total"], false), value: await spaceToLowest(data["total"], false),
mbvalue: (data["total"] / 1024 / 1024), mbvalue: (data["total"] / 2048),
gbvalue: (data["total"] / 1024 / 1024 / 1024), gbvalue: (data["total"] / 3072),
stringValue: spaceToLowest(data["total"], true).split(" ")[1].toLowerCase(), stringValue: (await spaceToLowest(data["total"], true)).split(" ")[1].toLowerCase(),
string: spaceToLowest(data["total"], true) string: await spaceToLowest(data["total"], true)
}; };
cacheIsReady = true; resolve(cachedFilesAndSpace);
cacheJSON = JSON.stringify(cachedFilesAndSpace);
global.modules.consoleHelper.printInfo(emoji.folder, `Stats api cache took ${new Date().getTime() - startCacheTime}ms`); global.modules.consoleHelper.printInfo(emoji.folder, `Stats api cache took ${new Date().getTime() - startCacheTime}ms`);
}
}); });
}
});
}
}); });
}); });
} }
@ -177,13 +175,23 @@ function validateConfig(json) {
else return true; else return true;
} }
function cleanURL(url = "") {
return url.split("%20").join(" ").split("%22").join("\"").split("%23").join("#").split("%24").join("$").split("%25").join("%").split("%26").join("&").split("%27").join("'").split("%2B").join("+").split("%2C").join(",").split("%2F").join("/")
.split("%3A").join(":").split("%3B").join(";").split("%3C").join("<").split("%3D").join("=").split("%3E").join(">").split("%3F").join("?")
.split("%40").join("@")
.split("%5B").join("[").split("%5C").join("\\").split("%5D").join("]").split("%5E").join("^")
.split("%60").join("`")
.split("%7B").join("{").split("%7C").join("|").split("%7D").join("}").split("%7E").join("~");
}
module.exports = { module.exports = {
extras:function() { extras:async function() {
// Setup express to use busboy // Setup express to use busboy
global.app.use(busboy()); global.app.use(busboy());
cacheFilesAndSpace(); cacheJSON = JSON.stringify(await cacheFilesAndSpace());
cacheIsReady = true;
}, },
get:function(req, res) { get:async function(req, res) {
/* /*
req - Request from client req - Request from client
res - Response from server res - Response from server
@ -207,17 +215,18 @@ module.exports = {
// Register the time at the start of the request // Register the time at the start of the request
d = new Date(); d = new Date();
startTime = d.getTime(); startTime = d.getTime();
// Get the requested image // Get the requested image
let urs = ""+req.url; urs = urs.split("/")[1]; let urs = `${req.url}`; urs = urs.split("/")[1];
// Get the file type of the image from image_json and make sure it exists // Get the file type of the image from image_json and make sure it exists
fs.access(__dirname + BASE_PATH + "/i/"+urs+image_json[urs], error => { fs.access(`${__dirname}${BASE_PATH}/i/${urs}${image_json[urs]}`, (error) => {
if (error) { if (error) {
// Doesn't exist, handle request normaly // Doesn't exist, handle request normaly
if (req.url === "/") { urs = "/index.html" } else { urs = req.url } if (req.url === "/") { urs = "/index.html" } else { urs = req.url }
fs.access(__dirname + BASE_PATH + "/files"+urs, error => { fs.access(`${__dirname}${BASE_PATH}/files${urs}`, (error) => {
if (error) { if (error) {
// Doesn't exist, send a 404 to the client. // Doesn't exist, send a 404 to the client.
res.status(404).end("404!"); res.status(404).send("404!<hr>EUS");
d = new Date(); d = new Date();
endTime = d.getTime(); endTime = d.getTime();
global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`); global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`);
@ -231,14 +240,14 @@ module.exports = {
}); });
} else { } else {
// Image does exist, send it back. // Image does exist, send it back.
res.sendFile(__dirname + BASE_PATH + "/i/"+urs+image_json[urs]); res.sendFile(`${__dirname}${BASE_PATH}/i/${urs}${image_json[urs]}`);
d = new Date(); d = new Date();
endTime = d.getTime(); endTime = d.getTime();
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`);
} }
}); });
}, },
post:function(req, res) { post:async function(req, res) {
/* /*
req - Request from client req - Request from client
res - Response from server res - Response from server
@ -281,21 +290,23 @@ module.exports = {
// Add image file type to the image_json array // Add image file type to the image_json array
image_json[fileOutName] = `.${filename.split(".")[filename.split(".").length-1]}`; image_json[fileOutName] = `.${filename.split(".")[filename.split(".").length-1]}`;
// Save image_json array to the file // Save image_json array to the file
fs.writeFile(`${__dirname}${BASE_PATH}/image-type.json`, JSON.stringify(image_json), function(err) { fs.writeFile(`${__dirname}${BASE_PATH}/image-type.json`, JSON.stringify(image_json), async (err) => {
if (err) throw err; if (err) reject(err);
else {
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: Upload of ${fileOutName} finished. Took ${endTime - startTime}ms`); global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: Upload of ${fileOutName} finished. Took ${endTime - startTime}ms`);
// Send URL of the uploaded image to the client // Send URL of the uploaded image to the client
res.end(eusConfig.baseURL+""+fileOutName); res.end(eusConfig.baseURL+""+fileOutName);
// Update cached files & space // Update cached files & space
cacheFilesAndSpace(); cacheJSON = JSON.stringify(await cacheFilesAndSpace());
}
}); });
}); });
}); });
} }
} }
function handleAPI(req, res) { async function handleAPI(req, res) {
d = new Date(); startTime = d.getTime(); d = new Date(); startTime = d.getTime();
let jsonaa = {}, filesaa = 0, spaceaa = 0; let jsonaa = {}, filesaa = 0, spaceaa = 0;
switch (req.url.split("?")[0]) { switch (req.url.split("?")[0]) {
@ -364,7 +375,8 @@ function handleAPI(req, res) {
const spaceValues = ["B", "KB", "MB", "GB", "TB", "PB", "EB"]; // Futureproofing:tm: const spaceValues = ["B", "KB", "MB", "GB", "TB", "PB", "EB"]; // Futureproofing:tm:
function spaceToLowest(spaceValue, includeStringValue) { async function spaceToLowest(spaceValue, includeStringValue) {
return new Promise((resolve, reject) => {
// Converts space values to lower values e.g MB, GB, TB etc depending on the size of the number // Converts space values to lower values e.g MB, GB, TB etc depending on the size of the number
let i1 = 1; let i1 = 1;
// Loop through until value is at it's lowest // Loop through until value is at it's lowest
@ -376,8 +388,9 @@ function spaceToLowest(spaceValue, includeStringValue) {
if (spaceValue >= 1024) i1++; if (spaceValue >= 1024) i1++;
} }
if (includeStringValue) return `${spaceValue.toFixed(2)} ${spaceValues[i1]}`; if (includeStringValue) resolve(`${spaceValue.toFixed(2)} ${spaceValues[i1]}`);
else return spaceValue; else resolve(spaceValue);
});
} }
module.exports.MOD_FUNC = MODULE_FUNCTION; module.exports.MOD_FUNC = MODULE_FUNCTION;