commit
78c6d91cb0
1 changed files with 333 additions and 320 deletions
97
EUS.js
97
EUS.js
|
@ -4,7 +4,7 @@ const fs = require("fs"),
|
|||
busboy = require("connect-busboy"),
|
||||
randomstring = require("randomstring"),
|
||||
getSize = require("get-folder-size"),
|
||||
diskUsage = require("diskusage"),
|
||||
diskUsage = require("diskusage-ng"),
|
||||
emoji = require("../misc/emoji_list.json");
|
||||
|
||||
// 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");
|
||||
}
|
||||
|
||||
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
|
||||
let cacheIsReady = false;
|
||||
async function cacheFilesAndSpace() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const startCacheTime = new Date().getTime();
|
||||
let cachedFilesAndSpace = {
|
||||
files: {}
|
||||
|
@ -89,13 +81,14 @@ async function cacheFilesAndSpace() {
|
|||
}
|
||||
// Read all files from the images directory
|
||||
fs.readdir(__dirname + BASE_PATH + "/i", async (err, files) => {
|
||||
if (err) throw err;
|
||||
if (err) reject(err);
|
||||
else {
|
||||
// Loop through all files
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
// Loop through all accepted file types to check for a match
|
||||
for (var i1 = 0; i1 < eusConfig.acceptedTypes.length; i1++) {
|
||||
const jsudfg = files[i].split(".");
|
||||
if (`.${jsudfg[jsudfg.length-1]}` == eusConfig.acceptedTypes[i1]) {
|
||||
const readFileName = files[i].split(".");
|
||||
if (`.${readFileName[readFileName.length-1]}` == eusConfig.acceptedTypes[i1]) {
|
||||
// There is a match! Add it to the json
|
||||
cachedFilesAndSpace["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++;
|
||||
// Also increase the total
|
||||
|
@ -112,28 +105,33 @@ async function cacheFilesAndSpace() {
|
|||
};
|
||||
// Get the space used on the disk
|
||||
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
|
||||
let sizeOfFolder = (size / 1024 / 1024);
|
||||
let sizeOfFolder = (size / 2048);
|
||||
cachedFilesAndSpace["space"]["usage"]["mb"] = sizeOfFolder;
|
||||
sizeOfFolder = (size / 1024 / 1024 / 1024);
|
||||
sizeOfFolder = (size / 3072);
|
||||
cachedFilesAndSpace["space"]["usage"]["gb"] = sizeOfFolder;
|
||||
cachedFilesAndSpace["space"]["usage"]["string"] = spaceToLowest(size, true);
|
||||
cachedFilesAndSpace["space"]["usage"]["string"] = await spaceToLowest(size, true);
|
||||
// Get total disk space
|
||||
diskUsage.check(__dirname, async (err, data) => {
|
||||
if (err) throw err;
|
||||
diskUsage(__dirname, async (err, data) => {
|
||||
if (err) reject(err);
|
||||
else {
|
||||
cachedFilesAndSpace["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)
|
||||
value: await spaceToLowest(data["total"], false),
|
||||
mbvalue: (data["total"] / 2048),
|
||||
gbvalue: (data["total"] / 3072),
|
||||
stringValue: (await spaceToLowest(data["total"], true)).split(" ")[1].toLowerCase(),
|
||||
string: await spaceToLowest(data["total"], true)
|
||||
};
|
||||
|
||||
cacheIsReady = true;
|
||||
cacheJSON = JSON.stringify(cachedFilesAndSpace);
|
||||
resolve(cachedFilesAndSpace);
|
||||
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;
|
||||
}
|
||||
|
||||
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 = {
|
||||
extras:function() {
|
||||
extras:async function() {
|
||||
// Setup express to 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
|
||||
res - Response from server
|
||||
|
@ -207,17 +215,18 @@ module.exports = {
|
|||
// 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];
|
||||
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 => {
|
||||
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 => {
|
||||
fs.access(`${__dirname}${BASE_PATH}/files${urs}`, (error) => {
|
||||
if (error) {
|
||||
// Doesn't exist, send a 404 to the client.
|
||||
res.status(404).end("404!");
|
||||
res.status(404).send("404!<hr>EUS");
|
||||
d = new Date();
|
||||
endTime = d.getTime();
|
||||
global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`);
|
||||
|
@ -231,14 +240,14 @@ module.exports = {
|
|||
});
|
||||
} else {
|
||||
// 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();
|
||||
endTime = d.getTime();
|
||||
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
|
||||
res - Response from server
|
||||
|
@ -281,21 +290,23 @@ module.exports = {
|
|||
// Add image file type to the image_json array
|
||||
image_json[fileOutName] = `.${filename.split(".")[filename.split(".").length-1]}`;
|
||||
// Save image_json array to the file
|
||||
fs.writeFile(`${__dirname}${BASE_PATH}/image-type.json`, JSON.stringify(image_json), function(err) {
|
||||
if (err) throw err;
|
||||
fs.writeFile(`${__dirname}${BASE_PATH}/image-type.json`, JSON.stringify(image_json), async (err) => {
|
||||
if (err) reject(err);
|
||||
else {
|
||||
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(eusConfig.baseURL+""+fileOutName);
|
||||
|
||||
// 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();
|
||||
let jsonaa = {}, filesaa = 0, spaceaa = 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:
|
||||
|
||||
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
|
||||
let i1 = 1;
|
||||
// Loop through until value is at it's lowest
|
||||
|
@ -376,8 +388,9 @@ function spaceToLowest(spaceValue, includeStringValue) {
|
|||
if (spaceValue >= 1024) i1++;
|
||||
}
|
||||
|
||||
if (includeStringValue) return `${spaceValue.toFixed(2)} ${spaceValues[i1]}`;
|
||||
else return spaceValue;
|
||||
if (includeStringValue) resolve(`${spaceValue.toFixed(2)} ${spaceValues[i1]}`);
|
||||
else resolve(spaceValue);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports.MOD_FUNC = MODULE_FUNCTION;
|
Loading…
Reference in a new issue