161 lines
4.4 KiB
JavaScript
161 lines
4.4 KiB
JavaScript
const Catalog = require('../models/Catalog')
|
|
const mongoose = require('mongoose')
|
|
const sharp = require('sharp')
|
|
const fs = require('fs')
|
|
const {res404, res500} = require('../plugins/controllersHelperFunctions')
|
|
const {_sr} = require('../plugins/serverResponses')
|
|
|
|
// variables
|
|
const coverWidth = 390
|
|
const coverHeight = 551
|
|
const coverQuality = 100
|
|
|
|
module.exports.create = [
|
|
async (req, res) => {
|
|
if (!req.files || !req.files.file) return res.status(422).json({validation: {file: {msg: _sr['fa'].required.file}}})
|
|
if (!req.files || !req.files.cover) return res.status(422).json({validation: {cover: {msg: _sr['fa'].required.cover}}})
|
|
if (!_sr.supportedImageFormats.includes(req.files.cover.mimetype.split('/')[1])) return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}})
|
|
|
|
const {showInFa, showInEn, fa_title, en_title} = req.body
|
|
|
|
const data = {
|
|
locale: {
|
|
fa: {
|
|
title: fa_title
|
|
},
|
|
en: {
|
|
title: en_title
|
|
}
|
|
},
|
|
showInFa, showInEn
|
|
}
|
|
|
|
// file
|
|
const file = req.files.file
|
|
const fileName = 'orisoxin_catalog_' + Date.now() + '_' + file.name
|
|
file.mv(`./static/uploads/catalogs/file/${fileName}`)
|
|
data.file = fileName
|
|
|
|
// cover
|
|
const image = req.files.cover
|
|
const imageName = 'orisoxin_catalog_cover_' + Date.now() + '.jpg'
|
|
|
|
try {
|
|
const target = await sharp(image.data)
|
|
|
|
await target
|
|
.resize(coverWidth, coverHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.toFormat('jpg')
|
|
.jpeg({quality: coverQuality})
|
|
.toFile(`./static/uploads/catalogs/cover/${imageName}`)
|
|
|
|
data.cover = imageName
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
|
|
const catalog = new Catalog(data)
|
|
catalog.save((err, data) => {
|
|
if (err) return res500(res, err)
|
|
return res.json(data)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getAll = [
|
|
(req, res) => {
|
|
Catalog.find({}).sort({_id: -1}).exec((err, images) => {
|
|
if (err) return res500(res, err)
|
|
else return res.json(images)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.getOne = [
|
|
(req, res) => {
|
|
Catalog.findById(req.params.id, (err, image) => {
|
|
if (err || !image) return res404(res, err)
|
|
else return res.json(image)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.update = [
|
|
async (req, res) => {
|
|
const {showInFa, showInEn, fa_title, en_title} = req.body
|
|
|
|
const data = {
|
|
locale: {
|
|
fa: {
|
|
title: fa_title
|
|
},
|
|
en: {
|
|
title: en_title
|
|
}
|
|
},
|
|
showInFa, showInEn
|
|
}
|
|
|
|
if (req.files) {
|
|
if (req.files.file) {
|
|
const file = req.files.file
|
|
const fileName = 'orisoxin_catalog_' + Date.now() + '_' + file.name
|
|
file.mv(`./static/uploads/catalogs/file/${fileName}`)
|
|
data.file = fileName
|
|
}
|
|
|
|
if (req.files.cover) {
|
|
if (!_sr.supportedImageFormats.includes(req.files.cover.mimetype.split('/')[1])) return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}})
|
|
|
|
const image = req.files.cover
|
|
const imageName = 'orisoxsin_cover_' + Date.now() + '.jpg'
|
|
|
|
try {
|
|
const target = await sharp(image.data)
|
|
await target
|
|
.resize(coverWidth, coverHeight, {fit: 'cover'})
|
|
.withMetadata()
|
|
.toFormat('jpg')
|
|
.jpeg({quality: coverQuality})
|
|
.toFile(`./static/uploads/catalogs/cover/${imageName}`)
|
|
|
|
data.cover = imageName
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
}
|
|
}
|
|
|
|
Catalog.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
if (req.files && req.files.file) {
|
|
fs.unlink(`./static/${oldData.file}`, (err) => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
if (req.files && req.files.cover) {
|
|
fs.unlink(`./static/${oldData.cover}`, (err) => {
|
|
if (err) console.log(err)
|
|
})
|
|
}
|
|
return res.json({message: _sr['fa'].response.success_save})
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.delete = [
|
|
(req, res) => {
|
|
Catalog.findByIdAndRemove(req.params.id, (err, data) => {
|
|
if (err || !data) return res404(res, err)
|
|
fs.unlink(`./static/${data.file}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
fs.unlink(`./static/${data.cover}`, err => {
|
|
if (err) console.log(err)
|
|
})
|
|
return res.json({message: _sr['fa'].response.success_remove})
|
|
})
|
|
}
|
|
]
|