From 87fdb0688ee910fc835b2c07231ed3e55d28bb01 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Mon, 21 Apr 2025 21:40:26 +0800 Subject: [PATCH] feat(api): implement FileCheckerAPI class with routes for file analysis --- index.js | 51 +++++++++++++++++++++++++++++++++------------ scripts/API/main.js | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 scripts/API/main.js diff --git a/index.js b/index.js index 5a66bb6..d2cf560 100644 --- a/index.js +++ b/index.js @@ -1,20 +1,45 @@ -var express = require('express'); -var cors = require('cors'); -require('dotenv').config() +/* Import modules */ +require('dotenv').config(); +const express = require('express'); +const cors = require('cors'); +const app = express(); -var app = express(); +const FileCheckerAPI = require('./scripts/API/main'); -app.use(cors()); -app.use('/public', express.static(process.cwd() + '/public')); +class WebServer { + // Basic Configuration + static port = process.env.PORT || 3000; + + /* + Initiate the web server. + @param {function} callback - The callback run before activating the server + */ + constructor(callback) { + app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204 + // app.use(cors()); + + this.#setDefaults(); + (callback) ? this[`activity`] = callback() : null; -app.get('/', function (req, res) { - res.sendFile(process.cwd() + '/views/index.html'); -}); + var listener = app.listen(WebServer.port, () => { + console.log(`Active on port ${listener.address().port}.`); + }); + } + /* Configure the default responses for the web server. */ + #setDefaults() { + app.use(WebServer.paths['assets'], express.static(__dirname + WebServer.paths['assets'])); + app.get("/", function (REQUEST, RESPONSE) { + RESPONSE.sendFile(__dirname + WebServer[`paths`][`default`]); + }); + } + + static paths = { + "assets": "/public", + "default": '/views/index.htm' + } +} +new WebServer(() => {return (new FileCheckerAPI(app))}); -const port = process.env.PORT || 3000; -app.listen(port, function () { - console.log('Your app is listening on port ' + port) -}); diff --git a/scripts/API/main.js b/scripts/API/main.js new file mode 100644 index 0000000..f021bb3 --- /dev/null +++ b/scripts/API/main.js @@ -0,0 +1,41 @@ +const bodyParser = require(`body-parser`); +const multer = require("multer"); +const fs = require('fs'); + +const Actions = { + 'metadata': require(`./actions/metadata`), +} + +const FileCheckerAPI = class API { + #paths = { + "analyze": ["/api/fileanalyse"] + } + + // Constructor for the API class. + constructor(INSTANCE) { + this[`instance`] = INSTANCE; + + const setConfig = () => { + // Set up body-parser and multer. + this[`instance`][`use`](bodyParser[`json`]()); + this[`instance`][`use`](bodyParser[`urlencoded`]({ extended: true })); + const upload = multer({ dest: 'uploads/' }); + this[`instance`][`use`](upload.single('upfile')); + } + + setConfig(); + this.initRoutes(); + + }; + + // Set up the routes for the API. + initRoutes() { + this.#paths[`analyze`].forEach((PATH) => { + [``, `.jsp`, `.php`, `.asp`, `.html`].forEach((SUFFIX) => { + this[`instance`][`post`](PATH + SUFFIX, Actions.metadata.analyze); + }); + }); + } +} + +module.exports = FileCheckerAPI; \ No newline at end of file