516 lines
15 KiB
JavaScript
516 lines
15 KiB
JavaScript
const Project = require('../models/Project')
|
|
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')
|
|
const readXlsxFile = require('read-excel-file/node')
|
|
|
|
// variables
|
|
const shortDescriptionMaxLength = 250
|
|
|
|
const coverWidth = 1920
|
|
const coverHeight = 418
|
|
const coverQuality = 100
|
|
|
|
const thumbWidth = 770
|
|
const thumbHeight = 354
|
|
const thumbQuality = 80
|
|
|
|
|
|
const s2ImageWidth = 1272
|
|
const s2ImageHeight = 670
|
|
const s2ImageQuality = 100
|
|
|
|
|
|
const s4ImageWidth = 1920
|
|
const s4ImageHeight = 500
|
|
const s4ImageQuality = 100
|
|
|
|
|
|
const s5ImageWidth = 1270
|
|
const s5ImageHeight = 950
|
|
const s5ImageQuality = 100
|
|
|
|
|
|
const s6ImageWidth = 1270
|
|
const s6ImageHeight = 950
|
|
const s6ImageQuality = 100
|
|
|
|
module.exports.create = [
|
|
[
|
|
|
|
body('fa_title')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('en_title')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('category')
|
|
.notEmpty().withMessage(_sr['fa'].required.category),
|
|
|
|
body('project_date')
|
|
.notEmpty().withMessage(_sr['fa'].required.date)
|
|
.bail()
|
|
.isNumeric().withMessage(_sr['fa'].format.number)
|
|
.bail()
|
|
.isLength({min: 4, max: 4}).withMessage('مقدار باید 4 رقمی باشد'),
|
|
|
|
body('has_details')
|
|
.notEmpty().withMessage('this flag is required')
|
|
],
|
|
checkValidations(validationResult),
|
|
(req, res) => {
|
|
const {fa_title, en_title, fa_client, en_client, category, tag, has_details, project_date} = req.body
|
|
const data = {
|
|
locale: {
|
|
fa: {
|
|
title: fa_title,
|
|
client: fa_client
|
|
},
|
|
en: {
|
|
title: en_title,
|
|
client: en_client
|
|
}
|
|
},
|
|
category,
|
|
has_details,
|
|
project_date
|
|
}
|
|
if (tag) data.tag = tag
|
|
if (!has_details) data.completed = true
|
|
const project = new Project(data)
|
|
project.save((err, data) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(data)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.addExel = [
|
|
async (req, res) => {
|
|
const allData = []
|
|
|
|
// const rows = await readXlsxFile(`LSAW.xlsx`, {sheet: 1})
|
|
|
|
// await Project.deleteMany({category : '61544d48340d0704add10a10'})
|
|
|
|
// for (let i = 0; i < rows.length; i++) {
|
|
// // const project = await Project.findOne({$or : [
|
|
// // {'locale.en.title' : rows[i][1].replace(/\n/g, " ")},
|
|
// // {'locale.fa.title' : rows[i][1].replace(/\n/g, " ")}
|
|
// // ]})
|
|
//
|
|
// // if (!project){
|
|
// allData.push({
|
|
// locale: {
|
|
// fa: {
|
|
// title:rows[i][1] && typeof rows[i][1]==='string'? rows[i][1].replace(/\n/g, " ") : rows[i][1],
|
|
// client: rows[i][3],
|
|
// area : rows[i][2]
|
|
// },
|
|
// en: {
|
|
// title: rows[i][1] && typeof rows[i][1]==='string'? rows[i][1].replace(/\n/g, " ") : rows[i][1],
|
|
// client: rows[i][3],
|
|
// area : rows[i][2]
|
|
// }
|
|
// },
|
|
// category : '61544d0d41304b825a19901c',
|
|
// tag : '61554c9e4b787cbf8b9bd6fd',
|
|
// importWithCode : true,
|
|
// project_date : rows[i][4]
|
|
// })
|
|
// }
|
|
// }
|
|
// console.log(allData.length)
|
|
// let addData = allData.map(data => Project.create(data));
|
|
// const result = await Promise.all(addData)
|
|
// return res.json(result)
|
|
const allPro = await Project.find()
|
|
for (const pro of allPro) {
|
|
if (pro?.locale?.en?.size || pro?.locale?.en?.area){
|
|
pro.size = pro.locale.en.size
|
|
pro.area = pro.locale.en.size
|
|
}
|
|
}
|
|
return res.json(allData)
|
|
// return res.json(rows)
|
|
// const data = {
|
|
// locale: {
|
|
// fa: {
|
|
// title: fa_title,
|
|
// client: fa_client
|
|
// },
|
|
// en: {
|
|
// title: en_title,
|
|
// client: en_client
|
|
// }
|
|
// },
|
|
// category,
|
|
// has_details,
|
|
// project_date
|
|
// }
|
|
// if (tag) data.tag = tag
|
|
// if (!has_details) data.completed = true
|
|
// const project = new Project(data)
|
|
// project.save((err, data) => {
|
|
// if (err) return res500(res, err)
|
|
// else return res.json(data)
|
|
// })
|
|
}
|
|
]
|
|
|
|
module.exports.getAll = [
|
|
(req, res) => {
|
|
Project.find().populate('category').sort({project_date : -1}).exec((err, projects) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(projects)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getOne = [
|
|
(req, res) => {
|
|
const query = req.params.id
|
|
const isObjectId = mongoose.isValidObjectId(query)
|
|
const filter = isObjectId ? {_id: query} : {$or: [{'locale.fa.title': query}, {'locale.en.title': query}]}
|
|
Project.findOne(filter, (err, project) => {
|
|
if (err || !project) return res404(res, err)
|
|
else return res.json(project)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.update = [
|
|
[
|
|
body('category')
|
|
.notEmpty().withMessage(_sr['fa'].required.category),
|
|
|
|
body('fa_title')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('en_title')
|
|
.notEmpty().withMessage(_sr['fa'].required.title),
|
|
|
|
body('category')
|
|
.notEmpty().withMessage(_sr['fa'].required.category),
|
|
|
|
body('project_date')
|
|
.notEmpty().withMessage(_sr['fa'].required.date)
|
|
.bail()
|
|
.isNumeric().withMessage(_sr['fa'].format.number)
|
|
.bail()
|
|
.isLength({min: 4, max: 4}).withMessage('مقدار باید 4 رقمی باشد'),
|
|
|
|
body('has_details')
|
|
.notEmpty().withMessage('this flag is required')
|
|
],
|
|
checkValidations(validationResult),
|
|
(req, res) => {
|
|
const {
|
|
fa_title,
|
|
en_title,
|
|
fa_short_description,
|
|
en_short_description,
|
|
fa_description,
|
|
en_description,
|
|
//
|
|
fa_client,
|
|
en_client,
|
|
//
|
|
fa_size,
|
|
en_size,
|
|
//
|
|
fa_area,
|
|
en_area,
|
|
//
|
|
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,
|
|
tag,
|
|
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,
|
|
size: fa_size,
|
|
area: fa_area,
|
|
//
|
|
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,
|
|
size: en_size,
|
|
area: en_area,
|
|
//
|
|
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 (tag) data.tag = tag
|
|
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'})
|
|
.withMetadata()
|
|
.toFormat('jpg')
|
|
.jpeg({quality: coverQuality})
|
|
.toFile(`./static/uploads/images/projects/${coverName}`)
|
|
|
|
target
|
|
.resize(thumbWidth, thumbHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.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'})
|
|
.withMetadata()
|
|
.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'})
|
|
.withMetadata()
|
|
.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'})
|
|
.withMetadata()
|
|
.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'})
|
|
.withMetadata()
|
|
.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) => {
|
|
Project.findByIdAndRemove(req.params.id, async (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
if (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 (oldData.s2_image) {
|
|
fs.unlink(`./static/${oldData.s2_image}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
if (oldData.s4_image) {
|
|
fs.unlink(`./static/${oldData.s4_image}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
if (oldData.s5_image) {
|
|
fs.unlink(`./static/${oldData.s5_image}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
if (oldData.s6_image) {
|
|
fs.unlink(`./static/${oldData.s6_image}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
return res.json({message: _sr['fa'].response.success_remove})
|
|
})
|
|
}
|
|
]
|
|
|
|
////////////////////////////
|
|
|
|
module.exports.addImageToProjectGallery = [
|
|
async (req, res) => {
|
|
if (!req.files || !req.files.image) return res.status(422).json({validation: {image: {msg: _sr['fa'].required.image}}})
|
|
try {
|
|
const project = await Project.findById(req.params.project_id)
|
|
if (!project) return res404(res, 'project not found')
|
|
|
|
|
|
const image = req.files.image
|
|
const imageName = 'project_images_' + Date.now() + '.jpg'
|
|
const thumbName = 'thumb_' + imageName
|
|
|
|
if (!_sr.supportedImageFormats.includes(image.mimetype.split('/')[1])) return res.status(422).json({validation: {image: {msg: _sr['fa'].format.image}}})
|
|
|
|
const target = new sharp(image.data)
|
|
target
|
|
.resize(s4ImageWidth, s4ImageHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.toFormat('jpg')
|
|
.jpeg({quality: s4ImageQuality})
|
|
.toFile(`./static/uploads/images/projects/${thumbName}`)
|
|
|
|
image.mv(`./static/uploads/images/projects/${imageName}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
|
|
project.images.push({image: imageName, thumb: thumbName})
|
|
project.save(err => {
|
|
if (err) return res500(res, err)
|
|
else return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
|
|
} catch (e) {
|
|
return res404(res, e)
|
|
}
|
|
|
|
}
|
|
]
|
|
|
|
module.exports.removeImageFromProject = [
|
|
(req, res) => {
|
|
Project.findOne({'images._id': req.params.image_id}, (err, project) => {
|
|
if (err || !project) return res404(res, err)
|
|
const image = project.images.id(req.params.image_id)
|
|
image.remove(err => {
|
|
if (err) console.log(err)
|
|
fs.unlink(`../static/${image.image}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
fs.unlink(`../static/${image.thumb}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
})
|
|
project.save(err => {
|
|
if (err) console.log(err)
|
|
else return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
})
|
|
}
|
|
]
|