S3 for other controolers
This commit is contained in:
@@ -3,7 +3,6 @@ const {
|
||||
body,
|
||||
validationResult
|
||||
} = require('express-validator');
|
||||
const sharp = require('sharp');
|
||||
const {
|
||||
_sr
|
||||
} = require('../plugins/serverResponses');
|
||||
@@ -14,7 +13,10 @@ const {
|
||||
isLatinCharactersWithSymbol,
|
||||
generateRandomDigits
|
||||
} = require('../plugins/controllersHelperFunctions');
|
||||
const fs = require('fs');
|
||||
const {
|
||||
deleteLinksImage,
|
||||
uploadProcessedImage,
|
||||
} = require('../plugins/linksImageStorage');
|
||||
|
||||
////// variables
|
||||
const profileWidth = 50;
|
||||
@@ -108,15 +110,13 @@ module.exports.addLinks = [
|
||||
}
|
||||
const imageName = `links_${Date.now()}_${createLinks._id}.${image.mimetype.split('/')[1]}`;
|
||||
try {
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(profileWidth, profileHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.jpeg({
|
||||
quality: profileQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/links/${imageName}`);
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
imageName,
|
||||
profileWidth,
|
||||
profileHeight,
|
||||
profileQuality
|
||||
);
|
||||
|
||||
createLinks.icon = imageName;
|
||||
|
||||
@@ -243,28 +243,15 @@ module.exports.updateLinks = [
|
||||
}
|
||||
const imageName = `links_${Date.now()}_${targetLinks._id}.${image.mimetype.split('/')[1]}`;
|
||||
try {
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(profileWidth, profileHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.jpeg({
|
||||
quality: profileQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/links/${imageName}`);
|
||||
await deleteLinksImage(targetLinks, 'icon');
|
||||
|
||||
/**
|
||||
* if exist old pic delete it from file
|
||||
*/
|
||||
if (targetLinks.icon) {
|
||||
var oldPic = `./static${targetLinks.icon}`;
|
||||
|
||||
if (fs.existsSync(oldPic)) {
|
||||
fs.unlink(oldPic, (err) => {
|
||||
if (err) return res500(res, err?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
imageName,
|
||||
profileWidth,
|
||||
profileHeight,
|
||||
profileQuality
|
||||
);
|
||||
|
||||
targetLinks.icon = imageName;
|
||||
targetLinks.linksStatus = true;
|
||||
@@ -312,15 +299,7 @@ module.exports.deletOneLinks = [
|
||||
return res500(res, `شما اجازه حذف این پیوند را ندارید`);
|
||||
}
|
||||
|
||||
if (targetLinks.icon) {
|
||||
var oldPic = `./static${targetLinks.icon}`;
|
||||
|
||||
if (fs.existsSync(oldPic)) {
|
||||
fs.unlink(oldPic, (error) => {
|
||||
if (error) return res500(res, error?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
await deleteLinksImage(targetLinks, 'icon');
|
||||
|
||||
/** delete links */
|
||||
await targetLinks.deleteOne();
|
||||
|
||||
Reference in New Issue
Block a user