backend: PUT /users - Supprime les anciens logo
This commit is contained in:
parent
5bf9a2ade6
commit
3ed605af1b
20
api/assets/utils/deleteFilesNameStartWith.js
Normal file
20
api/assets/utils/deleteFilesNameStartWith.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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
|
||||||
|
for (const name of fileNames) {
|
||||||
|
// If file name matches the pattern
|
||||||
|
if (name.startsWith(pattern)) {
|
||||||
|
console.log(name)
|
||||||
|
fs.unlinkSync(path.join(dirPath, name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = deleteFilesNameStartWith;
|
@ -14,6 +14,7 @@ const Favorites = require('../models/favorites');
|
|||||||
const Functions = require('../models/functions');
|
const Functions = require('../models/functions');
|
||||||
const Categories = require('../models/categories');
|
const Categories = require('../models/categories');
|
||||||
const Comments = require('../models/comments');
|
const Comments = require('../models/comments');
|
||||||
|
const deleteFilesNameStartWith = require('../assets/utils/deleteFilesNameStartWith');
|
||||||
|
|
||||||
async function handleEditUser(res, { name, email, biography, isPublicEmail }, userId, logoName) {
|
async function handleEditUser(res, { name, email, biography, isPublicEmail }, userId, logoName) {
|
||||||
const user = await Users.findOne({ where: { id: userId } });
|
const user = await Users.findOne({ where: { id: userId } });
|
||||||
@ -44,7 +45,14 @@ exports.putUser = (req, res, next) => {
|
|||||||
)) {
|
)) {
|
||||||
return errorHandling(next, { message:"Le profil doit avoir une image valide.", statusCode: 400 });
|
return errorHandling(next, { message:"Le profil doit avoir une image valide.", statusCode: 400 });
|
||||||
}
|
}
|
||||||
const logoName = name + uuid.v4() + logo.name;
|
const logoName = name + req.userId + uuid.v4() + logo.name;
|
||||||
|
// Supprime les anciens logo
|
||||||
|
try {
|
||||||
|
deleteFilesNameStartWith(`${name + req.userId}`, path.join(__dirname, '..', 'assets', 'images', 'users'));
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return errorHandling(next, serverError);
|
||||||
|
}
|
||||||
logo.mv(path.join(__dirname, '..', 'assets', 'images', 'users') + '/' + logoName, async (error) => {
|
logo.mv(path.join(__dirname, '..', 'assets', 'images', 'users') + '/' + logoName, async (error) => {
|
||||||
if (error) return errorHandling(next, serverError);
|
if (error) return errorHandling(next, serverError);
|
||||||
try {
|
try {
|
||||||
|
Reference in New Issue
Block a user