Added creation and fetching of directories and files

This commit is contained in:
2025-12-23 12:25:55 +01:00
parent 273314739f
commit 08f8cc7fc3
3 changed files with 79 additions and 15 deletions

View File

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

View File

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

View File

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