Added creation and fetching of directories and files
This commit is contained in:
73
src/cdn.js
73
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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user