👌 IMPROVE: backend - Hotfix suppression fichiers
This commit is contained in:
parent
3d0488a605
commit
43d2180209
@ -1,19 +1,18 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function deleteFilesNameStartWith(pattern, dirPath = __dirname) {
|
||||
// Get all file names in directory
|
||||
fs.readdir(path.resolve(dirPath), (err, fileNames) => {
|
||||
if (err) throw err;
|
||||
console.log(dirPath);
|
||||
// Iterate through the found file names
|
||||
function deleteFilesNameStartWith(pattern, dirPath, callback) {
|
||||
fs.readdir(path.resolve(dirPath), (_error, fileNames) => {
|
||||
for (const name of fileNames) {
|
||||
// If file name matches the pattern
|
||||
if (name.startsWith(pattern) && name !== 'default.png') {
|
||||
console.log(name)
|
||||
fs.unlinkSync(path.join(dirPath, name));
|
||||
const splitedName = name.split('.');
|
||||
if (splitedName.length === 2) {
|
||||
const fileName = splitedName[0];
|
||||
if (fileName === pattern && name !== 'default.png') {
|
||||
return fs.unlink(path.join(dirPath, name), callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
return callback();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -126,10 +126,12 @@ exports.putFunction = async (req, res, next) => {
|
||||
if (splitedImageName.length !== 2) return errorHandling(next, serverError);
|
||||
const imageName = slug + '.' + splitedImageName[1];
|
||||
// Supprime les anciennes images
|
||||
deleteFilesNameStartWith(slug, path.join(__dirname, '..', 'assets', 'images', 'functions'));
|
||||
image.mv(path.join(__dirname, '..', 'assets', 'images', 'functions', imageName), async (error) => {
|
||||
if (error) return errorHandling(next, serverError);
|
||||
return await handleEditFunction(res, resultFunction, { title, slug, description, type, categorieId }, imageName);
|
||||
const functionPath = path.join(__dirname, '..', 'assets', 'images', 'functions');
|
||||
deleteFilesNameStartWith(slug, functionPath, () => {
|
||||
image.mv(path.join(functionPath, imageName), async (error) => {
|
||||
if (error) return errorHandling(next, serverError);
|
||||
return await handleEditFunction(res, resultFunction, { title, slug, description, type, categorieId }, imageName);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return await handleEditFunction(res, resultFunction, { title, slug, description, type, categorieId });
|
||||
|
@ -35,14 +35,14 @@ async function handleEditUser(res, { name, email, biography, isPublicEmail }, us
|
||||
user.biography = biography;
|
||||
}
|
||||
user.isPublicEmail = isPublicEmail;
|
||||
if (logoName != undefined) {
|
||||
if (logoName != undefined && `/images/users/${logoName}` !== user.logo) {
|
||||
user.logo = `/images/users/${logoName}`;
|
||||
}
|
||||
await user.save();
|
||||
return res.status(200).json({ id: user.id, name: user.name, email: user.email, biography: user.biography, logo: user.logo, isPublicEmail: user.isPublicEmail, isAdmin: user.isAdmin, createdAt: user.createdAt });
|
||||
}
|
||||
|
||||
exports.putUser = (req, res, next) => {
|
||||
exports.putUser = async (req, res, next) => {
|
||||
const { name, email, biography, isPublicEmail } = req.body;
|
||||
const logo = req.files.logo;
|
||||
const errors = validationResult(req);
|
||||
@ -58,26 +58,24 @@ exports.putUser = (req, res, next) => {
|
||||
)) {
|
||||
return errorHandling(next, { message:"Le profil doit avoir une image valide (PNG, JPG, GIF) et moins de 5mo.", statusCode: 400 });
|
||||
}
|
||||
const logoName = name + req.userId + uuid.v4() + logo.name;
|
||||
const splitedLogoName = logo.name.split('.');
|
||||
if (splitedLogoName.length !== 2) return errorHandling(next, serverError);
|
||||
const logoName = name + req.userId + '.' + splitedLogoName[1];
|
||||
// Supprime les anciens logo
|
||||
try {
|
||||
deleteFilesNameStartWith(`${name + req.userId}`, path.join(__dirname, '..', 'assets', 'images', 'users'));
|
||||
deleteFilesNameStartWith(`${name + req.userId}`, path.join(__dirname, '..', 'assets', 'images', 'users'), async () => {
|
||||
logo.mv(path.join(__dirname, '..', 'assets', 'images', 'users', logoName), async (error) => {
|
||||
if (error) return errorHandling(next, serverError);
|
||||
return await handleEditUser(res, { name, email, biography, isPublicEmail }, req.userId, logoName);
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return errorHandling(next, serverError);
|
||||
}
|
||||
logo.mv(path.join(__dirname, '..', 'assets', 'images', 'users') + '/' + logoName, async (error) => {
|
||||
if (error) return errorHandling(next, serverError);
|
||||
try {
|
||||
return handleEditUser(res, { name, email, biography, isPublicEmail }, req.userId, logoName);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return errorHandling(next, serverError);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
return handleEditUser(res, { name, email, biography, isPublicEmail }, req.userId, null);
|
||||
return await handleEditUser(res, { name, email, biography, isPublicEmail }, req.userId, null);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return errorHandling(next, serverError);
|
||||
|
@ -97,7 +97,7 @@ AdminRouter.route('/functions')
|
||||
|
||||
AdminRouter.route('/functions/:id')
|
||||
|
||||
// Modifier information basique d'une fonction
|
||||
// Modifie information basique d'une fonction
|
||||
.put(isAuth, isAdmin,
|
||||
fileUpload({
|
||||
useTempFiles: true,
|
||||
@ -180,7 +180,7 @@ AdminRouter.route('/categories/:id')
|
||||
// Modifier une catégorie avec son id
|
||||
.put(isAuth, isAdmin, adminController.putCategory)
|
||||
|
||||
// Supprime une fonction avec son id
|
||||
// Supprime une catégorie avec son id
|
||||
.delete(isAuth, isAdmin, adminController.deleteCategory);
|
||||
|
||||
module.exports = AdminRouter;
|
Reference in New Issue
Block a user