282 lines
9.7 KiB
JavaScript
282 lines
9.7 KiB
JavaScript
const Slide = require('../models/Slide')
|
|
const {body, validationResult} = require('express-validator')
|
|
const {_sr} = require('../plugins/serverResponses')
|
|
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
|
const sharp = require('sharp')
|
|
const mongoose = require('mongoose')
|
|
const fs = require('fs')
|
|
|
|
// variables
|
|
const coverWidth = 1920
|
|
const coverHeight = 1080
|
|
const coverQuality = 100
|
|
|
|
module.exports.create = [
|
|
async (req, res) => {
|
|
try {
|
|
const isImage = req.body.isImage === 'true'
|
|
if (isImage && (!req.files || !req.files.image)) return res.status(422).json({validation: {image: {msg: _sr['fa'].required.image}}})
|
|
if (!isImage && (!req.files || !req.files.video)) return res.status(422).json({validation: {video: {msg: _sr['fa'].required.video}}})
|
|
if (isImage && !_sr.supportedImageFormats.includes(req.files.image.mimetype.split('/')[1])) return res.status(422).json({validation: {image: {msg: _sr['fa'].format.image}}})
|
|
if (!isImage && !_sr.supportedVideoFormats.includes(req.files.video.mimetype.split('/')[1])) return res.status(422).json({validation: {video: {msg: _sr['fa'].format.video}}})
|
|
const data = {isImage}
|
|
|
|
if (isImage) {
|
|
const image = req.files.image
|
|
const imageName = 'slider_' + Date.now() + '.jpg'
|
|
data.image = imageName
|
|
const target = await sharp(image.data)
|
|
await target
|
|
.resize(coverWidth, coverHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.toFormat('jpg')
|
|
.jpeg({quality: coverQuality})
|
|
.toFile(`./static/uploads/images/slider/${imageName}`)
|
|
} else {
|
|
const video = req.files.video
|
|
const videoName = 'slider_' + Date.now() + '.' + video.mimetype.split('/')[1]
|
|
data.video = videoName
|
|
video.mv(`./static/uploads/images/slider/${videoName}`)
|
|
}
|
|
|
|
const slide = new Slide(data)
|
|
slide.save(err => {
|
|
if (err) return res500(res, err)
|
|
else return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getAll = [
|
|
(req, res) => {
|
|
Slide.find({}).exec((err, projects) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(projects)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getOne = [
|
|
(req, res) => {
|
|
Slide.findById(req.params.id, (err, slide) => {
|
|
if (err || !slide) return res404(res, err)
|
|
else return res.json(slide)
|
|
})
|
|
}
|
|
]
|
|
|
|
// module.exports.update = [
|
|
// (req, res) => {
|
|
// const {
|
|
// fa_title,
|
|
// en_title,
|
|
// fa_short_description,
|
|
// en_short_description,
|
|
// fa_description,
|
|
// en_description,
|
|
// //
|
|
// fa_client,
|
|
// en_client,
|
|
// //
|
|
// fa_skills,
|
|
// en_skills,
|
|
// //
|
|
// fa_website,
|
|
// en_website,
|
|
// //
|
|
// fa_s3_title,
|
|
// en_s3_title,
|
|
// fa_s3_description,
|
|
// en_s3_description,
|
|
// //
|
|
// fa_s5_title,
|
|
// en_s5_title,
|
|
// fa_s5_description,
|
|
// en_s5_description,
|
|
// //
|
|
// fa_s6_title,
|
|
// en_s6_title,
|
|
// fa_s6_description,
|
|
// en_s6_description,
|
|
// category,
|
|
// project_date,
|
|
// has_details
|
|
// } = req.body
|
|
//
|
|
// const data = {
|
|
// locale: {
|
|
// fa: {
|
|
// title: fa_title,
|
|
// short_description: fa_short_description.slice(0, shortDescriptionMaxLength) + ' ... ',
|
|
// description: fa_description,
|
|
// //
|
|
// client: fa_client,
|
|
// skills: fa_skills,
|
|
// website: fa_website,
|
|
// //
|
|
// s3_title: fa_s3_title,
|
|
// s3_description: fa_s3_description,
|
|
// //
|
|
// s5_title: fa_s5_title,
|
|
// s5_description: fa_s5_description,
|
|
// //
|
|
// s6_title: fa_s6_title,
|
|
// s6_description: fa_s6_description
|
|
// },
|
|
// en: {
|
|
// title: en_title,
|
|
// short_description: en_short_description.slice(0, shortDescriptionMaxLength) + ' ... ',
|
|
// description: en_description,
|
|
// //
|
|
// client: en_client,
|
|
// skills: en_skills,
|
|
// website: en_website,
|
|
// //
|
|
// s3_title: en_s3_title,
|
|
// s3_description: en_s3_description,
|
|
// //
|
|
// s5_title: en_s5_title,
|
|
// s5_description: en_s5_description,
|
|
// //
|
|
// s6_title: en_s6_title,
|
|
// s6_description: en_s6_description
|
|
// }
|
|
// },
|
|
// category,
|
|
// project_date,
|
|
// has_details
|
|
// }
|
|
//
|
|
// if (req.files) {
|
|
// if (req.files.cover) {
|
|
// const cover = req.files.cover
|
|
// const coverName = 'project_' + Date.now() + '.jpg'
|
|
// if (!_sr.supportedImageFormats.includes(cover.mimetype.split('/')[1])) return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}})
|
|
// data.cover = coverName
|
|
// data.thumb = 'thumb_' + coverName
|
|
//
|
|
// const target = new sharp(cover.data)
|
|
// target
|
|
// .resize(coverWidth, coverHeight, {fit: 'cover'})
|
|
// .toFormat('jpg')
|
|
// .jpeg({quality: coverQuality})
|
|
// .toFile(`./static/uploads/images/projects/${coverName}`)
|
|
//
|
|
// target
|
|
// .resize(thumbWidth, thumbHeight, {fit: 'cover'})
|
|
// .toFormat('jpg')
|
|
// .jpeg({quality: thumbQuality})
|
|
// .toFile(`./static/uploads/images/projects/thumb_${coverName}`)
|
|
// }
|
|
//
|
|
// if (req.files.s2_image) {
|
|
// const s2_image = req.files.s2_image
|
|
// const s2_imageName = 's2_image_' + Date.now() + '.jpg'
|
|
// if (!_sr.supportedImageFormats.includes(s2_image.mimetype.split('/')[1])) return res.status(422).json({validation: {s2_image: {msg: _sr['fa'].format.image}}})
|
|
// data.s2_image = s2_imageName
|
|
//
|
|
// const target = new sharp(s2_image.data)
|
|
// target
|
|
// .resize(s2ImageWidth, s2ImageHeight, {fit: 'cover'})
|
|
// .toFormat('jpg')
|
|
// .jpeg({quality: s2ImageQuality})
|
|
// .toFile(`./static/uploads/images/projects/${s2_imageName}`)
|
|
// }
|
|
//
|
|
// if (req.files.s4_image) {
|
|
// const s4_image = req.files.s4_image
|
|
// const s4_imageName = 's4_image_' + Date.now() + '.jpg'
|
|
// if (!_sr.supportedImageFormats.includes(s4_image.mimetype.split('/')[1])) return res.status(422).json({validation: {s4_image: {msg: _sr['fa'].format.image}}})
|
|
// data.s4_image = s4_imageName
|
|
//
|
|
// const target = new sharp(s4_image.data)
|
|
// target
|
|
// .resize(s4ImageWidth, s4ImageHeight, {fit: 'cover'})
|
|
// .toFormat('jpg')
|
|
// .jpeg({quality: s4ImageQuality})
|
|
// .toFile(`./static/uploads/images/projects/${s4_imageName}`)
|
|
// }
|
|
//
|
|
// if (req.files.s5_image) {
|
|
// const s5_image = req.files.s5_image
|
|
// const s5_imageName = 's5_image_' + Date.now() + '.jpg'
|
|
// if (!_sr.supportedImageFormats.includes(s5_image.mimetype.split('/')[1])) return res.status(422).json({validation: {s5_image: {msg: _sr['fa'].format.image}}})
|
|
// data.s5_image = s5_imageName
|
|
//
|
|
// const target = new sharp(s5_image.data)
|
|
// target
|
|
// .resize(s5ImageWidth, s5ImageHeight, {fit: 'cover'})
|
|
// .toFormat('jpg')
|
|
// .jpeg({quality: s5ImageQuality})
|
|
// .toFile(`./static/uploads/images/projects/${s5_imageName}`)
|
|
// }
|
|
//
|
|
// if (req.files.s6_image) {
|
|
// const s6_image = req.files.s6_image
|
|
// const s6_imageName = 's6_image_' + Date.now() + '.jpg'
|
|
// if (!_sr.supportedImageFormats.includes(s6_image.mimetype.split('/')[1])) return res.status(422).json({validation: {s6_image: {msg: _sr['fa'].format.image}}})
|
|
// data.s6_image = s6_imageName
|
|
//
|
|
// const target = new sharp(s6_image.data)
|
|
// target
|
|
// .resize(s6ImageWidth, s6ImageHeight, {fit: 'cover'})
|
|
// .toFormat('jpg')
|
|
// .jpeg({quality: s6ImageQuality})
|
|
// .toFile(`./static/uploads/images/projects/${s6_imageName}`)
|
|
// }
|
|
// }
|
|
//
|
|
// Project.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
|
// if (err || !oldData) return res404(res, err)
|
|
// if (req.files) {
|
|
// if (req.files.cover && oldData.cover) {
|
|
// fs.unlink(`./static/${oldData.cover}`, err => {
|
|
// if (err) console.log(err)
|
|
// })
|
|
// fs.unlink(`./static/${oldData.thumb}`, err => {
|
|
// if (err) console.log(err)
|
|
// })
|
|
// }
|
|
//
|
|
// if (req.files.s2_image && oldData.s2_image) {
|
|
// fs.unlink(`./static/${oldData.s2_image}`, err => {
|
|
// if (err) console.log(err)
|
|
// })
|
|
// }
|
|
// if (req.files.s4_image && oldData.s4_image) {
|
|
// fs.unlink(`./static/${oldData.s4_image}`, err => {
|
|
// if (err) console.log(err)
|
|
// })
|
|
// }
|
|
// if (req.files.s5_image && oldData.s5_image) {
|
|
// fs.unlink(`./static/${oldData.s5_image}`, err => {
|
|
// if (err) console.log(err)
|
|
// })
|
|
// }
|
|
// if (req.files.s6_image && oldData.s6_image) {
|
|
// fs.unlink(`./static/${oldData.s6_image}`, err => {
|
|
// if (err) console.log(err)
|
|
// })
|
|
// }
|
|
//
|
|
// }
|
|
// return res.json({message: _sr['fa'].response.success_save})
|
|
// })
|
|
// }
|
|
// ]
|
|
|
|
module.exports.delete = [
|
|
(req, res) => {
|
|
Slide.findByIdAndRemove(req.params.id, async (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
fs.unlink(`./static/${oldData.isImage ? oldData.image : oldData.video}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
return res.json({message: _sr['fa'].response.success_remove})
|
|
})
|
|
}
|
|
]
|