General code sanity

This commit is contained in:
tgpethan 2020-06-15 12:04:17 +01:00
parent 919bfd9993
commit 4db94ce6c3
1 changed files with 79 additions and 74 deletions

151
EUS.js
View File

@ -36,10 +36,10 @@ if (!fs.existsSync(__dirname + BASE_PATH + "/i")) {
console.log(`[EUS] Made EUS images folder`); console.log(`[EUS] Made EUS images folder`);
} }
// Makes the image-type file // Makes the image-type file
if (fs.existsSync(__dirname + BASE_PATH + "image-type.json")) { if (!fs.existsSync(__dirname + BASE_PATH + "/image-type.json")) {
// Doesn't exist, create it. // Doesn't exist, create it.
fs.writeFileSync(`${__dirname}${BASE_PATH}/image-type.json`, '{}'); fs.writeFileSync(`${__dirname}${BASE_PATH}/image-type.json`, '{}');
console.log("[EUS] Created image-type File!"); console.log("[EUS] Made EUS image-type File!");
// File has been created, load it. // File has been created, load it.
image_json = require(`${__dirname}${BASE_PATH}/image-type.json`); image_json = require(`${__dirname}${BASE_PATH}/image-type.json`);
} else { } else {
@ -48,10 +48,10 @@ if (fs.existsSync(__dirname + BASE_PATH + "image-type.json")) {
} }
// Makes the config file // Makes the config file
if (fs.existsSync(__dirname + BASE_PATH + "config.json")) { if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) {
// Config doesn't exist, make it. // Config doesn't exist, make it.
fs.writeFileSync(`${__dirname}${BASE_PATH}/config.json`, '{\n\t"baseURL":"http://example.com/",\n\t"acceptedTypes": [\n\t\t".png",\n\t\t".jpg",\n\t\t".jpeg",\n\t\t".gif"\n\t],\n\t"uploadKey": ""\n}'); fs.writeFileSync(`${__dirname}${BASE_PATH}/config.json`, '{\n\t"baseURL":"http://example.com/",\n\t"acceptedTypes": [\n\t\t".png",\n\t\t".jpg",\n\t\t".jpeg",\n\t\t".gif"\n\t],\n\t"uploadKey": ""\n}');
console.log("[EUS] Created config File!"); console.log("[EUS] Made EUS config File!");
console.log("[EUS] Please edit the EUS Config file before restarting."); console.log("[EUS] Please edit the EUS Config file before restarting.");
// Config has been made, close framework. // Config has been made, close framework.
process.exit(0); process.exit(0);
@ -93,7 +93,10 @@ function validateConfig(json) {
} }
// Check if server needs to be shutdown // Check if server needs to be shutdown
if (performShutdownAfterValidation) throw "EUS config properties are missing, refer to docs for more details (https://docs.ethanus.ml)"; if (performShutdownAfterValidation) {
console.error("EUS config properties are missing, refer to docs for more details (https://docs.ethanus.ml)");
process.exit(1);
}
else return true; else return true;
} }
@ -166,9 +169,7 @@ module.exports = {
// Get time at the start of upload // Get time at the start of upload
if (useUploadKey) { if (useUploadKey && eusConfig["uploadKey"] != req.header("key")) return res.end("Incorrect key provided for upload");
if (eusConfig["uploadKey"] != req.header("key")) return res.end("Incorrect key provided for upload");
}
d = new Date(); startTime = d.getTime(); d = new Date(); startTime = d.getTime();
var fstream; var fstream;
@ -211,76 +212,80 @@ module.exports = {
} }
function handleAPI(req, res) { function handleAPI(req, res) {
switch (req.url.split("?")[0]) {
// Status check to see the onlint status of EUS
// Used by ESL to make sure EUS is online
case "/api/get-server-status":
return res.end('{ "status":1, "version":"'+global.internals.version+'" }');
// Status check for ESL to make sure EUS is online /* Stats api endpoint
if (req.query["stat"] == "get") return res.end('{ "status":1, "version":"'+global.internals.version+'" }'); Query inputs
f : Values [0,1]
/* Stats api endpoint s : Values [0,1]
Query inputs */
f : Values [0,1] case "/api/get-stats":
s : Values [0,1] const filesaa = req.query["f"],
*/ spaceaa = req.query["s"];
if (req.url.split("?")[0] == "/api/get-stats") { let jsonaa = {};
const filesaa = req.query["f"], // If total files is asked for
spaceaa = req.query["s"]; if (filesaa == 1) {
let jsonaa = {}; let total = 0;
// If total files is asked for jsonaa["files"] = {};
if (filesaa == 1) { // Add each accepted file type to the json
let total = 0; for (var i2 = 0; i2 < eusConfig.acceptedTypes.length; i2++) {
jsonaa["files"] = {}; jsonaa["files"][`${eusConfig.acceptedTypes[i2]}`.replace(".", "")] = 0;
// Add each accepted file type to the json }
for (var i2 = 0; i2 < eusConfig.acceptedTypes.length; i2++) { // Read all files from the images directory
jsonaa["files"][`${eusConfig.acceptedTypes[i2]}`.replace(".", "")] = 0; fs.readdir(__dirname + BASE_PATH + "/i", (err, files) => {
} if (err) throw err;
// Read all files from the images directory // Loop through all files
fs.readdir(__dirname + BASE_PATH + "/i", (err, files) => { for (var i = 0; i < files.length; i++) {
if (err) throw err; // Loop through all accepted file types to check for a match
// Loop through all files for (var i1 = 0; i1 < eusConfig.acceptedTypes.length; i1++) {
for (var i = 0; i < files.length; i++) { const jsudfg = files[i].split(".");
// Loop through all accepted file types to check for a match if (`.${jsudfg[jsudfg.length-1]}` == eusConfig.acceptedTypes[i1]) {
for (var i1 = 0; i1 < eusConfig.acceptedTypes.length; i1++) { // There is a match! Add it to the json
const jsudfg = files[i].split("."); jsonaa["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++;
if (`.${jsudfg[jsudfg.length-1]}` == eusConfig.acceptedTypes[i1]) { // Also increase the total
// There is a match! Add it to the json total++;
jsonaa["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++; }
// Also increase the total
total++;
} }
} }
} // Set the total in the json to the calculated total value
// Set the total in the json to the calculated total value jsonaa["files"]["total"] = total;
jsonaa["files"]["total"] = total;
// If getting the space used on the server isn't required send the json // If getting the space used on the server isn't required send the json
if (spaceaa != 1) return res.end(JSON.stringify(jsonaa)); if (spaceaa != 1) return res.end(JSON.stringify(jsonaa));
}); });
} }
// Getting space is required // Getting space is required
if (spaceaa == 1) { if (spaceaa == 1) {
jsonaa["space"] = {}; jsonaa["space"] = {};
// Get the space used on the disk // Get the space used on the disk
getSize(__dirname + BASE_PATH + "/i", (err, size) => { getSize(__dirname + BASE_PATH + "/i", (err, size) => {
if (err) throw err; if (err) throw err;
// 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 / 1024 / 1024);
jsonaa["space"]["mb"] = sizeOfFolder; jsonaa["space"]["mb"] = sizeOfFolder;
sizeOfFolder = (size / 1024 / 1024 / 1024); sizeOfFolder = (size / 1024 / 1024 / 1024);
jsonaa["space"]["gb"] = sizeOfFolder; jsonaa["space"]["gb"] = sizeOfFolder;
sizeOfFolder = (size / 1024 / 1024 / 1024).toFixed(2); sizeOfFolder = (size / 1024 / 1024 / 1024).toFixed(2);
jsonaa["space"]["string"] = `${sizeOfFolder} GB`; jsonaa["space"]["string"] = `${sizeOfFolder} GB`;
// Send the json to the requesting client // Send the json to the requesting client
return res.end(JSON.stringify(jsonaa)); return res.end(JSON.stringify(jsonaa));
}); });
} }
}
// Information API if (filesaa != 1 && spaceaa != 1) return res.end("Please add f and or s to your queries to get the files and space");
if (req.url.split("?")[0] == "/api/get-info") { break;
let jsonaa = {
version: global.internals.version, // Information API
instance: config["server"]["instance_type"] case "/api/get-info":
}; let jsona = {
return res.end(JSON.stringify(jsonaa)); version: global.internals.version,
instance: config["server"]["instance_type"]
};
return res.end(JSON.stringify(jsona));
} }
} }