143 lines
4.8 KiB
JavaScript
143 lines
4.8 KiB
JavaScript
const Gallery = require('../models/Gallery')
|
|
const sharp = require('sharp')
|
|
const fs = require('fs')
|
|
const {body, validationResult} = require('express-validator')
|
|
const {_sr} = require('../plugins/serverResponses')
|
|
const {res404, res500} = require('../plugins/controllersHelperFunctions')
|
|
|
|
|
|
///// variables
|
|
const thumbWidth = 420
|
|
const thumbHeight = 280
|
|
const thumbQuality = 80
|
|
|
|
const maxCaptionLength = 100
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////// create image
|
|
module.exports.create = [
|
|
[
|
|
body('caption')
|
|
.exists().withMessage(_sr['fa'].required.caption)
|
|
],
|
|
async (req, res) => {
|
|
////////////// check validation result
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
|
|
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}}})
|
|
|
|
|
|
let image = req.files.image
|
|
let imageName = 'gallery_' + Date.now() + '.' + image.mimetype.split('/')[1]
|
|
|
|
if (image) {
|
|
//////// validate image format
|
|
try {
|
|
const target = sharp(image.data)
|
|
await target.toFile(`./static/uploads/images/gallery/${imageName}`)
|
|
await target
|
|
.resize(thumbWidth, thumbHeight, {fit: 'cover'})
|
|
.jpeg({quality: thumbQuality})
|
|
.toFile(`./static/uploads/images/gallery/thumb/thumb_${imageName}`)
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
}
|
|
////////////// pass values throw model
|
|
let gallery = new Gallery({
|
|
image: imageName,
|
|
caption: req.body.caption,
|
|
shortCaption: req.body.caption.slice(0, maxCaptionLength)
|
|
})
|
|
gallery.save((err, data) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json({message: _sr['fa'].response.success_save})
|
|
|
|
})
|
|
}
|
|
]
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////// get all images
|
|
module.exports.getAll = [
|
|
(req, res) => {
|
|
Gallery.find({}, (err, images) => {
|
|
if (err) return res500(res, err)
|
|
return res.json(images)
|
|
})
|
|
}
|
|
]
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////// get one image
|
|
module.exports.getOne = [
|
|
(req, res) => {
|
|
Gallery.findById(req.params.id, (err, image) => {
|
|
if (err || !image) return res404(res, err)
|
|
return res.json(image)
|
|
})
|
|
}
|
|
]
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////// update image
|
|
module.exports.update = [
|
|
[
|
|
body('caption')
|
|
.exists().withMessage(_sr['fa'].required.caption)
|
|
],
|
|
async (req, res) => {
|
|
////////////// check validation result
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
|
|
|
|
let data = {}
|
|
data.caption = req.body.caption.slice(0, maxCaptionLength) + ' ... '
|
|
/////
|
|
if (req.files?.image) {
|
|
let image = req.files.image
|
|
let imageName = 'gallery_' + Date.now() + '.' + image.mimetype.split('/')[1]
|
|
|
|
//////// validate image format
|
|
if (!_sr.supportedImageFormats.includes(image.mimetype.split('/')[1])) return res.status(422).json({validation: {image: {msg: _sr['fa'].format.image}}})
|
|
data.image = imageName
|
|
try {
|
|
const target = sharp(image.data)
|
|
await target.toFile(`./static/uploads/images/gallery/${imageName}`)
|
|
await target
|
|
.resize(thumbWidth, thumbHeight, {fit: 'cover'})
|
|
.jpeg({quality: thumbQuality})
|
|
.toFile(`./static/uploads/images/gallery/thumb/thumb_${imageName}`)
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
}
|
|
////////////// pass values throw model
|
|
Gallery.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
if (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})
|
|
})
|
|
}
|
|
]
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////// delete image
|
|
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})
|
|
})
|
|
}
|
|
]
|