From 819ccad51e996b1b92e6a7432dfa660e886dc11a Mon Sep 17 00:00:00 2001 From: tgpethan Date: Sat, 23 Nov 2019 21:02:50 +0000 Subject: [PATCH] Push new stuff --- .gitattributes | 2 - .gitignore | 94 ------------------------------------ EUS.js | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 96 deletions(-) delete mode 100644 .gitattributes delete mode 100644 .gitignore create mode 100644 EUS.js diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index dfe0770..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto diff --git a/.gitignore b/.gitignore deleted file mode 100644 index d6352f1..0000000 --- a/.gitignore +++ /dev/null @@ -1,94 +0,0 @@ -config/config.json -i/ -image-type.json -.vscode/ -package-lock.json - -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* -lerna-debug.log* - -# Diagnostic reports (https://nodejs.org/api/report.html) -report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -*.lcov - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# TypeScript cache -*.tsbuildinfo - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env -.env.test - -# parcel-bundler cache (https://parceljs.org/) -.cache - -# next.js build output -.next - -# nuxt.js build output -.nuxt - -# vuepress build output -.vuepress/dist - -# Serverless directories -.serverless/ - -# FuseBox cache -.fusebox/ - -# DynamoDB Local files -.dynamodb/ diff --git a/EUS.js b/EUS.js new file mode 100644 index 0000000..d6c5ade --- /dev/null +++ b/EUS.js @@ -0,0 +1,127 @@ +const fs = require("fs"), +config = require("../config/config.json"), +chalk = require("chalk"), +busboy = require("connect-busboy"), +randomstring = require("randomstring"), +emoji = require("../misc/emoji_list.json"); + +// Defines the function of this module +const MODULE_FUNCTION = "handle_requests", + +// Base path for module folder creation and navigation +BASE_PATH = "/EUS"; + +const exportURI = "http://localhost:"+config.server.port+"/"; +const acceptedTypes = [ + ".png", + ".jpg", + ".jpeg", + ".gif" +]; +let image_json = {}, +d = new Date(), +startTime, +endTime; + +// Only ran on startup so using sync functions is fine +// Makes the folders for files of the module +if (!fs.existsSync(__dirname + BASE_PATH)) { + fs.mkdirSync(__dirname + BASE_PATH); + console.log(`[EUS] Made EUS module folder`); +} +if (!fs.existsSync(__dirname + BASE_PATH + "/files")) { + fs.mkdirSync(__dirname + BASE_PATH + "/files"); + console.log(`[EUS] Made EUS web files folder`); +} +if (!fs.existsSync(__dirname + BASE_PATH + "/i")) { + fs.mkdirSync(__dirname + BASE_PATH + "/i"); + console.log(`[EUS] Made EUS images folder`); +} +fs.access(`${__dirname}${BASE_PATH}/image-type.json`, error => { + if (error) { + 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!"); + image_json = require(`${__dirname}${BASE_PATH}/image-type.json`); + }); + } else { + image_json = require(`${__dirname}${BASE_PATH}/image-type.json`); + } +}); + +module.exports = { + extras:function() { + global.app.use(busboy()); + }, + get:function(req, res) { + /* + req - Request from client + res - Response from server + */ + + d = new Date(); + startTime = d.getTime(); + let urs = ""+req.url; urs = urs.split("/")[1]; + fs.access(__dirname + BASE_PATH + "/i/"+urs+image_json[urs], error => { + if (error) { + fs.access(__dirname + BASE_PATH + "/files"+req.url, error => { + if (error) { + 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 { + res.sendFile(__dirname + BASE_PATH + "/files"+req.url); + d = new Date(); + endTime = d.getTime(); + global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); + } + }); + } else { + 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) { + /* + req - Request from client + res - Response from server + */ + + if (req.url != "/upload") return req.end(""); + + d = new Date(); startTime = d.getTime(); + var fstream; + var thefe; + req.pipe(req.busboy); + req.busboy.on('file', function (fieldname, file, filename) { + image_json = require(`${__dirname}${BASE_PATH}/image-type.json`); + fileOutName = randomstring.generate(14); + global.modules.consoleHelper.printInfo(emoji.fast_up, `${req.method}: Upload of ${fileOutName} started.`); + if (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 + BASE_PATH + "/i/" + fileOutName + thefe); + file.pipe(fstream); + fstream.on('close', function () { + d = new Date(); + endTime = d.getTime(); + image_json[fileOutName] = `.${filename.split(".")[filename.split(".").length-1]}`; + 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`); + res.end(exportURI+""+fileOutName); + }); + }); + }); + } +} + +module.exports.MOD_FUNC = MODULE_FUNCTION; \ No newline at end of file