194 lines
5.8 KiB
JavaScript
194 lines
5.8 KiB
JavaScript
const SiteSetting = require('../models/SiteSetting')
|
|
const BlogPost = require('../models/BlogPost')
|
|
const GalleryCategory = require('../models/GalleryCategory')
|
|
const Project = require('../models/Project')
|
|
const mongoose = require('mongoose')
|
|
const sharp = require('sharp')
|
|
const fs = require('fs')
|
|
const {body, param, validationResult} = require('express-validator')
|
|
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
|
const {_sr} = require('../plugins/serverResponses')
|
|
|
|
// variables
|
|
const imageWidth = 1920
|
|
const imageHeight = 600
|
|
const imageQuality = 100
|
|
|
|
module.exports.update = [
|
|
async (req, res) => {
|
|
try {
|
|
const setting = await SiteSetting.findOne()
|
|
if (setting) {
|
|
|
|
} else {
|
|
|
|
}
|
|
} catch (e) {
|
|
return res500(res, _sr['fa'].response.problem)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getAllByAdmin = [
|
|
async (req, res) => {
|
|
try {
|
|
const images = await SiteSetting.findOne()
|
|
if (images) {
|
|
return res.json(images)
|
|
} else {
|
|
const create = await SiteSetting.create({})
|
|
return res.json(create)
|
|
}
|
|
} catch (e) {
|
|
return res500(res, _sr['fa'].response.problem)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.uploadCover = [
|
|
[
|
|
param('id')
|
|
.notEmpty().withMessage('لطفا نوع عکس را مشخص کنید')
|
|
],
|
|
checkValidations(validationResult),
|
|
async (req, res) => {
|
|
try {
|
|
const picType = req?.params?.id
|
|
const image = req?.files?.file
|
|
|
|
let validation = {validation: {}}
|
|
|
|
if (!image) {
|
|
validation.validation[picType] = {msg: _sr['fa'].required.image}
|
|
return res.status(422).json(validation)
|
|
}
|
|
|
|
if (image.mimetype.split('/')[1] !== 'jpeg' && image.mimetype.split('/')[1] !== 'jpg') {
|
|
validation.validation[picType] = {msg: _sr['fa'].format.image}
|
|
res.status(422).json(validation)
|
|
}
|
|
|
|
const setting = await SiteSetting.findOne()
|
|
if (!setting) return res500(res, _sr['fa'].response.problem)
|
|
if (!setting[picType]) {
|
|
validation[picType] = {msg: 'نوع عکس وارد شده صحیح نمی باشد'}
|
|
return res.status(422).json(validation)
|
|
}
|
|
|
|
|
|
const imageName = `setting_${picType}_${Date.now()}.jpeg`
|
|
|
|
|
|
if (image && !setting[picType].includes('undefined')) {
|
|
fs.unlink(`../static/${setting[picType]}`, (err) => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
|
|
const target = await sharp(image.data)
|
|
await target
|
|
.resize(imageWidth, imageHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.toFormat('jpeg')
|
|
.jpeg({quality: imageQuality})
|
|
.toFile(`./static/uploads/images/setting/${imageName}`)
|
|
|
|
setting[picType] = imageName
|
|
|
|
await setting.save();
|
|
|
|
return res.json({message: _sr['fa'].response.success_save})
|
|
} catch (e) {
|
|
console.log(e.message)
|
|
return res500(res, _sr['fa'].response.problem)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.getAllByUser = [
|
|
async (req, res) => {
|
|
try {
|
|
const images = await SiteSetting.findOne()
|
|
|
|
if (images) {
|
|
return res.json(images)
|
|
} else {
|
|
return res.json({
|
|
aboutUS: null,
|
|
activity: null,
|
|
projects: null,
|
|
gallery: null,
|
|
catalog: null,
|
|
blogs: null,
|
|
})
|
|
}
|
|
|
|
} catch (e) {
|
|
return res500(res, _sr['fa'].response.problem)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.search = [
|
|
async (req, res) => {
|
|
const {terms} = req.query;
|
|
|
|
try {
|
|
const requests = [
|
|
BlogPost.find({
|
|
published: true,
|
|
$or: [
|
|
{'locale.fa.title': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.description': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.short_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.title': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.description': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.short_description': {$regex: terms, $options: 'i'}},
|
|
]
|
|
}),
|
|
GalleryCategory.find({
|
|
$or: [
|
|
{'locale.fa.title': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.title': {$regex: terms, $options: 'i'}},
|
|
]
|
|
}),
|
|
Project.find({
|
|
$or: [
|
|
{'locale.fa.title': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.short_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.description': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.s3_title': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.s3_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.s5_title': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.s5_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.s6_title': {$regex: terms, $options: 'i'}},
|
|
{'locale.fa.s6_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.title': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.short_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.description': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.s3_title': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.s3_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.s5_title': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.s5_description': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.s6_title': {$regex: terms, $options: 'i'}},
|
|
{'locale.en.s6_description': {$regex: terms, $options: 'i'}},
|
|
]
|
|
})
|
|
]
|
|
|
|
const getData = await Promise.all(requests);
|
|
|
|
let [blogs, categoryGallery, projects] = getData;
|
|
|
|
return res.json({
|
|
blogs,
|
|
categoryGallery,
|
|
projects
|
|
});
|
|
|
|
} catch (e) {
|
|
return res500(res, _sr['fa'].response.problem);
|
|
}
|
|
}
|
|
]
|