diff --git a/index.js b/index.js index 90824f1..078bcfc 100644 --- a/index.js +++ b/index.js @@ -7,19 +7,13 @@ import cookieParser from 'cookie-parser'; import cors from 'cors'; import { initWebsocket } from './src/websocket.js'; import { initAuth } from './src/auth.js'; -import { close as closeDbConnection, initDbConnection, db } from './src/db.js'; +import { initDbConnection, db } from './src/db.js'; import { initUsers } from './src/user.js'; import { initCdn } from './src/cdn.js'; const app = express(); const appWs = expressWs(app); const port = 12345; -process.on('exit', function () { - console.log('Shutting down...'); - console.log('Closing db connection...'); - closeDbConnection(); -}); - app.use(cors({ credentials: true, origin: process.env.JEOPARDY_URL })); app.use(morgan(process.env.production ? 'common' : 'dev')); app.use(express.json()); diff --git a/src/cdn.js b/src/cdn.js index 445bbe1..4f7df5a 100644 --- a/src/cdn.js +++ b/src/cdn.js @@ -1,5 +1,5 @@ import { rmSync } from 'fs'; -import { copyFile, mkdir } from 'fs/promises'; +import { copyFile, mkdir, readdir } from 'fs/promises'; import { Collection, Db, ObjectId } from 'mongodb'; import multer from 'multer'; @@ -11,6 +11,10 @@ const upload = multer({ dest: dataPath }); */ let ressources; +function buildPath(userid, path) { + return `${dataPath}/${userid}${path}`; +} + /** * * @param {*} app @@ -20,6 +24,8 @@ export function initCdn(app, db) { ressources = db.collection('ressources'); app.post('/upload', upload.single('file'), uploadFile); app.get('/cdn/:userid/:resid', fetchFile); + app.post('/directory', fetchDirectory); + app.put('/directory', addDirectory); } /** @@ -37,7 +43,7 @@ function uploadFile(req, res, next) { const path = req.body.path; if (path !== undefined && path.startsWith('/') && !path.includes('.')) { - let destinationPath = `${dataPath}/${req.user._id}${req.body.path}`; + let destinationPath = buildPath(req.user._id, req.body.path); mkdir(destinationPath, { recursive: true, }) @@ -89,3 +95,66 @@ async function fetchFile(req, res) { res.sendStatus(404); } } + +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +async function fetchDirectory(req, res) { + if (!req.body) { + res.sendStatus(400); + return; + } + const path = req.body.path; + if (!path) { + res.sendStatus(400); + return; + } + + const files = ressources.find({ + path, + }); + + readdir(buildPath(req.user._id, path), { + withFileTypes: true, + }) + .then(async (value) => { + let directories = value + .filter((dir) => dir.isDirectory()) + .map((dir) => { + return { + name: dir.name, + isDir: true, + }; + }); + + res.status(200).send([...directories, ...(await files.toArray())]); + }) + .catch(async (err) => { + console.error(err); + res.status(200).send(await files.toArray()); + }); +} + +async function addDirectory(req, res) { + if (!req.body) { + res.sendStatus(400); + return; + } + const name = req.body.name; + const path = req.body.path; + if (!name || !path || !/^[a-zA-Z0-9-_]+$/.test(name)) { + res.sendStatus(400); + return; + } + + mkdir(buildPath(req.user._id, path + '/' + name)) + .then(() => { + res.sendStatus(200); + }) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +} diff --git a/src/db.js b/src/db.js index 4f9c90a..7f0167b 100644 --- a/src/db.js +++ b/src/db.js @@ -1,5 +1,8 @@ -import { MongoClient } from "mongodb"; +import { MongoClient } from 'mongodb'; +/** + * @type {MongoClient} + */ let client; const dbName = `jeopardy`; @@ -10,12 +13,10 @@ const dbName = `jeopardy`; export let db; export async function initDbConnection() { - client = new MongoClient(`mongodb://${process.env.JEOPARDYSERVER_MONGO_USERNAME}:${process.env.JEOPARDYSERVER_MONGO_PASSWORD}@${process.env.JEOPARDYSERVER_MONGO_URL}/`); + client = new MongoClient( + `mongodb://${process.env.JEOPARDYSERVER_MONGO_USERNAME}:${process.env.JEOPARDYSERVER_MONGO_PASSWORD}@${process.env.JEOPARDYSERVER_MONGO_URL}/`, + ); await client.connect(); console.log('Connected successfully to mongodb'); db = client.db(dbName); } - -export function close() { - client.close(); -}