Added creation and fetching of directories and files
This commit is contained in:
8
index.js
8
index.js
@@ -7,19 +7,13 @@ import cookieParser from 'cookie-parser';
|
|||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import { initWebsocket } from './src/websocket.js';
|
import { initWebsocket } from './src/websocket.js';
|
||||||
import { initAuth } from './src/auth.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 { initUsers } from './src/user.js';
|
||||||
import { initCdn } from './src/cdn.js';
|
import { initCdn } from './src/cdn.js';
|
||||||
const app = express();
|
const app = express();
|
||||||
const appWs = expressWs(app);
|
const appWs = expressWs(app);
|
||||||
const port = 12345;
|
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(cors({ credentials: true, origin: process.env.JEOPARDY_URL }));
|
||||||
app.use(morgan(process.env.production ? 'common' : 'dev'));
|
app.use(morgan(process.env.production ? 'common' : 'dev'));
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|||||||
73
src/cdn.js
73
src/cdn.js
@@ -1,5 +1,5 @@
|
|||||||
import { rmSync } from 'fs';
|
import { rmSync } from 'fs';
|
||||||
import { copyFile, mkdir } from 'fs/promises';
|
import { copyFile, mkdir, readdir } from 'fs/promises';
|
||||||
import { Collection, Db, ObjectId } from 'mongodb';
|
import { Collection, Db, ObjectId } from 'mongodb';
|
||||||
import multer from 'multer';
|
import multer from 'multer';
|
||||||
|
|
||||||
@@ -11,6 +11,10 @@ const upload = multer({ dest: dataPath });
|
|||||||
*/
|
*/
|
||||||
let ressources;
|
let ressources;
|
||||||
|
|
||||||
|
function buildPath(userid, path) {
|
||||||
|
return `${dataPath}/${userid}${path}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {*} app
|
* @param {*} app
|
||||||
@@ -20,6 +24,8 @@ export function initCdn(app, db) {
|
|||||||
ressources = db.collection('ressources');
|
ressources = db.collection('ressources');
|
||||||
app.post('/upload', upload.single('file'), uploadFile);
|
app.post('/upload', upload.single('file'), uploadFile);
|
||||||
app.get('/cdn/:userid/:resid', fetchFile);
|
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;
|
const path = req.body.path;
|
||||||
|
|
||||||
if (path !== undefined && path.startsWith('/') && !path.includes('.')) {
|
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, {
|
mkdir(destinationPath, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
})
|
})
|
||||||
@@ -89,3 +95,66 @@ async function fetchFile(req, res) {
|
|||||||
res.sendStatus(404);
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
13
src/db.js
13
src/db.js
@@ -1,5 +1,8 @@
|
|||||||
import { MongoClient } from "mongodb";
|
import { MongoClient } from 'mongodb';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {MongoClient}
|
||||||
|
*/
|
||||||
let client;
|
let client;
|
||||||
|
|
||||||
const dbName = `jeopardy`;
|
const dbName = `jeopardy`;
|
||||||
@@ -10,12 +13,10 @@ const dbName = `jeopardy`;
|
|||||||
export let db;
|
export let db;
|
||||||
|
|
||||||
export async function initDbConnection() {
|
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();
|
await client.connect();
|
||||||
console.log('Connected successfully to mongodb');
|
console.log('Connected successfully to mongodb');
|
||||||
db = client.db(dbName);
|
db = client.db(dbName);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function close() {
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user