From 9e23c68204b9cc972fb4bb6c0b137ddd7d87ab32 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 20 Feb 2020 07:41:04 +0000 Subject: [PATCH 1/3] Add api endpoint for status --- EUS.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/EUS.js b/EUS.js index 8eb8668..0502843 100644 --- a/EUS.js +++ b/EUS.js @@ -60,13 +60,10 @@ fs.access(`${__dirname}${BASE_PATH}/config.json`, error => { process.exit(0); }); } else { - eusConfig = require(`${__dirname}${BASE_PATH}/image-type.json`); + eusConfig = require(`${__dirname}${BASE_PATH}/config.json`); } }); -// Construct the full exported url from the config string and port using javascript magic -const exportURL = `${eusConfig.baseURL}:${config.server.port}/`; - module.exports = { extras:function() { // Setup express to use busboy @@ -158,7 +155,7 @@ module.exports = { if (err) throw err; 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 - res.end(exportURL+""+fileOutName); + res.end(eusConfig.baseURL+""+fileOutName); }); }); }); From 2fe9efd36a65d8eb097012204348b8fe07208413 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 20 Feb 2020 07:42:46 +0000 Subject: [PATCH 2/3] oops --- EUS.js | 99 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 30 deletions(-) diff --git a/EUS.js b/EUS.js index 0502843..b319e68 100644 --- a/EUS.js +++ b/EUS.js @@ -3,6 +3,7 @@ config = require("../config/config.json"), chalk = require("chalk"), busboy = require("connect-busboy"), randomstring = require("randomstring"), +getSize = require('get-folder-size'), emoji = require("../misc/emoji_list.json"); // Defines the function of this module @@ -75,41 +76,79 @@ module.exports = { res - Response from server */ + let isAPI = false; + if (req.query["stat"] == "get") return res.end('{ "status":1, "version":"'+global.internals.version+'" }'); - // Register the time at the start of the request - d = new Date(); - startTime = d.getTime(); - // Get the requested image - let urs = ""+req.url; urs = urs.split("/")[1]; - // 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 => { - if (error) { - // Doesn't exist, handle request normaly - if (req.url === "/") { urs = "/index.html" } else { urs = req.url }; - fs.access(__dirname + BASE_PATH + "/files"+urs, error => { - if (error) { - // Doesn't exist, send a 404 to the client. - res.status(404).end("404!"); - d = new Date(); - endTime = d.getTime(); - global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`); - } else { - // File does exist, send it back to the client. - res.sendFile(__dirname + BASE_PATH + "/files"+req.url); - d = new Date(); - endTime = d.getTime(); - global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); + if (req.url.split("?")[0] == "/api/get-stats") { + isAPI = true; + const filesaa = req.query["f"], + spaceaa = req.query["s"]; + let jsonaa = {}; + if (filesaa == 1) { + jsonaa["files"] = {}; + for (var i2 = 0; i2 < eusConfig.acceptedTypes.length; i2++) { + jsonaa["files"][`${eusConfig.acceptedTypes[i2]}`.replace(".", "")] = 0; + } + fs.readdir(__dirname + BASE_PATH + "/i", (err, files) => { + if (err) throw err; + for (var i = 0; i < files.length; i++) { + for (var i1 = 0; i1 < eusConfig.acceptedTypes.length; i1++) { + const jsudfg = files[i].split("."); + if (`.${jsudfg[jsudfg.length-1]}` == eusConfig.acceptedTypes[i1]) { + jsonaa["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++; + } + } } + if (spaceaa != 1) return res.end(JSON.stringify(jsonaa)); }); - } else { - // Image does exist, send it back. - res.sendFile(__dirname + BASE_PATH + "/i/"+urs+image_json[urs]); - d = new Date(); - endTime = d.getTime(); - global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); } - }); + if (spaceaa == 1) { + jsonaa["used"] = ""; + getSize(__dirname + BASE_PATH + "/i", (err, size) => { + if (err) throw err; + const sizeOfFolder = (size / 1024 / 1024 / 1024).toFixed(2); + jsonaa["used"] = `${sizeOfFolder} GB`; + return res.end(JSON.stringify(jsonaa)); + }); + } + } + + if (!isAPI) { + // Register the time at the start of the request + d = new Date(); + startTime = d.getTime(); + // Get the requested image + let urs = ""+req.url; urs = urs.split("/")[1]; + // 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 => { + if (error) { + // Doesn't exist, handle request normaly + if (req.url === "/") { urs = "/index.html" } else { urs = req.url }; + fs.access(__dirname + BASE_PATH + "/files"+urs, error => { + if (error) { + // Doesn't exist, send a 404 to the client. + res.status(404).end("404!"); + d = new Date(); + endTime = d.getTime(); + global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`); + } else { + // File does exist, send it back to the client. + res.sendFile(__dirname + BASE_PATH + "/files"+req.url); + d = new Date(); + endTime = d.getTime(); + global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); + } + }); + } else { + // Image does exist, send it back. + res.sendFile(__dirname + BASE_PATH + "/i/"+urs+image_json[urs]); + d = new Date(); + endTime = d.getTime(); + global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); + } + }); + } }, post:function(req, res) { /* From 38d532d58c4139c822738c8609ebeca266b9effc Mon Sep 17 00:00:00 2001 From: tgpethan Date: Thu, 20 Feb 2020 07:45:42 +0000 Subject: [PATCH 3/3] Add a total files counter --- EUS.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/EUS.js b/EUS.js index b319e68..d58c451 100644 --- a/EUS.js +++ b/EUS.js @@ -86,6 +86,7 @@ module.exports = { spaceaa = req.query["s"]; let jsonaa = {}; if (filesaa == 1) { + let total = 0; jsonaa["files"] = {}; for (var i2 = 0; i2 < eusConfig.acceptedTypes.length; i2++) { jsonaa["files"][`${eusConfig.acceptedTypes[i2]}`.replace(".", "")] = 0; @@ -97,9 +98,11 @@ module.exports = { const jsudfg = files[i].split("."); if (`.${jsudfg[jsudfg.length-1]}` == eusConfig.acceptedTypes[i1]) { jsonaa["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++; + total++; } } } + jsonaa["files"]["total"] = total; if (spaceaa != 1) return res.end(JSON.stringify(jsonaa)); }); }