diff --git a/server/controllers/galleryController.js b/server/controllers/galleryController.js index 97bff16..6cf94fb 100644 --- a/server/controllers/galleryController.js +++ b/server/controllers/galleryController.js @@ -1,15 +1,12 @@ -const Gallery = require('../models/Gallery'); -const {body, validationResult} = require('express-validator'); -const sharp = require('sharp'); -const crypto = require('crypto') -const {_sr} = require('../plugins/serverResponses'); -const { - res404, - res500 -} = require('../plugins/controllersHelperFunctions'); -const fs = require('fs'); -const moment = require('moment-jalaali'); -const Category = require('./../models/Category') +const Gallery = require("../models/Gallery"); +const { body, validationResult } = require("express-validator"); +const sharp = require("sharp"); +const crypto = require("crypto"); +const { _sr } = require("../plugins/serverResponses"); +const { res404, res500 } = require("../plugins/controllersHelperFunctions"); +const fs = require("fs"); +const moment = require("moment-jalaali"); +const Category = require("./../models/Category"); ////// variables const picsWidth = 1920; @@ -21,7 +18,6 @@ const thumbPicsWidth = 600; const thumbPicsHeight = 300; const thumbPicsQuality = 80; - const coverQuality = 100; const coverWidth = 800; @@ -38,90 +34,89 @@ const thumbGraphicWidth = 600; const thumbGraphicHeight = 300; const thumbGraphicQuality = 80; -const shortTitleFront = 150 -const shortTitlePanel = 20 -const shortSumTitle = 250 - -const titleFilterRegex = new RegExp('/', 'g') +const shortTitleFront = 150; +const shortTitlePanel = 20; +const shortSumTitle = 250; +const titleFilterRegex = new RegExp("/", "g"); /////// limit const imageLimit = 2; const graphicLimit = 5; const videoLimit = 20; ////// functions -const _upload = async (fileType, file, galleryId,Hash) => { +const _upload = async (fileType, file, galleryId, Hash) => { try { - - const fileName = `gallery_${galleryId}_${Date.now()}.${file.mimetype.split('/')[1]}`; + const fileName = `gallery_${galleryId}_${Date.now()}.${ + file.mimetype.split("/")[1] + }`; const uploadPath = `./static/uploads/${fileType}s/gallery/${fileName}`; const thumbUploadPath = `./static/uploads/${fileType}s/gallery/thumb_${fileName}`; switch (fileType) { - case 'image' : + case "image": try { - const target = sharp(file.data) + const target = sharp(file.data); await target - .resize(picsWidth, picsHeight, { - fit: 'cover' - }) - .toFormat('jpg') - .jpeg({ - quality: picsQuality - }) - .toFile(uploadPath) + .resize(picsWidth, picsHeight, { + fit: "cover", + }) + .toFormat("jpg") + .jpeg({ + quality: picsQuality, + }) + .toFile(uploadPath); await target - .resize(thumbPicsWidth, thumbPicsHeight, { - fit: 'cover' - }) - .toFormat('jpg') - .jpeg({ - quality: thumbPicsQuality - }) - .toFile(thumbUploadPath) + .resize(thumbPicsWidth, thumbPicsHeight, { + fit: "cover", + }) + .toFormat("jpg") + .jpeg({ + quality: thumbPicsQuality, + }) + .toFile(thumbUploadPath); } catch (error) { - console.log(error) + console.log(error); } - break - case 'graphic': + break; + case "graphic": try { - const target = sharp(file.data) + const target = sharp(file.data); await target - // .resize(graphicWidth, graphicHeight, { - // fit: 'cover' - // }) - .toFormat('jpg') - .jpeg({ - quality: graphicQuality - }) - .toFile(uploadPath) + // .resize(graphicWidth, graphicHeight, { + // fit: 'cover' + // }) + .toFormat("jpg") + .jpeg({ + quality: graphicQuality, + }) + .toFile(uploadPath); await target - .resize(thumbGraphicWidth, thumbGraphicHeight, { - fit: 'cover' - }) - .toFormat('jpg') - .jpeg({ - quality: thumbGraphicQuality - }) - .toFile(thumbUploadPath) + .resize(thumbGraphicWidth, thumbGraphicHeight, { + fit: "cover", + }) + .toFormat("jpg") + .jpeg({ + quality: thumbGraphicQuality, + }) + .toFile(thumbUploadPath); } catch (error) { - console.log(error) + console.log(error); } - break - case 'video': + break; + case "video": await file.mv(uploadPath); - break + break; } return fileName; - } catch (error) { console.log(error); - return null + return null; } -} +}; const _deleteOne = async (fileType, name) => { try { @@ -130,23 +125,22 @@ const _deleteOne = async (fileType, name) => { if (fs.existsSync(targetFile)) { fs.unlink(targetFile, (err) => { - if (err) console.log(err) + if (err) console.log(err); }); - if (fileType !== 'video') { + if (fileType !== "video") { fs.unlink(thumbTargetFile, (err) => { - if (err) console.log(err) + if (err) console.log(err); }); } return name; } else { return null; } - } catch (error) { console.log(error); - return null + return null; } -} +}; const _deleteAll = async (galleryId, galleryType) => { try { @@ -161,11 +155,10 @@ const _deleteAll = async (galleryId, galleryType) => { }); } } - } catch (error) { - console.log(error) + console.log(error); } -} +}; const _deleteCover = async (galleryId) => { try { @@ -174,16 +167,16 @@ const _deleteCover = async (galleryId) => { const getFiles = fs.readdirSync(targetDir); for (const file of getFiles) { - if (file.includes(galleryId) && file.includes('cover')) { + if (file.includes(galleryId) && file.includes("cover")) { fs.unlink(`./static/uploads/images/gallery/${file}`, (err) => { - if (err) console.log(err) + if (err) console.log(err); }); } } } catch (error) { - console.log(err) + console.log(err); } -} +}; const _randomGallery = async (queryObject) => { let setSkip; @@ -217,116 +210,117 @@ const _randomGallery = async (queryObject) => { /** return result */ return result; } catch (error) { - console.log(error) + console.log(error); /** return error */ - return [] + return []; } -} +}; const _checkSpecPermission = async (req, category) => { - return !req.user.specialPermissions.includes(category._id) && //check parent id exist in special permissions or not - !req.user.specialPermissions.some(item => category?.treeCat?.join().includes(item)); -} - + return ( + !req.user.specialPermissions.includes(category._id) && //check parent id exist in special permissions or not + !req.user.specialPermissions.some((item) => + category?.treeCat?.join().includes(item) + ) + ); +}; /////////////////////////////////////////////////////////////////////// admin module.exports.addGallery = [ [ - body('title') - .notEmpty().withMessage(_sr['fa'].required.title) - .custom((value, {req}) => { - return Gallery.findOne({title: value}) - .then(gallery => { - if (gallery) return Promise.reject(_sr['fa'].duplicated.title); - else return true - }) - }), + body("title") + .notEmpty() + .withMessage(_sr["fa"].required.title) + .custom((value, { req }) => { + return Gallery.findOne({ title: value }).then((gallery) => { + if (gallery) return Promise.reject(_sr["fa"].duplicated.title); + else return true; + }); + }), // body('summaryTitle') // .notEmpty().withMessage(_sr['fa'].required.caption), - body('leadTitle') - .notEmpty().withMessage(_sr['fa'].required.caption), + body("leadTitle").notEmpty().withMessage(_sr["fa"].required.caption), - body('catId') - .isMongoId().withMessage(_sr['fa'].required.field) - .if((value, { - req - }) => { - return req.body.catId - }) - .custom((value) => { - return Category.findOne({ - _id: value - }) - .then(category => { - if (!category) return Promise.reject(_sr['fa'].not_found.category); - else return true - }) - }), + body("catId") + .isMongoId() + .withMessage(_sr["fa"].required.field) + .if((value, { req }) => { + return req.body.catId; + }) + .custom((value) => { + return Category.findOne({ + _id: value, + }).then((category) => { + if (!category) return Promise.reject(_sr["fa"].not_found.category); + else return true; + }); + }), - body('priority') - .isNumeric().withMessage(_sr['fa'].format.number), + body("priority").isNumeric().withMessage(_sr["fa"].format.number), - body('showInHome') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("showInHome").isBoolean().withMessage(_sr["fa"].format.boolean), - body('special') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("special").isBoolean().withMessage(_sr["fa"].format.boolean), - body('siteMap') - .optional().isBoolean().withMessage(_sr['fa'].format.boolean), + body("siteMap") + .optional() + .isBoolean() + .withMessage(_sr["fa"].format.boolean), - body('isEmbedded') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("isEmbedded").isBoolean().withMessage(_sr["fa"].format.boolean), - body('publish') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("publish").isBoolean().withMessage(_sr["fa"].format.boolean), - body('galleryType') - .notEmpty().withMessage(_sr['fa'].required.field) - .bail() - .isIn(['image', 'video', 'graphic']).withMessage(`${_sr['fa'].optional.onlyValid} => 'image' , 'video' , 'graphic`), + body("galleryType") + .notEmpty() + .withMessage(_sr["fa"].required.field) + .bail() + .isIn(["image", "video", "graphic"]) + .withMessage( + `${_sr["fa"].optional.onlyValid} => 'image' , 'video' , 'graphic` + ), - body('keywords') - .if((value) => { - return value - }) - .custom((value) => { - const keywords = _.isString(value) ? JSON.parse(value) : value; - if (!_.isArray(keywords)) { - return Promise.reject(`این فیلد باید به صورت آرایه فرستاده شود`); - } + body("keywords") + .if((value) => { + return value; + }) + .custom((value) => { + const keywords = _.isString(value) ? JSON.parse(value) : value; + if (!_.isArray(keywords)) { + return Promise.reject(`این فیلد باید به صورت آرایه فرستاده شود`); + } - return true; - }), + return true; + }), - body('modelType') - .custom((value, {req}) => { - if (req.user.portals.join() === portal.join()) { - if (!portal.includes(value)) { - return Promise.reject(_sr['fa'].required.field); - } else { - return true - } - } else { - return true - } - }), + body("modelType").custom((value, { req }) => { + if (req.user.portals.join() === portal.join()) { + if (!portal.includes(value)) { + return Promise.reject(_sr["fa"].required.field); + } else { + return true; + } + } else { + return true; + } + }), // body('metaTagDesc') // .notEmpty().withMessage(_sr['fa'].required.field), - body('newsFileId') - .optional().isMongoId().withMessage(`لطفا یک ای دی صحیح ارسال کنید`), - + body("newsFileId") + .optional() + .isMongoId() + .withMessage(`لطفا یک ای دی صحیح ارسال کنید`), ], async (req, res) => { - - const errors = validationResult(req) - if (!errors.isEmpty()) return res.status(422).json({ - validation: errors.mapped() - }); + const errors = validationResult(req); + if (!errors.isEmpty()) + return res.status(422).json({ + validation: errors.mapped(), + }); try { let { @@ -345,17 +339,20 @@ module.exports.addGallery = [ keywords, metaTagDesc, newsFileId, - favorite + favorite, } = req.body; /** this code fix problem with array in form data */ keywords = _.isString(keywords) ? JSON.parse(keywords) : keywords; /** set portals */ - modelType = (req.user.portals.join() === portal.join()) ? modelType : req.user.portals[0]; + modelType = + req.user.portals.join() === portal.join() + ? modelType + : req.user.portals[0]; const galleryData = { - title: title?.trim()?.replace(titleFilterRegex, '-'), + title: title?.trim()?.replace(titleFilterRegex, "-"), summaryTitle: summaryTitle?.trim(), leadTitle, catId, @@ -374,35 +371,38 @@ module.exports.addGallery = [ modelType, shortTitleFront: title?.trim()?.slice(0, shortTitleFront), shortTitlePanel: title?.trim()?.slice(0, shortTitlePanel), - shortSummaryTitle: summaryTitle?.trim()?.slice(0, shortSumTitle) + shortSummaryTitle: summaryTitle?.trim()?.slice(0, shortSumTitle), }; if (catId) { /** get category with populate parent */ const category = await Category.findOne({ - _id: catId - }) - .populate({ - path: 'parent', - populate: { - path: 'parent', - model: 'Category' - } - }); + _id: catId, + }).populate({ + path: "parent", + populate: { + path: "parent", + model: "Category", + }, + }); /** if undefined we have error from database */ - if (!category) return res404(res, `دسته بندی انتخاب شده برای این خبر پیدا نشد`); + if (!category) + return res404(res, `دسته بندی انتخاب شده برای این خبر پیدا نشد`); /** check special permission */ - if (req.user.specialPermissions.length && await _checkSpecPermission(req, category)) { + if ( + req.user.specialPermissions.length && + (await _checkSpecPermission(req, category)) + ) { return res.status(403).json({ - message: `شما نمیتوانید این دسته بندی را انتخاب کنید` + message: `شما نمیتوانید این دسته بندی را انتخاب کنید`, }); } if (category.modelType !== modelType) { return res.status(403).json({ - message: `شما اجازه استفاده از این دسته بندی را ندارید` + message: `شما اجازه استفاده از این دسته بندی را ندارید`, }); } @@ -410,13 +410,13 @@ module.exports.addGallery = [ galleryData.rootParent = category.rootParent || category._id; } - if (special){ - const getSpecGallery = await Gallery.find({special , modelType}) + if (special) { + const getSpecGallery = await Gallery.find({ special, modelType }); - if (getSpecGallery.length){ + if (getSpecGallery.length) { for (const item of getSpecGallery) { - item.special = false - await item.save() + item.special = false; + await item.save(); } } } @@ -427,7 +427,6 @@ module.exports.addGallery = [ return res500(res, `مشکلی در اجرای درخواست شما پیش آمده است`); } - /** * show result */ @@ -439,115 +438,112 @@ module.exports.addGallery = [ */ res500(res, error.message); } - } -] + }, +]; module.exports.updateGallery = [ [ - body('title') - .notEmpty().withMessage(_sr['fa'].required.title), + body("title").notEmpty().withMessage(_sr["fa"].required.title), // body('summaryTitle') // .notEmpty().withMessage(_sr['fa'].required.caption), - body('leadTitle') - .notEmpty().withMessage(_sr['fa'].required.caption), + body("leadTitle").notEmpty().withMessage(_sr["fa"].required.caption), - body('catId') - .isMongoId().withMessage(_sr['fa'].required.field) - .if((value, { - req - }) => { - return req.body.catId - }) - .custom((value) => { - return Category.findOne({ - _id: value - }) - .then(category => { - if (!category) return Promise.reject(_sr['fa'].not_found.category); - else return true - }) - }), + body("catId") + .isMongoId() + .withMessage(_sr["fa"].required.field) + .if((value, { req }) => { + return req.body.catId; + }) + .custom((value) => { + return Category.findOne({ + _id: value, + }).then((category) => { + if (!category) return Promise.reject(_sr["fa"].not_found.category); + else return true; + }); + }), // body('priority') // .isNumeric().withMessage(_sr['fa'].format.number), + body("showInHome").isBoolean().withMessage(_sr["fa"].format.boolean), - body('showInHome') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("special").isBoolean().withMessage(_sr["fa"].format.boolean), - body('special') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("isEmbedded").isBoolean().withMessage(_sr["fa"].format.boolean), - body('isEmbedded') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("embeddedCode").custom((value, { req }) => { + if (JSON.parse(req.body.isEmbedded) && _.isEmpty(value)) { + return Promise.reject(_sr["fa"].required.field); + } else { + return true; + } + }), + body("siteMap") + .optional() + .isBoolean() + .withMessage(_sr["fa"].format.boolean), - body('embeddedCode') - .custom((value, {req}) => { - if (JSON.parse(req.body.isEmbedded) && _.isEmpty(value)) { - return Promise.reject(_sr['fa'].required.field); - } else { - return true; - } - }), + body("publish").isBoolean().withMessage(_sr["fa"].format.boolean), - body('siteMap') - .optional().isBoolean().withMessage(_sr['fa'].format.boolean), + body("newsFileId") + .optional() + .isMongoId() + .withMessage(`لطفا یک ای دی صحیح ارسال کنید`), - body('publish') - .isBoolean().withMessage(_sr['fa'].format.boolean), + body("galleryType") + .notEmpty() + .withMessage(_sr["fa"].required.field) + .bail() + .isIn(["image", "video", "graphic"]) + .withMessage( + `${_sr["fa"].optional.onlyValid} => 'image' , 'video' , 'graphic` + ), - body('newsFileId') - .optional().isMongoId().withMessage(`لطفا یک ای دی صحیح ارسال کنید`), + body("keywords") + .if((value) => { + return value; + }) + .custom((value) => { + const keywords = _.isString(value) ? JSON.parse(value) : value; + if (!_.isArray(keywords)) { + return Promise.reject(`این فیلد باید به صورت آرایه فرستاده شود`); + } - body('galleryType') - .notEmpty().withMessage(_sr['fa'].required.field) - .bail() - .isIn(['image', 'video', 'graphic']).withMessage(`${_sr['fa'].optional.onlyValid} => 'image' , 'video' , 'graphic`), - - body('keywords') - .if((value) => { - return value - }) - .custom((value) => { - const keywords = _.isString(value) ? JSON.parse(value) : value; - if (!_.isArray(keywords)) { - return Promise.reject(`این فیلد باید به صورت آرایه فرستاده شود`); - } - - return true; - }), + return true; + }), // body('metaTagDesc') // .notEmpty().withMessage(_sr['fa'].required.field), - body('modelType') - .custom((value, {req}) => { - if (req.user.portals.join() === portal.join()) { - if (!portal.includes(value)) { - return Promise.reject(_sr['fa'].required.field); - } else { - return true - } - } else { - return true - } - }), - - body('customDate') - .optional().isString().withMessage(`لطفا یک تاریخ صحیح وارد نمایید`) + body("modelType").custom((value, { req }) => { + if (req.user.portals.join() === portal.join()) { + if (!portal.includes(value)) { + return Promise.reject(_sr["fa"].required.field); + } else { + return true; + } + } else { + return true; + } + }), + body("customDate") + .optional() + .isString() + .withMessage(`لطفا یک تاریخ صحیح وارد نمایید`), ], async (req, res) => { - const errors = validationResult(req) - if (!errors.isEmpty()) return res.status(422).json({ - validation: errors.mapped() - }); + const errors = validationResult(req); + if (!errors.isEmpty()) + return res.status(422).json({ + validation: errors.mapped(), + }); try { - /** * get data from input */ @@ -569,10 +565,9 @@ module.exports.updateGallery = [ metaTagDesc, customDate, newsFileId, - favorite + favorite, } = req.body; - /** this code fix problem with array in form data */ keywords = _.isString(keywords) ? JSON.parse(keywords) : keywords; @@ -581,7 +576,7 @@ module.exports.updateGallery = [ */ let galleryId = req.params.id; - let targetGallery = await Gallery.findOne({_id: galleryId}); + let targetGallery = await Gallery.findOne({ _id: galleryId }); if (!targetGallery) { return res404(res, `گالری مورد نظر پیدا نشد`); @@ -593,11 +588,13 @@ module.exports.updateGallery = [ if (targetGallery.title !== title) { var checkDuppTitle = await Gallery.findOne({ - title: title + title: title, }); if (checkDuppTitle) { - return res.status(422).json({validation: {title: {msg: _sr['fa'].duplicated.title}}}); + return res.status(422).json({ + validation: { title: { msg: _sr["fa"].duplicated.title } }, + }); } } @@ -614,29 +611,32 @@ module.exports.updateGallery = [ /** get category with populate parent */ var category = await Category.findOne({ - _id: catId - }) - .populate({ - path: 'parent', - populate: { - path: 'parent', - model: 'Category' - } - }); + _id: catId, + }).populate({ + path: "parent", + populate: { + path: "parent", + model: "Category", + }, + }); /** if undefined we have error from database */ - if (!category) return res404(res, `دسته بندی انتخاب شده برای این رسانه پیدا نشد`); + if (!category) + return res404(res, `دسته بندی انتخاب شده برای این رسانه پیدا نشد`); /** check special permission */ - if (req.user.specialPermissions.length && await _checkSpecPermission(req, category)) { + if ( + req.user.specialPermissions.length && + (await _checkSpecPermission(req, category)) + ) { return res.status(403).json({ - message: `شما مجاز به انتخاب این دسته بندی نمی باشید` + message: `شما مجاز به انتخاب این دسته بندی نمی باشید`, }); } if (category.modelType !== modelType) { return res.status(403).json({ - message: `محدوده انتخاب شده شما با محدوده دسته بندی همخوانی ندارد` + message: `محدوده انتخاب شده شما با محدوده دسته بندی همخوانی ندارد`, }); } @@ -645,30 +645,30 @@ module.exports.updateGallery = [ targetGallery.rootParent = category.rootParent || category._id; } - if (isEmbedded === 'true' && galleryType === 'video') { + if (isEmbedded === "true" && galleryType === "video") { await _deleteOne(galleryType, targetGallery.video); - targetGallery.video = null - targetGallery.galleryStatus = false + targetGallery.video = null; + targetGallery.galleryStatus = false; } + if (special) { + const getSpecGallery = await Gallery.find({ special, modelType }); - if (special){ - const getSpecGallery = await Gallery.find({special , modelType}) - - if (getSpecGallery.length){ + if (getSpecGallery.length) { for (const item of getSpecGallery) { - item.special = false - await item.save() + item.special = false; + await item.save(); } } } - targetGallery.title = title?.trim()?.replace(titleFilterRegex, '-'); + targetGallery.title = title?.trim()?.replace(titleFilterRegex, "-"); targetGallery.summaryTitle = summaryTitle?.trim(); - targetGallery.leadTitle = leadTitle + targetGallery.leadTitle = leadTitle; targetGallery.isEmbedded = isEmbedded; targetGallery.embeddedCode = embeddedCode; - if (!_.isEmpty(embeddedCode) && targetGallery.galleryType === 'video') targetGallery.galleryStatus = true + if (!_.isEmpty(embeddedCode) && targetGallery.galleryType === "video") + targetGallery.galleryStatus = true; targetGallery.catId = catId; // targetGallery.priority = priority; targetGallery.showInHome = showInHome; @@ -679,17 +679,20 @@ module.exports.updateGallery = [ targetGallery.keywords = keywords; targetGallery.metaTagDesc = metaTagDesc?.trim() || leadTitle?.trim(); targetGallery.favorite = favorite; - targetGallery.shortTitleFront = title?.slice(0, shortTitleFront) - targetGallery.shortTitlePanel = title?.slice(0, shortTitlePanel) - targetGallery.shortSummaryTitle = summaryTitle?.trim().slice(0, shortSumTitle) + targetGallery.shortTitleFront = title?.slice(0, shortTitleFront); + targetGallery.shortTitlePanel = title?.slice(0, shortTitlePanel); + targetGallery.shortSummaryTitle = summaryTitle + ?.trim() + .slice(0, shortSumTitle); targetGallery.customDate = customDate || targetGallery.created_at; targetGallery.newsFileId = newsFileId; targetGallery._updatedBy = req.user._id; - if (customDate) targetGallery.customDateHistory.push({ - customDate, - _updatedBy: req.user._id - }) + if (customDate) + targetGallery.customDateHistory.push({ + customDate, + _updatedBy: req.user._id, + }); await targetGallery.save(); @@ -697,19 +700,18 @@ module.exports.updateGallery = [ * show result */ return res.json(targetGallery); - } catch (error) { return res500(res, error.message); } - } -] + }, +]; module.exports.deletOneGallery = [ async (req, res) => { try { var galleryId = req.params.id; - var targetGallery = await Gallery.findOne({_id: galleryId}); + var targetGallery = await Gallery.findOne({ _id: galleryId }); /** check gallery exist or not */ if (!targetGallery) { @@ -730,15 +732,14 @@ module.exports.deletOneGallery = [ * show result */ return res.json(targetGallery); - } catch (error) { /** * if we got unkown error show to client */ return res500(res, error); } - } -] + }, +]; module.exports.getOneGallery = [ async (req, res) => { @@ -747,7 +748,7 @@ module.exports.getOneGallery = [ // var findQuery = req.user.portals.length == 2 ? {_id : galleryId} : {_id : galleryId , modelType : req.user.portals[0]}; - var gallery = await Gallery.findOne({_id: galleryId}).populate('catId'); + var gallery = await Gallery.findOne({ _id: galleryId }).populate("catId"); if (!gallery) { return res404(res, `گالری مورد نظر پیدا نشد`); @@ -767,39 +768,48 @@ module.exports.getOneGallery = [ */ res500(res, error); } - } -] + }, +]; module.exports.getAll = [ async (req, res) => { try { - - - var findQuery = req.user.portals.join() === portal.join() ? {} : {modelType: req.user.portals[0]}; + var findQuery = + req.user.portals.join() === portal.join() + ? {} + : { modelType: req.user.portals[0] }; // @todo can change it if (req.user.specialPermissions.length) { - findQuery.catId = {$in: req.user.specialPermissions} + findQuery.catId = { $in: req.user.specialPermissions }; } /** get all gallery */ var allGallery = await Gallery.paginate(findQuery, { - page: req.query.page, limit, populate: [{ - path: '_creator', - select: { - firstName: 1, - lastName: 1, - profilePic: 1 - } - }, {path: 'catId'}], + page: req.query.page, + limit, + populate: [ + { + path: "_creator", + select: { + firstName: 1, + lastName: 1, + profilePic: 1, + }, + }, + { path: "catId" }, + ], sort: { - customDate: -1 - } + customDate: -1, + }, }); /** if null we have error */ if (!allGallery) { - return res500(res, `مشکلی در گرفتن گالری ها پیش آمده لطفا با پشتیبانی تماس بگیرید`); + return res500( + res, + `مشکلی در گرفتن گالری ها پیش آمده لطفا با پشتیبانی تماس بگیرید` + ); } /** show result to client */ @@ -808,19 +818,19 @@ module.exports.getAll = [ /** if we have unknown error show it */ return res500(res, error.message); } - } -] + }, +]; module.exports.search = [ async (req, res) => { try { let findQuery = {}; - findQuery['$and'] = []; - findQuery['$or'] = []; + findQuery["$and"] = []; + findQuery["$or"] = []; if (req.user.portals.length) { findQuery.modelType = { - $in: req.user.portals + $in: req.user.portals, }; } @@ -832,120 +842,119 @@ module.exports.search = [ galleryStatus, publish, galleryType, - date + date, } = req.body; /** search by term */ if (term && !_.isEmpty(term)) { - findQuery['$or'].push( - { - title: { - $regex: '.*' + term + '.*' - }, - } - ); + findQuery["$or"].push({ + title: { + $regex: ".*" + term + ".*", + }, + }); } /** search by catId */ if (req.user.specialPermissions.length) { - findQuery.catId = {$in: req.user.specialPermissions} + findQuery.catId = { $in: req.user.specialPermissions }; } else if (catId && !_.isEmpty(catId)) { - findQuery['$and'].push({ - catId + findQuery["$and"].push({ + catId, }); } /** search by galleryType */ if (galleryType && !_.isEmpty(galleryType)) { - findQuery['$and'].push({ - galleryType + findQuery["$and"].push({ + galleryType, }); } /** search by publish */ if (!_.isUndefined(publish) && _.isBoolean(publish)) { - findQuery['$and'].push({ - publish + findQuery["$and"].push({ + publish, }); } /** search by portals */ if (req.user.portals.length) { findQuery.modelType = { - $in: req.user.portals + $in: req.user.portals, }; } else if (portals && !_.isEmpty(portals)) { - findQuery['$and'].push({ + findQuery["$and"].push({ modelType: { - $in: portals - } - }) + $in: portals, + }, + }); } /** search by galleryStatus */ if (!_.isUndefined(galleryStatus) && _.isBoolean(galleryStatus)) { - findQuery['$and'].push({ - galleryStatus - }) + findQuery["$and"].push({ + galleryStatus, + }); } /** search by date */ if (date && !_.isEmpty(date)) { if (date.length === 1) { - findQuery['$and'].push({ + findQuery["$and"].push({ created_at: { - $gte: moment(date[0]).startOf('days'), - $lt: moment(date[0]).endOf('days') - } + $gte: moment(date[0]).startOf("days"), + $lt: moment(date[0]).endOf("days"), + }, }); } else { - findQuery['$and'].push({ + findQuery["$and"].push({ created_at: { $gte: new Date(date[0]), - $lt: new Date(date[1]) - } + $lt: new Date(date[1]), + }, }); } } - _.isEmpty(findQuery['$and']) && delete findQuery['$and']; - _.isEmpty(findQuery['$or']) && delete findQuery['$or']; + _.isEmpty(findQuery["$and"]) && delete findQuery["$and"]; + _.isEmpty(findQuery["$or"]) && delete findQuery["$or"]; /** get gallery */ var gallery = await Gallery.paginate(findQuery, { - page: req.query.page, limit, populate: [{ - path: '_creator', - select: { - firstName: 1, - lastName: 1, - profilePic: 1 - } - },{path: 'catId'}], + page: req.query.page, + limit, + populate: [ + { + path: "_creator", + select: { + firstName: 1, + lastName: 1, + profilePic: 1, + }, + }, + { path: "catId" }, + ], sort: { - customDate: -1 - } + customDate: -1, + }, }); /** show result */ return res.json(gallery); } catch (error) { - return res500(res, error?.message) + return res500(res, error?.message); } - } -] + }, +]; /////////////////////////////////////////////////////////////////////// manage gallery files /// Develope module.exports.uploadPDF = [ [ - body('title') - .notEmpty().withMessage(_sr['fa'].required.title) - .bail(), - body('pdf') - .notEmpty().withMessage(_sr['fa'].required.field) - .bail(), + body("title").notEmpty().withMessage(_sr["fa"].required.title).bail(), + body("pdf").notEmpty().withMessage(_sr["fa"].required.field).bail(), ], async (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ - validation: errors.mapped() + validation: errors.mapped(), }); } try { @@ -953,7 +962,7 @@ module.exports.uploadPDF = [ if (!req.files || !req.files.file) { return res.status(422).json({ - validation: { file: { msg: `لطفا یک فایل ارسال کنید` } } + validation: { file: { msg: `لطفا یک فایل ارسال کنید` } }, }); } @@ -961,64 +970,88 @@ module.exports.uploadPDF = [ const mb = file.size / (1024 * 1024); - if (fileType === 'image' && mb > imageLimit) { + if (fileType === "image" && mb > imageLimit) { return res.status(422).json({ - validation: { file: { msg: `${_sr['fa'].max_char.data_size} 5 مگابایت` } } + validation: { + file: { msg: `${_sr["fa"].max_char.data_size} 5 مگابایت` }, + }, }); } - if (fileType === 'graphic' && mb > graphicLimit) { + if (fileType === "graphic" && mb > graphicLimit) { return res.status(422).json({ - validation: { file: { msg: `${_sr['fa'].max_char.data_size} 10 مگابایت` } } + validation: { + file: { msg: `${_sr["fa"].max_char.data_size} 10 مگابایت` }, + }, }); } - if (fileType === 'video' && mb > videoLimit) { + if (fileType === "video" && mb > videoLimit) { return res.status(422).json({ - validation: { file: { msg: `${_sr['fa'].max_char.data_size} 50 مگابایت` } } + validation: { + file: { msg: `${_sr["fa"].max_char.data_size} 50 مگابایت` }, + }, }); } - if (fileType === 'image' && !_sr.supportedImageFormats.includes(file.mimetype.split('/')[1])) { + if ( + fileType === "image" && + !_sr.supportedImageFormats.includes(file.mimetype.split("/")[1]) + ) { return res.status(422).json({ - validation: { file: { msg: _sr['fa'].format.image } } + validation: { file: { msg: _sr["fa"].format.image } }, }); } - if (fileType === 'video' && !_sr.supportedVideoFormats.includes(file.mimetype.split('/')[1])) { + if ( + fileType === "video" && + !_sr.supportedVideoFormats.includes(file.mimetype.split("/")[1]) + ) { return res.status(422).json({ - validation: { file: { msg: _sr['fa'].format.video } } + validation: { file: { msg: _sr["fa"].format.video } }, }); } if (!req.user.portals.includes(targetGallery.modelType)) { - return res.status(500).json({ msg: `شما اجازه آپلود فایل برای این گالری را ندارید` }); + return res + .status(500) + .json({ msg: `شما اجازه آپلود فایل برای این گالری را ندارید` }); } if (targetGallery.galleryType !== fileType) { return res.status(422).json({ - validation: { file: { msg: `شما اجازه آپلود فایل با تایپ متفاوت از گالری ندارید` } } + validation: { + file: { + msg: `شما اجازه آپلود فایل با تایپ متفاوت از گالری ندارید`, + }, + }, }); } const fileName = await _upload(fileType, file, targetGallery._id); switch (fileType) { - case 'image': - targetGallery.uidImage.push(fileHash) + case "image": + targetGallery.uidImage.push(fileHash); targetGallery.images.push(fileName); targetGallery.thumbImages.push(`thumb_${fileName}`); break; - case 'video': - if (targetGallery.video && await fs.existsSync(`./static/${targetGallery?.video}`)) { + case "video": + if ( + targetGallery.video && + (await fs.existsSync(`./static/${targetGallery?.video}`)) + ) { fs.unlink(`./static/${targetGallery.video}`, (err) => { if (err) return res.status(500).json({ msg: err.message }); }); } targetGallery.video = fileName; break; - case 'graphic': - if (targetGallery.graphic && await fs.existsSync(`./static/${targetGallery?.graphic}`)) { + case "graphic": + if ( + targetGallery.graphic && + (await fs.existsSync(`./static/${targetGallery?.graphic}`)) + ) { fs.unlink(`./static/${targetGallery.graphic}`, (err) => { if (err) return res.status(500).json({ msg: err.message }); }); @@ -1044,31 +1077,35 @@ module.exports.uploadPDF = [ return res.status(500).json({ msg: error.message }); } - } + }, ]; module.exports.uploadFile = [ [ - body('galleryId') - .notEmpty().withMessage(_sr['fa'].required.title) - .bail() - .custom((value, { req }) => { - return Gallery.findOne({ _id: value }) - .then(gallery => { - if (!gallery) return Promise.reject(_sr['fa'].not_found.item_id); - else return true; - }); - }), - body('fileType') - .notEmpty().withMessage(_sr['fa'].required.field) - .bail() - .isIn(['image', 'video', 'graphic']).withMessage(`${_sr['fa'].optional.onlyValid} => 'image' , 'video' , 'graphic'`), + body("galleryId") + .notEmpty() + .withMessage(_sr["fa"].required.title) + .bail() + .custom((value, { req }) => { + return Gallery.findOne({ _id: value }).then((gallery) => { + if (!gallery) return Promise.reject(_sr["fa"].not_found.item_id); + else return true; + }); + }), + body("fileType") + .notEmpty() + .withMessage(_sr["fa"].required.field) + .bail() + .isIn(["image", "video", "graphic"]) + .withMessage( + `${_sr["fa"].optional.onlyValid} => 'image' , 'video' , 'graphic'` + ), ], async (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ - validation: errors.mapped() + validation: errors.mapped(), }); } @@ -1077,84 +1114,113 @@ module.exports.uploadFile = [ if (!req.files || !req.files.file) { return res.status(422).json({ - validation: { file: { msg: `لطفا یک فایل ارسال کنید` } } + validation: { file: { msg: `لطفا یک فایل ارسال کنید` } }, }); } const file = req.files.file; const fileBuffer = file.data; - const fileHash = crypto.createHash('md5').update(fileBuffer).digest('hex'); + const fileHash = crypto + .createHash("md5") + .update(fileBuffer) + .digest("hex"); const targetGallery = await Gallery.findOne({ _id: galleryId }); if (!targetGallery) { return res.status(404).json({ msg: `گالری پیدا نشد` }); } - const Hash = targetGallery.uidImage.map(path => path.replace("/uploads/images/gallery/", "")); + const Hash = targetGallery.uidImage.map((path) => + path.replace("/uploads/images/gallery/", "") + ); if (Hash.includes(fileHash)) { return res.status(200).json({ msg: `این فایل قبلا بارگذاری شده` }); } const mb = file.size / (1024 * 1024); - if (fileType === 'image' && mb > imageLimit) { + if (fileType === "image" && mb > imageLimit) { return res.status(422).json({ - validation: { file: { msg: `${_sr['fa'].max_char.data_size} 5 مگابایت` } } + validation: { + file: { msg: `${_sr["fa"].max_char.data_size} 5 مگابایت` }, + }, }); } - if (fileType === 'graphic' && mb > graphicLimit) { + if (fileType === "graphic" && mb > graphicLimit) { return res.status(422).json({ - validation: { file: { msg: `${_sr['fa'].max_char.data_size} 10 مگابایت` } } + validation: { + file: { msg: `${_sr["fa"].max_char.data_size} 10 مگابایت` }, + }, }); } - if (fileType === 'video' && mb > videoLimit) { + if (fileType === "video" && mb > videoLimit) { return res.status(422).json({ - validation: { file: { msg: `${_sr['fa'].max_char.data_size} 50 مگابایت` } } + validation: { + file: { msg: `${_sr["fa"].max_char.data_size} 50 مگابایت` }, + }, }); } - if (fileType === 'image' && !_sr.supportedImageFormats.includes(file.mimetype.split('/')[1])) { + if ( + fileType === "image" && + !_sr.supportedImageFormats.includes(file.mimetype.split("/")[1]) + ) { return res.status(422).json({ - validation: { file: { msg: _sr['fa'].format.image } } + validation: { file: { msg: _sr["fa"].format.image } }, }); } - if (fileType === 'video' && !_sr.supportedVideoFormats.includes(file.mimetype.split('/')[1])) { + if ( + fileType === "video" && + !_sr.supportedVideoFormats.includes(file.mimetype.split("/")[1]) + ) { return res.status(422).json({ - validation: { file: { msg: _sr['fa'].format.video } } + validation: { file: { msg: _sr["fa"].format.video } }, }); } if (!req.user.portals.includes(targetGallery.modelType)) { - return res.status(500).json({ msg: `شما اجازه آپلود فایل برای این گالری را ندارید` }); + return res + .status(500) + .json({ msg: `شما اجازه آپلود فایل برای این گالری را ندارید` }); } if (targetGallery.galleryType !== fileType) { return res.status(422).json({ - validation: { file: { msg: `شما اجازه آپلود فایل با تایپ متفاوت از گالری ندارید` } } + validation: { + file: { + msg: `شما اجازه آپلود فایل با تایپ متفاوت از گالری ندارید`, + }, + }, }); } const fileName = await _upload(fileType, file, targetGallery._id); switch (fileType) { - case 'image': - targetGallery.uidImage.push(fileHash) + case "image": + targetGallery.uidImage.push(fileHash); targetGallery.images.push(fileName); targetGallery.thumbImages.push(`thumb_${fileName}`); break; - case 'video': - if (targetGallery.video && await fs.existsSync(`./static/${targetGallery?.video}`)) { + case "video": + if ( + targetGallery.video && + (await fs.existsSync(`./static/${targetGallery?.video}`)) + ) { fs.unlink(`./static/${targetGallery.video}`, (err) => { if (err) return res.status(500).json({ msg: err.message }); }); } targetGallery.video = fileName; break; - case 'graphic': - if (targetGallery.graphic && await fs.existsSync(`./static/${targetGallery?.graphic}`)) { + case "graphic": + if ( + targetGallery.graphic && + (await fs.existsSync(`./static/${targetGallery?.graphic}`)) + ) { fs.unlink(`./static/${targetGallery.graphic}`, (err) => { if (err) return res.status(500).json({ msg: err.message }); }); @@ -1186,7 +1252,7 @@ module.exports.uploadFile = [ */ return res.status(500).json({ msg: error.message }); } - } + }, ]; // module.exports.uploadFile = [ // [ @@ -1359,42 +1425,43 @@ module.exports.uploadFile = [ module.exports.deleteFile = [ [ - body('galleryId') - .notEmpty().withMessage(_sr['fa'].required.title) - .bail() - .custom((value, {req}) => { - return Gallery.findOne({_id: value}) - .then(gallery => { - if (!gallery) return Promise.reject(_sr['fa'].not_found.item_id); - else return true - }) - }), - - body('name') - .notEmpty().withMessage(_sr['fa'].required.field) + body("galleryId") + .notEmpty() + .withMessage(_sr["fa"].required.title) + .bail() + .custom((value, { req }) => { + return Gallery.findOne({ _id: value }).then((gallery) => { + if (!gallery) return Promise.reject(_sr["fa"].not_found.item_id); + else return true; + }); + }), + body("name").notEmpty().withMessage(_sr["fa"].required.field), ], async (req, res) => { - - const errors = validationResult(req) - if (!errors.isEmpty()) return res.status(422).json({ - validation: errors.mapped() - }); + const errors = validationResult(req); + if (!errors.isEmpty()) + return res.status(422).json({ + validation: errors.mapped(), + }); try { - const {galleryId, name} = req.body; + const { galleryId, name } = req.body; - const targetGallery = await Gallery.findOne({_id: galleryId}).lean(); + const targetGallery = await Gallery.findOne({ _id: galleryId }).lean(); if (!targetGallery) return res404(res, `گالری مورد نظر پیدا نشد`); /** check portal type */ - if (!req.user.portals.includes(targetGallery.modelType)) return res500(res, `شما اجازه حذف فایل برای این گالری را ندارید`); + if (!req.user.portals.includes(targetGallery.modelType)) + return res500(res, `شما اجازه حذف فایل برای این گالری را ندارید`); if ( - (targetGallery.images.length === 0 && targetGallery.galleryType === 'image') || - (!targetGallery.video && targetGallery.galleryType === 'video') - ) return res404(res, `عکس یا فیلم درخواست شده پیدا نشد`) + (targetGallery.images.length === 0 && + targetGallery.galleryType === "image") || + (!targetGallery.video && targetGallery.galleryType === "video") + ) + return res404(res, `عکس یا فیلم درخواست شده پیدا نشد`); let deleteFile = await _deleteOne(targetGallery.galleryType, name); @@ -1402,23 +1469,29 @@ module.exports.deleteFile = [ /** update gallery after delete file */ let imageArr = []; - if (targetGallery.galleryType === 'image' || targetGallery.galleryType === 'graphic') { + if ( + targetGallery.galleryType === "image" || + targetGallery.galleryType === "graphic" + ) { let index = targetGallery.images.findIndex((item) => { return item.includes(deleteFile || name); }); if (index > -1) { targetGallery.images.splice(index, 1); - targetGallery.thumbImages.splice(index, 1) + targetGallery.thumbImages.splice(index, 1); } - } else { targetGallery.video = null; } targetGallery.galleryStatus = !!targetGallery.images.length; - var gallery = await Gallery.findByIdAndUpdate(targetGallery._id, targetGallery, {new: true}); + var gallery = await Gallery.findByIdAndUpdate( + targetGallery._id, + targetGallery, + { new: true } + ); /** * show result */ @@ -1430,8 +1503,8 @@ module.exports.deleteFile = [ */ return res500(res, error.message); } - } -] + }, +]; // module.exports.uploadCover = [ // [ @@ -1575,74 +1648,160 @@ module.exports.deleteFile = [ //Develope module.exports.uploadCover = [ [ - body('galleryId') - .notEmpty().withMessage(_sr['fa'].required.title) + body("galleryId") + .notEmpty() + .withMessage(_sr["fa"].required.title) + .bail() + .isMongoId() + .withMessage("آی دی گالری نامعتبر است") + .bail() + .custom(async (value, { req }) => { + try { + const gallery = await Gallery.findById(value); + if (!gallery) { + return Promise.reject(_sr["fa"].not_found.item_id); + } + return true; + } catch (error) { + return Promise.reject("آی دی گالری نامعتبر است"); + } + }), ], async (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ - validation: errors.mapped() + validation: errors.mapped(), }); } try { const { galleryId } = req.body; + // Debug logging + console.log("galleryId received:", galleryId); + console.log("req.body:", req.body); + + if (!galleryId) { + return res.status(422).json({ + validation: { galleryId: { msg: `آی دی گالری الزامی است` } }, + }); + } + if (!req.files || !req.files.file) { return res.status(422).json({ - validation: { file: { msg: `لطفا یک فایل ارسال کنید` } } + validation: { file: { msg: `لطفا یک فایل ارسال کنید` } }, }); } const file = req.files.file; + // Find the gallery record with better error handling + let targetGallery; + try { + targetGallery = await Gallery.findById(galleryId); + console.log("targetGallery found:", !!targetGallery); + } catch (dbError) { + console.error("Database error:", dbError); + return res.status(422).json({ + validation: { galleryId: { msg: `آی دی گالری نامعتبر است` } }, + }); + } + + if (!targetGallery) { + return res404(res, `گالری پیدا نشد`); + } + + // Check if user object exists and has portals + if (!req.user || !req.user.portals) { + return res.status(401).json({ message: `اطلاعات کاربری نامعتبر است` }); + } + + /** check portal type */ + if (!req.user.portals.includes(targetGallery.modelType)) { + return res500(res, `شما اجازه آپلود فایل برای این گالری را ندارید`); + } + /** start convert byte to mb */ const mb = file.size / (1024 * 1024); // Convert bytes to megabytes /** check limit size of image */ if (mb > 5) { return res.status(422).json({ - validation: { file: { msg: `${_sr['fa'].max_char.data_size} 5 مگابایت` } } + validation: { + file: { msg: `${_sr["fa"].max_char.data_size} 5 مگابایت` }, + }, }); } /** check format of image */ - if (!_sr.supportedImageFormats.includes(file.mimetype.split('/')[1])) { + if (!_sr.supportedImageFormats.includes(file.mimetype.split("/")[1])) { return res.status(422).json({ - validation: { file: { msg: _sr['fa'].format.image } } + validation: { file: { msg: _sr["fa"].format.image } }, }); } - const imageName = `cover_gallery_${Date.now()}.jpg`; + const imageName = `cover_gallery_${Date.now()}_${targetGallery._id}.jpg`; const thumbImg = `thumb_${imageName}`; try { const target = sharp(file.data); await target - .resize(coverWidth, coverHeight, { - fit: 'cover' - }) - .toFormat('jpg') - .jpeg({ - quality: coverQuality - }) - .toFile(`./static/uploads/images/gallery/${imageName}`); + .resize(coverWidth, coverHeight, { + fit: "cover", + }) + .toFormat("jpg") + .jpeg({ + quality: coverQuality, + }) + .toFile(`./static/uploads/images/gallery/${imageName}`); await target - .resize(thumbCoverWidth, thumbCoverHeight, { - fit: 'cover' - }) - .toFormat('jpg') - .jpeg({ - quality: thumbCoverQuality - }) - .toFile(`./static/uploads/images/gallery/${thumbImg}`); + .resize(thumbCoverWidth, thumbCoverHeight, { + fit: "cover", + }) + .toFormat("jpg") + .jpeg({ + quality: thumbCoverQuality, + }) + .toFile(`./static/uploads/images/gallery/${thumbImg}`); - return res.json({ - cover: imageName, - thumbCover: thumbImg, - message: "Images uploaded successfully" - }); + /** + * if exist old pic delete it from file + */ + if (targetGallery.cover) { + let oldPic = `./static/uploads/images/gallery/${targetGallery.cover}`; + if (fs.existsSync(oldPic)) { + fs.unlink(oldPic, (err) => { + if (err) console.log("Error deleting old cover:", err); + }); + } + } + + if (targetGallery.thumbCover) { + let oldThumbPic = `./static/uploads/images/gallery/${targetGallery.thumbCover}`; + if (fs.existsSync(oldThumbPic)) { + fs.unlink(oldThumbPic, (err) => { + if (err) console.log("Error deleting old thumb cover:", err); + }); + } + } + + // Update gallery record with new cover images + targetGallery.cover = imageName; + targetGallery.thumbCover = thumbImg; + targetGallery.galleryStatus = + Boolean(targetGallery.cover) || + Boolean(targetGallery.images?.length) || + Boolean(targetGallery.video) || + Boolean(targetGallery.graphic); + targetGallery._updatedBy = req.user._id; + + await targetGallery.save(); + + // Return the updated gallery + const gallery = await Gallery.findById(targetGallery._id); + + return res.json(gallery); } catch (e) { return res500(res, e?.message); } @@ -1650,14 +1809,14 @@ module.exports.uploadCover = [ console.log(error); return res500(res, error.message); } - } + }, ]; ////////////////////////////////////////////////////////////////////// get gallery module.exports.getGalleryPublic = [ async (req, res) => { try { - const {type, page} = req.query; - const catId = req.params.catId + const { type, page } = req.query; + const catId = req.params.catId; const findQuery = {}; findQuery.galleryType = type; @@ -1670,40 +1829,40 @@ module.exports.getGalleryPublic = [ page: page || 1, limit: setLimit, populate: { - path: '_creator', select: 'firstName lastName' + path: "_creator", + select: "firstName lastName", }, - sort: {customDate: -1} + sort: { customDate: -1 }, }); - const favorites = await Gallery.find({...findQuery, favorite: true}) + const favorites = await Gallery.find({ ...findQuery, favorite: true }); const result = { all: getGallery, - favorites: favorites - } + favorites: favorites, + }; return res.json(result); } catch (error) { return res500(res, error?.message); } - } -] - + }, +]; module.exports.GetArchive = [ - async (req,res)=>{ - try { - const Archive = await Gallery.find({ images: { $exists: true } }); - res.status(200).json(Archive) - }catch (e) { - res.status(500).json({msg:e.message}) - } + async (req, res) => { + try { + const Archive = await Gallery.find({ images: { $exists: true } }); + res.status(200).json(Archive); + } catch (e) { + res.status(500).json({ msg: e.message }); } -] + }, +]; module.exports.getAllGallery = [ async (req, res) => { try { - const {type, page, portal} = req.query; + const { type, page, portal } = req.query; let findQuery = {}; findQuery.galleryType = type; @@ -1717,26 +1876,32 @@ module.exports.getAllGallery = [ page: page || 1, limit: setLimit, populate: { - path: '_creator', select: 'firstName lastName' + path: "_creator", + select: "firstName lastName", }, - sort: {customDate: -1} + sort: { customDate: -1 }, }); - const favorites = await Gallery.find({...findQuery, favorite: true}).sort({customDate: -1}) - const hero = await Gallery.findOne({...findQuery, special: true}).sort({customDate: -1}) + const favorites = await Gallery.find({ + ...findQuery, + favorite: true, + }).sort({ customDate: -1 }); + const hero = await Gallery.findOne({ ...findQuery, special: true }).sort({ + customDate: -1, + }); const result = { all: getGallery, hero: hero, - favorites: favorites - } + favorites: favorites, + }; return res.json(result); } catch (error) { return res500(res, error?.message); } - } -] + }, +]; module.exports.viewGallery = [ async (req, res) => { @@ -1746,9 +1911,8 @@ module.exports.viewGallery = [ let targetGallery = await Gallery.findOne({ _id: galleryTitle, galleryStatus: true, - publish: true - }) - .populate('_creator', {firstName: 1, lastName: 1, profilePic: 1}); + publish: true, + }).populate("_creator", { firstName: 1, lastName: 1, profilePic: 1 }); if (!targetGallery) { return res404(res, `گالری مورد نطر پیدا نشد`); @@ -1762,16 +1926,13 @@ module.exports.viewGallery = [ } catch (error) { return res500(res, error?.message); } - } -] + }, +]; module.exports.getRelated = [ async (req, res) => { try { - const { - type, - title - } = req.query; + const { type, title } = req.query; const targetGallery = await Gallery.findById(title); @@ -1783,21 +1944,24 @@ module.exports.getRelated = [ findQuery.galleryType = type; findQuery._id = { - $ne: title + $ne: title, }; findQuery.galleryStatus = true; findQuery.publish = true; - const getGallery = await Gallery.find(findQuery).populate({ - path: '_creator', - select: 'firstName lastName' - }).limit(4).sort({ - customDate: -1 - }); + const getGallery = await Gallery.find(findQuery) + .populate({ + path: "_creator", + select: "firstName lastName", + }) + .limit(4) + .sort({ + customDate: -1, + }); return res.json(getGallery); } catch (error) { return res500(res, error?.message); } - } -] + }, +]; diff --git a/server/index.js b/server/index.js index 7caeb29..0f54e89 100644 --- a/server/index.js +++ b/server/index.js @@ -26,7 +26,7 @@ for (const item of portals) { // Create express instance const app = express(); -app.set("trust proxy", true); +app.set("trust proxy", 1); app.use((req, res, next) => { console.log(req.ip); console.log(req.ips); diff --git a/static/uploads/images/gallery/cover_gallery_1753089285039_687de3d19987640011091020.jpg b/static/uploads/images/gallery/cover_gallery_1753089285039_687de3d19987640011091020.jpg new file mode 100644 index 0000000..3c3927d Binary files /dev/null and b/static/uploads/images/gallery/cover_gallery_1753089285039_687de3d19987640011091020.jpg differ diff --git a/static/uploads/images/gallery/cover_gallery_1753089310464_687c5ec6a96cbc0010159e31.jpg b/static/uploads/images/gallery/cover_gallery_1753089310464_687c5ec6a96cbc0010159e31.jpg new file mode 100644 index 0000000..3c3927d Binary files /dev/null and b/static/uploads/images/gallery/cover_gallery_1753089310464_687c5ec6a96cbc0010159e31.jpg differ diff --git a/static/uploads/images/gallery/thumb_cover_gallery_1753089285039_687de3d19987640011091020.jpg b/static/uploads/images/gallery/thumb_cover_gallery_1753089285039_687de3d19987640011091020.jpg new file mode 100644 index 0000000..5fcde39 Binary files /dev/null and b/static/uploads/images/gallery/thumb_cover_gallery_1753089285039_687de3d19987640011091020.jpg differ diff --git a/static/uploads/images/gallery/thumb_cover_gallery_1753089310464_687c5ec6a96cbc0010159e31.jpg b/static/uploads/images/gallery/thumb_cover_gallery_1753089310464_687c5ec6a96cbc0010159e31.jpg new file mode 100644 index 0000000..5fcde39 Binary files /dev/null and b/static/uploads/images/gallery/thumb_cover_gallery_1753089310464_687c5ec6a96cbc0010159e31.jpg differ