Added deletion and renaming of files and deletion of directories
This commit is contained in:
146
src/cdn.js
146
src/cdn.js
@@ -1,5 +1,5 @@
|
||||
import { rmSync } from 'fs';
|
||||
import { copyFile, mkdir, readdir } from 'fs/promises';
|
||||
import { copyFile, mkdir, readdir, rm } from 'fs/promises';
|
||||
import { Collection, Db, ObjectId } from 'mongodb';
|
||||
import multer from 'multer';
|
||||
|
||||
@@ -12,7 +12,11 @@ const upload = multer({ dest: dataPath });
|
||||
let ressources;
|
||||
|
||||
function buildPath(userid, path) {
|
||||
return `${dataPath}/${userid}${path}`;
|
||||
return `${dataPath}/${userid}${path.length === '/' ? '' : path}`;
|
||||
}
|
||||
|
||||
function ressourcePath(res) {
|
||||
return `${res.fullpath}/${res.filename}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,8 +28,11 @@ export function initCdn(app, db) {
|
||||
ressources = db.collection('ressources');
|
||||
app.post('/upload', upload.single('file'), uploadFile);
|
||||
app.get('/cdn/:userid/:resid', fetchFile);
|
||||
app.put('/cdn/:userid/:resid', renameFile);
|
||||
app.delete('/cdn/:userid/:resid', deleteFile);
|
||||
app.post('/directory', fetchDirectory);
|
||||
app.put('/directory', addDirectory);
|
||||
app.delete('/directory', deleteDirectory);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,12 +97,99 @@ async function fetchFile(req, res) {
|
||||
});
|
||||
|
||||
if (ressource) {
|
||||
res.sendFile(ressource.fullpath + ressource.filename);
|
||||
res.sendFile(ressourcePath(ressource));
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function renameFile(req, res) {
|
||||
if (req.user._id.toString() !== req.params.userid) {
|
||||
res.sendStatus(403);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
req.body === undefined ||
|
||||
req.body.name === undefined ||
|
||||
req.body.name.length <= 0 ||
|
||||
/\.{2}|\/|\\/.test(req.body.name)
|
||||
) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
const name = req.body.name;
|
||||
|
||||
let ressource = await ressources.findOne({
|
||||
user: new ObjectId(req.params.userid),
|
||||
filename: req.params.resid,
|
||||
});
|
||||
|
||||
if (!ressource) {
|
||||
console.log('Ressource could not be found');
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
ressources
|
||||
.updateOne(
|
||||
{
|
||||
_id: ressource._id,
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
name,
|
||||
},
|
||||
},
|
||||
)
|
||||
.then(() => {
|
||||
res.sendStatus(200);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function deleteFile(req, res) {
|
||||
if (req.user._id.toString() !== req.params.userid) {
|
||||
res.sendStatus(403);
|
||||
return;
|
||||
}
|
||||
let ressource = await ressources.findOne({
|
||||
user: new ObjectId(req.params.userid),
|
||||
filename: req.params.resid,
|
||||
});
|
||||
|
||||
if (!ressource) {
|
||||
console.log('Ressource could not be found');
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
rm(ressourcePath(ressource))
|
||||
.then(() => {
|
||||
return ressources.deleteOne({ _id: ressource._id });
|
||||
})
|
||||
.then(() => {
|
||||
res.sendStatus(200);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
@@ -137,6 +231,11 @@ async function fetchDirectory(req, res) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function addDirectory(req, res) {
|
||||
if (!req.body) {
|
||||
res.sendStatus(400);
|
||||
@@ -158,3 +257,44 @@ async function addDirectory(req, res) {
|
||||
res.sendStatus(500);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function deleteDirectory(req, res) {
|
||||
console.log(req.body);
|
||||
if (
|
||||
req.body === undefined ||
|
||||
req.body.path === undefined ||
|
||||
req.body.path.length <= 0
|
||||
) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(req.body);
|
||||
|
||||
const path = req.body.path;
|
||||
|
||||
rm(buildPath(req.user._id, path), {
|
||||
force: true,
|
||||
recursive: true,
|
||||
})
|
||||
.then(() => {
|
||||
return ressources.deleteMany({
|
||||
user: req.user._id,
|
||||
path: {
|
||||
$regex: '^' + path + '($|\\/.*)',
|
||||
},
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
res.sendStatus(200);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user