fix: parse multipart uploads for shop-chat files and chat-room images

Same root cause as the shop logo fix: these two routes still expected
req.files.<field> (the removed express-form-data pattern) with no
multer middleware in front of them, so:
- POST /shop-chat/:shopId/messages/file always rejected with "فایل
  الزامی است" even when a valid image/video was sent.
- POST /chat-rooms room creation silently dropped room_image, and
  would lose all text fields (title, visibility, ...) whenever the
  request was multipart (i.e. whenever an image was attached).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
payacom
2026-10-28 17:01:43 +03:30
parent 6e25e1a38a
commit 0cfec31f52
4 changed files with 46 additions and 8 deletions

View File

@@ -225,14 +225,14 @@ const createRoom = async (req, res, next) => {
}
let imagePath = null
const imageFile = req.files?.room_image
const imageFile = req.file
if (imageFile) {
const uploadDir = path.join(__dirname, '../../../storage/chat-rooms')
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true })
}
// اسمِ فایل هیچ‌وقت مستقیم از ورودیِ کاربر (imageFile.name) گرفته نمی‌شه — فقط پسوندش
const uniqueFileName = `${Date.now()}-${crypto.randomUUID()}${path.extname(imageFile.name || '')}`
// اسمِ فایل هیچ‌وقت مستقیم از ورودیِ کاربر (imageFile.originalname) گرفته نمی‌شه — فقط پسوندش
const uniqueFileName = `${Date.now()}-${crypto.randomUUID()}${path.extname(imageFile.originalname || '')}`
const filePath = path.join(uploadDir, uniqueFileName)
await fs.move(imageFile.path, filePath)
await optimizeImageFile(filePath, { maxDimension: 800 })

View File

@@ -238,7 +238,7 @@ const sendShopChatFileMessage = async (req, res, next) => {
const { shop, isOwner, buyerId } = ctx
const { content, fileType } = req.body
const file = req.files?.file
const file = req.file
if (!file) return chatError(res, 422, 'فایل الزامی است')
if (!['image', 'video'].includes(fileType)) {
@@ -250,8 +250,8 @@ const sendShopChatFileMessage = async (req, res, next) => {
const uploadDir = path.join(__dirname, '../../../storage/shop-chat')
fs.ensureDirSync(uploadDir)
// اسمِ فایل هیچ‌وقت مستقیم از ورودیِ کاربر (file.name) گرفته نمی‌شه — فقط پسوندش
const uniqueFileName = `${Date.now()}-${crypto.randomUUID()}${path.extname(file.name || '')}`
// اسمِ فایل هیچ‌وقت مستقیم از ورودیِ کاربر (file.originalname) گرفته نمی‌شه — فقط پسوندش
const uniqueFileName = `${Date.now()}-${crypto.randomUUID()}${path.extname(file.originalname || '')}`
const filePath = path.join(uploadDir, uniqueFileName)
await fs.move(file.path, filePath, { overwrite: true })
if (fileType === 'image') {