fix(shops): parse multipart logo upload on PATCH /shops/:id/logo
req.files.logo relied on an express-form-data style parser that was removed app-wide, so the route never had a multipart parser and every logo upload failed with "لطفاً لوگوی فروشگاه را انتخاب کنید" even though the client had already picked and sent an image. Add the same multer-based pattern already used for profile_image and shop-products image uploads. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -281,7 +281,7 @@ const updateShopLogo = async (req, res, next) => {
|
||||
if (error === 'NOT_FOUND') return shopError(res, 404, 'فروشگاه یافت نشد')
|
||||
if (error === 'FORBIDDEN') return shopError(res, 403, 'شما مالک این فروشگاه نیستید')
|
||||
|
||||
const logo = req.files?.logo
|
||||
const logo = req.file
|
||||
if (!logo) {
|
||||
return shopError(res, 422, 'لطفاً لوگوی فروشگاه را انتخاب کنید')
|
||||
}
|
||||
@@ -289,7 +289,7 @@ const updateShopLogo = async (req, res, next) => {
|
||||
const uploadDir = path.join(__dirname, '../../../storage/shops')
|
||||
fs.ensureDirSync(uploadDir)
|
||||
|
||||
const uniqueFileName = `${shop._id}-${Date.now()}${path.extname(logo.name)}`
|
||||
const uniqueFileName = `${shop._id}-${Date.now()}${path.extname(logo.originalname || '')}`
|
||||
const filePath = path.join(uploadDir, uniqueFileName)
|
||||
await fs.move(logo.path, filePath, { overwrite: true })
|
||||
await optimizeImageFile(filePath, { maxDimension: 800 })
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
const express = require('express')
|
||||
const multer = require('multer')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const {
|
||||
createDraftShop,
|
||||
getMyShops,
|
||||
@@ -29,6 +32,23 @@ const requireSubUserPermission = require('../../../middlewares/requireSubUserPer
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
// req.files مثلِ قبلاً توسطِ express-form-data پر میشد؛ بعدِ حذفِ اون middleware
|
||||
// (تداخل با multer توی posts/chat) این روت بدونِ parser جایگزین مونده بود — یعنی
|
||||
// آپلودِ لوگوی فروشگاه همیشه با «لطفاً لوگو را انتخاب کنید» رد میشد
|
||||
const shopLogoTmpDir = '/tmp/modstagram-uploads'
|
||||
if (!fs.existsSync(shopLogoTmpDir)) {
|
||||
fs.mkdirSync(shopLogoTmpDir, { recursive: true })
|
||||
}
|
||||
const shopLogoUpload = multer({
|
||||
storage: multer.diskStorage({
|
||||
destination: (req, file, cb) => cb(null, shopLogoTmpDir),
|
||||
filename: (req, file, cb) => {
|
||||
cb(null, `${Date.now()}-${Math.round(Math.random() * 1e9)}${path.extname(file.originalname || '')}`)
|
||||
}
|
||||
}),
|
||||
limits: { fileSize: 50 * 1024 * 1024 }
|
||||
}).single('logo')
|
||||
|
||||
router.post('/draft', [blockCheck], requireSubUserPermission('create_shop'), createDraftShop)
|
||||
router.get('/mine', [blockCheck], getMyShops)
|
||||
router.get('/analytics', [blockCheck], getShopAnalytics)
|
||||
@@ -44,7 +64,7 @@ router.post('/favorites/toggle', [blockCheck], toggleFavorite)
|
||||
router.get('/public/:id', getShopPublic)
|
||||
router.get('/:id', [blockCheck], getShopById)
|
||||
router.patch('/:id/basic', [blockCheck], requireSubUserPermission('create_shop'), updateShopBasic)
|
||||
router.patch('/:id/logo', [blockCheck], requireSubUserPermission('create_shop'), updateShopLogo)
|
||||
router.patch('/:id/logo', [blockCheck], requireSubUserPermission('create_shop'), shopLogoUpload, updateShopLogo)
|
||||
router.patch('/:id/shipping', [blockCheck], requireSubUserPermission('create_shop'), updateShopShipping)
|
||||
router.patch('/:id/location', [blockCheck], requireSubUserPermission('create_shop'), updateShopLocation)
|
||||
router.patch('/:id/contact', [blockCheck], requireSubUserPermission('create_shop'), updateShopContact)
|
||||
|
||||
Reference in New Issue
Block a user