Initial code commit

Put all code for the Open Source Github release up.
This commit is contained in:
tgpethan 2019-09-07 15:57:59 +01:00
parent ce5ecb06e3
commit 1b54c54b83
9 changed files with 255 additions and 0 deletions

6
.gitignore vendored
View file

@ -1,3 +1,9 @@
config/config.json
i/
image-type.json
.vscode/
package-lock.json
# Logs # Logs
logs logs
*.log *.log

View file

@ -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"
]
}
}

33
files/index.html Normal file
View file

@ -0,0 +1,33 @@
<html>
<head>
<title>EUS - Default Page</title>
<style>
footer {
position: fixed;
background-color: #1B1C1D;
height: 30px;
width: 100%;
bottom: 0;
left: 0;
z-index: 200;
text-align: right;
}
.footer-element {
position: absolute;
right: 12px;
top: 5px;
color: white;
}
.footer-element b {
color:#008B8B;
}
</style>
</head>
<body>
<footer>
<div class="footer-element">
Running Revolution Version: <b>|replaceVersion|</b> | Instance: <b>|replaceInstance|</b>
</div>
</footer>
</body>
</html>

102
index.js Normal file
View file

@ -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<hr>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`);
});
}

6
misc/ascii.txt Normal file
View file

@ -0,0 +1,6 @@
____ __ __ _
/ __ \___ _ ______ / /_ __/ /_(_)___ ____ |titlecard|
/ /_/ / _ \ | / / __ \/ / / / / __/ / __ \/ __ \ |replaceVersion|
/ _, _/ __/ |/ / /_/ / / /_/ / /_/ / /_/ / / / / |replaceStats|
/_/ |_|\___/|___/\____/_/\__,_/\__/_/\____/_/ /_/ |replaceType|
--------------------------------------------------------------------------------

10
misc/emoji_list.json Normal file
View file

@ -0,0 +1,10 @@
{
"wave":"👋",
"thumb_up":"👍",
"page":"📄",
"dizzy":"😵",
"heavy_check":"✔️",
"folder":"📁",
"fast_up":"⏫",
"cross":"❌"
}

19
modules/logger.js Normal file
View file

@ -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;
}
}

View file

@ -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`);
}
});
}
}
}

15
package.json Normal file
View file

@ -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"
}
}