191 lines
5.6 KiB
JavaScript
191 lines
5.6 KiB
JavaScript
const Gallery = require('../models/Gallery')
|
|
const mongoose = require('mongoose')
|
|
const sharp = require('sharp')
|
|
const fs = require('fs')
|
|
const {body, validationResult} = require('express-validator')
|
|
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
|
const {_sr} = require('../plugins/serverResponses')
|
|
|
|
// variables
|
|
const imageQuality = 100
|
|
|
|
const imageThumbWidth = 500
|
|
const imageThumbHeight = 500
|
|
const imageThumbQuality = 80
|
|
|
|
module.exports.create = [
|
|
[
|
|
body('category')
|
|
.if((value, {req}) => req.body.noCategory === 'false')
|
|
.notEmpty().withMessage(_sr['fa'].required.category)
|
|
.bail()
|
|
.custom((value, {req}) => {
|
|
return mongoose.isValidObjectId(value) ? true : Promise.reject(_sr['fa'].required.category)
|
|
})
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
try {
|
|
if (!req.files || !req.files.image) return res.status(422).json({validation: {image: {msg: _sr['fa'].required.image}}})
|
|
if (!_sr.supportedImageFormats.includes(req.files.image.mimetype.split('/')[1])) return res.status(422).json({validation: {image: {msg: _sr['fa'].format.image}}})
|
|
|
|
const {fa_caption, en_caption, category, noCategory} = req.body
|
|
|
|
const data = {
|
|
locale: {
|
|
fa: {
|
|
caption: fa_caption
|
|
},
|
|
en: {
|
|
caption: en_caption
|
|
}
|
|
},
|
|
noCategory
|
|
}
|
|
if (mongoose.isValidObjectId(category)) data.category = category
|
|
const image = req.files.image
|
|
const imageName = 'gallery_' + Date.now() + '.jpg'
|
|
const imageThumb = 'thumb_' + imageName
|
|
|
|
try {
|
|
const target = await sharp(image.data)
|
|
await target
|
|
.toFormat('jpg')
|
|
.jpeg({quality: imageQuality})
|
|
.toFile(`./static/uploads/images/gallery/${imageName}`)
|
|
|
|
await target
|
|
.resize(imageThumbWidth, imageThumbHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.toFormat('jpg')
|
|
.jpeg({quality: imageThumbQuality})
|
|
.toFile(`./static/uploads/images/gallery/${imageThumb}`)
|
|
|
|
data.image = imageName
|
|
data.thumb = imageThumb
|
|
} catch (e) {
|
|
return res500(res , _sr['fa'].response.problem)
|
|
}
|
|
|
|
const post = new Gallery(data)
|
|
post.save((err, data) => {
|
|
if (err) return res500(res , _sr['fa'].response.problem)
|
|
return res.json(data)
|
|
})
|
|
}catch (e) {
|
|
return res500(res , _sr['fa'].response.problem)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getAll = [
|
|
(req, res) => {
|
|
Gallery.find({}).populate('category').sort({_id: -1}).exec((err, images) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(images)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getByCategoryId = [
|
|
(req, res) => {
|
|
Gallery.find({category: req.params.id}).sort({_id: -1}).exec((err, images) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(images)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getOne = [
|
|
(req, res) => {
|
|
Gallery.findById(req.params.id, (err, image) => {
|
|
if (err || !image) return res404(res, err)
|
|
else return res.json(image)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.update = [
|
|
[
|
|
body('category')
|
|
.if((value, {req}) => req.body.noCategory === 'false')
|
|
.notEmpty().withMessage(_sr['fa'].required.category)
|
|
.bail()
|
|
.custom((value, {req}) => {
|
|
return mongoose.isValidObjectId(value) ? true : Promise.reject(_sr['fa'].required.category)
|
|
})
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
const {fa_caption, en_caption, category, noCategory} = req.body
|
|
|
|
const data = {
|
|
locale: {
|
|
fa: {
|
|
caption: fa_caption
|
|
},
|
|
en: {
|
|
caption: en_caption
|
|
}
|
|
},
|
|
noCategory
|
|
}
|
|
if (mongoose.isValidObjectId(category)) data.category = category
|
|
if (req.files && req.files.image) {
|
|
if (!_sr.supportedImageFormats.includes(req.files.image.mimetype.split('/')[1])) return res.status(422).json({validation: {image: {msg: _sr['fa'].format.image}}})
|
|
|
|
const image = req.files.image
|
|
const imageName = 'gallery_' + Date.now() + '.jpg'
|
|
const imageThumb = 'thumb_' + imageName
|
|
|
|
try {
|
|
const target = await sharp(image.data)
|
|
await target
|
|
.toFormat('jpg')
|
|
.jpeg({quality: imageQuality})
|
|
.toFile(`./static/uploads/images/gallery/${imageName}`)
|
|
|
|
await target
|
|
.resize(imageThumbWidth, imageThumbHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.toFormat('jpg')
|
|
.jpeg({quality: imageThumbQuality})
|
|
.toFile(`./static/uploads/images/gallery/${imageThumb}`)
|
|
|
|
data.image = imageName
|
|
data.thumb = imageThumb
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
}
|
|
|
|
Gallery.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
if (req.files && req.files.image) {
|
|
fs.unlink(`./static/${oldData.image}`, (err) => {
|
|
if (err) console.log(err)
|
|
})
|
|
fs.unlink(`./static/${oldData.thumb}`, (err) => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.delete = [
|
|
(req, res) => {
|
|
Gallery.findByIdAndRemove(req.params.id, (err, data) => {
|
|
if (err || !data) return res404(res, err)
|
|
fs.unlink(`./static/${data.image}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
fs.unlink(`./static/${data.thumb}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
return res.json({message: _sr['fa'].response.success_remove})
|
|
})
|
|
}
|
|
]
|