S3 for other controolers
This commit is contained in:
@@ -10,8 +10,11 @@ const {
|
||||
hasWhiteSpaces,
|
||||
isLatinCharactersWithSymbol
|
||||
} = require('../plugins/controllersHelperFunctions');
|
||||
const fs = require('fs');
|
||||
const sharp = require('sharp');
|
||||
const {
|
||||
deleteCandidateImage,
|
||||
deleteElectionCandidateImages,
|
||||
uploadProcessedImage,
|
||||
} = require('../plugins/electionImageStorage');
|
||||
|
||||
////// variables
|
||||
const profileWidth = 300;
|
||||
@@ -292,17 +295,7 @@ module.exports.deletOneElection = [
|
||||
}
|
||||
|
||||
if (deleteElection.candidates.length) {
|
||||
const targetDir = `./static/uploads/images/election/`;
|
||||
|
||||
const getFiles = fs.readdirSync(targetDir);
|
||||
|
||||
for (const file of getFiles) {
|
||||
if (file.includes(deleteElection._id)) {
|
||||
fs.unlink(`./static/uploads/images/election/${file}`, (err) => {
|
||||
if (err) console.log(err)
|
||||
});
|
||||
}
|
||||
}
|
||||
await deleteElectionCandidateImages(deleteElection);
|
||||
}
|
||||
|
||||
await deleteElection.deleteOne();
|
||||
@@ -457,16 +450,13 @@ module.exports.addCandidate = [
|
||||
}
|
||||
const imageName = 'candidate_' + Date.now() + '_' + targetElection._id + '.jpg';
|
||||
data.profilePic = imageName;
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(profileWidth, profileHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.toFormat('jpg')
|
||||
.jpeg({
|
||||
quality: profileQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/election/${imageName}`)
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
imageName,
|
||||
profileWidth,
|
||||
profileHeight,
|
||||
profileQuality
|
||||
);
|
||||
|
||||
|
||||
targetElection.candidates.push(data);
|
||||
@@ -528,29 +518,16 @@ module.exports.updateCandidate = [
|
||||
return res.status(422).json({validation: {profilePic: {msg: _sr['fa'].format.image}}});
|
||||
}
|
||||
const imageName = 'candidate_' + Date.now() + '_' + targetElection._id + '.jpg';
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(profileWidth, profileHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.toFormat('jpg')
|
||||
.jpeg({
|
||||
quality: profileQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/election/${imageName}`)
|
||||
await deleteCandidateImage(candidate);
|
||||
|
||||
/**
|
||||
* if exist old pic delete it from file
|
||||
*/
|
||||
if (candidate.profilePic) {
|
||||
const oldPic = `./static${candidate.profilePic}`;
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
imageName,
|
||||
profileWidth,
|
||||
profileHeight,
|
||||
profileQuality
|
||||
);
|
||||
|
||||
if (fs.existsSync(oldPic)) {
|
||||
fs.unlink(oldPic, (err) => {
|
||||
if (err) return res500(res, err?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
candidate.profilePic = imageName;
|
||||
}
|
||||
|
||||
@@ -586,15 +563,7 @@ module.exports.deleteCandidate = [
|
||||
|
||||
if (!candidate) return res404(res, `کاندید مورد نظر پیدا نشد`);
|
||||
|
||||
if (candidate.profilePic) {
|
||||
const oldPic = `./static${candidate.profilePic}`;
|
||||
|
||||
if (fs.existsSync(oldPic)) {
|
||||
fs.unlink(oldPic, (err) => {
|
||||
if (err) return res500(res, err?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
await deleteCandidateImage(candidate);
|
||||
|
||||
await targetElection.candidates.id(candidateId).remove();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user