S3 for other controolers
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
const {body, validationResult} = require('express-validator');
|
||||
const sharp = require('sharp');
|
||||
const {_sr} = require('../plugins/serverResponses');
|
||||
const {res404, res500, hasWhiteSpaces, isLatinCharactersWithSymbol, generateRandomDigits} = require('../plugins/controllersHelperFunctions');
|
||||
const Content = require('../models/Content');
|
||||
const fs = require('fs');
|
||||
const {
|
||||
deleteContentImages,
|
||||
uploadProcessedImage,
|
||||
} = require('../plugins/contentImageStorage');
|
||||
|
||||
|
||||
////// variables
|
||||
@@ -85,26 +87,23 @@ module.exports.addContent = [
|
||||
const imageName = `manager_${Date.now()}_${create._id}.${image.mimetype.split('/')[1]}`;
|
||||
const thumbName = 'thumb_' + imageName
|
||||
try {
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(profileWidth, profileHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.jpeg({
|
||||
quality: profileQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/content/${imageName}`)
|
||||
await target
|
||||
.resize(profileThumbWidth, profileThumbHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.jpeg({
|
||||
quality: profileThumbQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/content/${thumbName}`)
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
imageName,
|
||||
profileWidth,
|
||||
profileHeight,
|
||||
profileQuality
|
||||
);
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
thumbName,
|
||||
profileThumbWidth,
|
||||
profileThumbHeight,
|
||||
profileThumbQuality
|
||||
);
|
||||
|
||||
create.cover = imageName;
|
||||
create.thumb = imageName;
|
||||
create.thumb = thumbName;
|
||||
|
||||
await create.save();
|
||||
} catch (e) {
|
||||
@@ -180,42 +179,22 @@ module.exports.updateContent = [
|
||||
const imageName = `manager_${Date.now()}_${targetContent._id}.${image.mimetype.split('/')[1]}`;
|
||||
const thumbName = 'thumb_' + imageName
|
||||
try {
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(profileWidth, profileHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.jpeg({
|
||||
quality: profileQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/content/${imageName}`)
|
||||
await target
|
||||
.resize(profileThumbWidth, profileThumbHeight, {
|
||||
fit: 'cover'
|
||||
})
|
||||
.jpeg({
|
||||
quality: profileThumbQuality
|
||||
})
|
||||
.toFile(`./static/uploads/images/content/${thumbName}`)
|
||||
await deleteContentImages(targetContent);
|
||||
|
||||
/**
|
||||
* if exist old pic delete it from file
|
||||
*/
|
||||
if (targetContent.cover) {
|
||||
const oldPic = `./static${targetContent.cover}`
|
||||
const oldThumb = `./static${targetContent.thumb}`
|
||||
|
||||
if (fs.existsSync(oldPic)) {
|
||||
fs.unlink(oldPic, (err) => {
|
||||
if (err) return res500(res, err?.message);
|
||||
});
|
||||
}
|
||||
if (fs.existsSync(oldThumb)) {
|
||||
fs.unlink(oldThumb, (err) => {
|
||||
if (err) return res500(res, err?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
imageName,
|
||||
profileWidth,
|
||||
profileHeight,
|
||||
profileQuality
|
||||
);
|
||||
await uploadProcessedImage(
|
||||
image.data,
|
||||
thumbName,
|
||||
profileThumbWidth,
|
||||
profileThumbHeight,
|
||||
profileThumbQuality
|
||||
);
|
||||
|
||||
targetContent.cover = imageName;
|
||||
targetContent.thumb = thumbName;
|
||||
|
||||
Reference in New Issue
Block a user