From 7e4e9a4fee03345064d9912ddb40b1b1da5fa971 Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 4 Jan 2020 10:44:20 +0000 Subject: [PATCH] Add comments to EUS.js --- EUS.js | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/EUS.js b/EUS.js index cd40fae..332003d 100644 --- a/EUS.js +++ b/EUS.js @@ -36,22 +36,27 @@ if (!fs.existsSync(__dirname + BASE_PATH + "/i")) { // Makes the image-type file fs.access(`${__dirname}${BASE_PATH}/image-type.json`, error => { if (error) { + // Doesn't exist, create it. fs.writeFile(`${__dirname}${BASE_PATH}/image-type.json`, '{\n\}', function(err) { if (err) throw err; global.modules.consoleHelper.printInfo(emoji.heavy_check, "Created image-type File!"); + // File has been created, load it. image_json = require(`${__dirname}${BASE_PATH}/image-type.json`); }); } else { + // File already exists, load it. image_json = require(`${__dirname}${BASE_PATH}/image-type.json`); } }); // Makes the config file fs.access(`${__dirname}${BASE_PATH}/config.json`, error => { if (error) { + // Config doesn't exist, make it. fs.writeFile(`${__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}', function(err) { if (err) throw err; global.modules.consoleHelper.printInfo(emoji.heavy_check, "Created config File!"); global.modules.consoleHelper.printInfo(emoji.wave, "Please edit the EUS Config file before restarting."); + // Config has been made, close framework. process.exit(0); }); } else { @@ -64,6 +69,7 @@ const exportURL = `${eusConfig.baseURL}:${config.server.port}/`; module.exports = { extras:function() { + // Setup express to use busboy global.app.use(busboy()); }, get:function(req, res) { @@ -72,18 +78,24 @@ module.exports = { res - Response from server */ + // 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 fs.access(__dirname + BASE_PATH + "/files"+req.url, 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(); @@ -91,6 +103,7 @@ module.exports = { } }); } else { + // Image does exist, send it back. res.sendFile(__dirname + BASE_PATH + "/i/"+urs+image_json[urs]); d = new Date(); endTime = d.getTime(); @@ -104,32 +117,44 @@ module.exports = { res - Response from server */ + // Make sure the endpoint is /upload + // If it isn't upload send an empty response if (req.url != "/upload") return req.end(""); + // Get time at the start of upload d = new Date(); startTime = d.getTime(); var fstream; var thefe; + // Pipe the request to busboy req.pipe(req.busboy); req.busboy.on('file', function (fieldname, file, filename) { + // Get the image-type json image_json = require(`${__dirname}${BASE_PATH}/image-type.json`); + // Make a new file name fileOutName = randomstring.generate(14); - global.modules.consoleHelper.printInfo(emoji.fast_up, `${req.method}: Upload of ${fileOutName} started.`); + global.modules.consoleHelper.printInfo(emoji.fast_up, `${req.method}: Upload of ${fileOutName} started.`); + // Check the file is within the accepted file types if (eusConfig.acceptedTypes.includes(`.${filename.split(".")[filename.split(".").length-1]}`)) { + // File is accepted, set the extention of the file in thefe for later use. thefe = `.${filename.split(".")[filename.split(".").length-1]}`; } else { + // File isn't accepted, send response back to client stating so. res.end("This file type isn't accepted currently."); return; } - //Path where image will be uploaded + // Create a write stream for the file fstream = fs.createWriteStream(__dirname + BASE_PATH + "/i/" + fileOutName + thefe); file.pipe(fstream); fstream.on('close', function () { - d = new Date(); - endTime = d.getTime(); + // Get the time at the end of the upload + d = new Date(); endTime = d.getTime(); + // 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; - global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: Upload of ${fileOutName} finished. Took ${endTime - startTime}ms`); + 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); }); });