diff --git a/.gitignore b/.gitignore index ed0d3c8..d6352f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ +config/config.json +i/ +image-type.json +.vscode/ +package-lock.json + # Logs logs *.log diff --git a/config/config.example.json b/config/config.example.json new file mode 100644 index 0000000..02991ef --- /dev/null +++ b/config/config.example.json @@ -0,0 +1,14 @@ +{ + "server": { + "port":8080, + "image_dir":"./i/", + "export_uri":"This url is used to return to the user when an image is uploaded e.g http://localhost:8080/", + "instance_type":"Github Open Release", + "acceptedTypes":[ + ".png", + ".jpg", + ".jpeg", + ".gif" + ] + } +} \ No newline at end of file diff --git a/files/index.html b/files/index.html new file mode 100644 index 0000000..3e3c70c --- /dev/null +++ b/files/index.html @@ -0,0 +1,33 @@ + + + EUS - Default Page + + + + + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..37e5178 --- /dev/null +++ b/index.js @@ -0,0 +1,102 @@ +const express = require("express"), app = express(), config = require("./config/config.json"), emoji = require("./misc/emoji_list.json"), randomstring = require("randomstring"), fs = require("fs"), chalk = require("chalk"), busboy = require("connect-busboy"), dirname = __dirname+"/"; +internals = { + version:"0.0.5 Open", + types: { + a:"INFO", + b:"REQUEST", + c:"WARN" + } +}; +let dE = new Date(), startTime = dE.getTime(), endTime, modules = [], img_json; + +// Clear console before printing anything +console.clear(); + +fs.readFile('./misc/ascii.txt', function(err, data) { + if (err) throw err; + fs.readdir(config.server.image_dir, (err, files) => { + // Generate the banner + let asciiOut = data.toString() + .replace("|replaceVersion|", `${chalk.yellow("Version:")} ${chalk.cyan(internals.version)}`) + .replace("|titlecard|", chalk.yellow("The web server made for EUS")) + .replace("DEV", chalk.red("DEV")).replace("RELEASE", chalk.green("RELEASE")) + .replace("|replaceType|", `${chalk.yellow("Type: ")}${chalk.cyan(config.server.instance_type)}`) + .replace("|replaceStats|", `${chalk.yellow("Images: ")}${chalk.cyan(files.length)}`); + // Print the banner + console.log(asciiOut); + // Get the modules from the ./modules folder + fs.readdir("./modules", (err, files) => { + if (err) throw err; + for (var i = 0; i < files.length; i++) { + /* + For every file in the array, output that it was found + in the console and attempt to load it using require. + Oh, and check that it has .js in it's file name! + */ + if (files[i].includes(".js")) { + modules[files[i].toString().replace(".js", "")] = require(`./modules/${files[i].toString()}`); + console.log(`[Modules] Found module ${files[i].toString()}`); + } else { + console.log(`[Modules] Found file ${files[i]}. It is not a module.`) + } + } + fs.access("./image-type.json", error => { + if (error) { + fs.writeFile('./image-type.json', '{\n\}', function(err) { + if (err) throw err; + modules.logger.log(internals.types.a, emoji.thumb_down, "Created image-type File!"); + }); + } else { + image_json = require("./image-type.json"); + } + }); + modules.logger.log(internals.types.a, emoji.wave, "Starting Revolution..."); + server(); + }); + }); +}); + +// File name that is returned to the uploader +let fileOutName; + +function server() { + app.use(busboy()); + app.route('/upload') + .post(function (req, res, next) { + dE = new Date(); startTime = dE.getTime(); + var fstream; + var thefe; + req.pipe(req.busboy); + req.busboy.on('file', function (fieldname, file, filename) { + image_json = require("./image-type.json"); + fileOutName = randomstring.generate(14); + modules.logger.log(`${internals.types.b}: ${req.method}`, emoji.fast_up, `Upload of ${fileOutName} started.`); + if (config.server.acceptedTypes.includes(`.${filename.split(".")[filename.split(".").length-1]}`)) { + thefe = `.${filename.split(".")[filename.split(".").length-1]}`; + } else { + res.end("This file type isn't accepted currently."); + return; + } + //Path where image will be uploaded + fstream = fs.createWriteStream(__dirname + '/i/' + fileOutName + thefe); + file.pipe(fstream); + fstream.on('close', function () { + dE = new Date(); + endTime = dE.getTime(); + image_json[fileOutName] = `.${filename.split(".")[filename.split(".").length-1]}`; + fs.writeFile('./image-type.json', JSON.stringify(image_json), function(err) { + if (err) throw err; + modules.logger.log(`${internals.types.b}: ${req.method}`, emoji.heavy_check, ` Upload of ${fileOutName} finished. Took ${endTime - startTime}ms`); + res.end(config.server.export_uri+""+fileOutName); + }); + }); + }); + }) + .get(function (req, res, next) { + res.status(405).send("405! You requested the \"/upload\" endpoint with method \"GET\" but it was expecting POST
Revolution - A web server designed for EUS") + }); + app.get('*', (req, res) => { modules.request_handler.handle(modules.logger, image_json, internals, emoji, config, fs, chalk, req, res, dirname); }); + app.listen(config.server.port, () => { dE = new Date(), endTime = dE.getTime(); + modules.logger.log(internals.types.a, emoji.thumb_up, `Started Revolution on port ${config.server.port}! Took ${endTime - startTime}ms`); + }); +} \ No newline at end of file diff --git a/misc/ascii.txt b/misc/ascii.txt new file mode 100644 index 0000000..1f3b946 --- /dev/null +++ b/misc/ascii.txt @@ -0,0 +1,6 @@ + ____ __ __ _ + / __ \___ _ ______ / /_ __/ /_(_)___ ____ |titlecard| + / /_/ / _ \ | / / __ \/ / / / / __/ / __ \/ __ \ |replaceVersion| + / _, _/ __/ |/ / /_/ / / /_/ / /_/ / /_/ / / / / |replaceStats| +/_/ |_|\___/|___/\____/_/\__,_/\__/_/\____/_/ /_/ |replaceType| +-------------------------------------------------------------------------------- \ No newline at end of file diff --git a/misc/emoji_list.json b/misc/emoji_list.json new file mode 100644 index 0000000..e330c85 --- /dev/null +++ b/misc/emoji_list.json @@ -0,0 +1,10 @@ +{ + "wave":"👋", + "thumb_up":"👍", + "page":"📄", + "dizzy":"😵", + "heavy_check":"✔️", + "folder":"📁", + "fast_up":"⏫", + "cross":"❌" +} \ No newline at end of file diff --git a/modules/logger.js b/modules/logger.js new file mode 100644 index 0000000..d1da0cd --- /dev/null +++ b/modules/logger.js @@ -0,0 +1,19 @@ +"use strict"; +const fs = require("fs"); +const chalk = require("chalk"); +let d = new Date(); + +module.exports = { + log: function(type, emoji, text) { + d = new Date(); + console.log(`${chalk.green(`[${timeNumbers(d.getHours())}:${timeNumbers(d.getMinutes())}:${timeNumbers(d.getSeconds())} - ${type}]`)} ${emoji} ${text}`) + } +} + +function timeNumbers(inp) { + if (inp <= 9) { + return "0"+inp; + } else { + return inp; + } +} \ No newline at end of file diff --git a/modules/request_handler.js b/modules/request_handler.js new file mode 100644 index 0000000..3ded021 --- /dev/null +++ b/modules/request_handler.js @@ -0,0 +1,50 @@ +"use strict"; +let d = new Date(), +startTime, +endTime, +filesC; + +module.exports = { + handle: function(logger, image_json, internals, emoji, config, fs, chalk, req, res, dirname) { + d = new Date(); + startTime = d.getTime(); + res.set('Server-Type', 'Revolution EUS'); + if (req.url == "/" || req.url == "/index.html") { + fs.readFile('./files/index.html', function(err, data) { + if (err) throw err; + fs.readdir(dirname+"i/", (err, files) => { + if (err) throw err; + filesC = data.toString().replace("|replaceVersion|", internals.version).replace("|replaceInstance|", config.server.instance_type); + res.send(filesC); + d = new Date(); + endTime = d.getTime(); + logger.log(`${internals.types.b}: ${req.method}`, emoji.page, `${req.ip} | ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`) + }); + }); + } else { + let urs = ""+req.url; urs = urs.split("/")[1]; + fs.access(config.server.image_dir+urs+image_json[urs], error => { + if (error) { + fs.access("./files"+req.url, error => { + if (error) { + res.status(404).end("404!"); + d = new Date(); + endTime = d.getTime(); + logger.log(`${internals.types.b}: ${req.method}`, emoji.cross, `${req.ip} | ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`); + } else { + res.sendFile(dirname+"files"+req.url); + d = new Date(); + endTime = d.getTime(); + logger.log(`${internals.types.b}: ${req.method}`, emoji.heavy_check, `${req.ip} | ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); + } + }); + } else { + res.sendFile(dirname+"i/"+urs+image_json[urs]); + d = new Date(); + endTime = d.getTime(); + logger.log(`${internals.types.b}: ${req.method}`, emoji.heavy_check, `${req.ip} | ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); + } + }); + } + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..795af66 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "neweus", + "version": "0.0.5", + "description": "", + "main": "index.js", + "author": "", + "license": "ISC", + "dependencies": { + "chalk": "^2.4.2", + "connect-busboy": "0.0.2", + "express": "^4.17.1", + "http": "0.0.0", + "randomstring": "^1.1.5" + } +}