feat(api): implement FileCheckerAPI class with routes for file analysis

This commit is contained in:
buzz-lightsnack-2007 2025-04-21 21:40:26 +08:00
parent 74fb118aa4
commit 87fdb0688e
2 changed files with 79 additions and 13 deletions

View file

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

41
scripts/API/main.js Normal file
View file

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