full
This commit is contained in:
@@ -1,12 +1,25 @@
|
||||
const express = require('express');
|
||||
const { createPostValidationRules, createPost, getUserPosts, getUserPostsWeb, getPostByIdWeb } = require('../../../controllers/application/posts/postController');
|
||||
const { createPostValidationRules, createPost, updatePost, getUserPosts, getUserPostsWeb, getPostByIdWeb } = require('../../../controllers/application/posts/postController');
|
||||
const { toggleLike } = require('../../../controllers/application/posts/likeController');
|
||||
const {
|
||||
toggleBookmark,
|
||||
getBookmarks,
|
||||
sendPostToUsers,
|
||||
toggleFollow,
|
||||
getMyLikedPosts,
|
||||
getMyCommentedPosts,
|
||||
sharePost,
|
||||
} = require('../../../controllers/application/posts/engagementController');
|
||||
const { getPostAnalytics, getMyPostsAnalyticsList } = require('../../../controllers/application/posts/postAnalyticsController');
|
||||
const { reportPost } = require('../../../controllers/application/posts/reportController');
|
||||
const blockCheck = require('../../../middlewares/blockCheck');
|
||||
const auth = require('../../../middlewares/auth');
|
||||
const isHalfRegister = require('../../../middlewares/isHalfRegister');
|
||||
const requirePostExpertise = require('../../../middlewares/requirePostExpertise');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
const multer = require('multer');
|
||||
const { saveBase64PostFiles } = require('../../../utils/postBase64Files');
|
||||
|
||||
console.log('DEBUG: Starting to load routes/posts.js');
|
||||
|
||||
@@ -36,7 +49,10 @@ const storage = multer.diskStorage({
|
||||
cb(null, dir);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const filename = `${Date.now()}-${file.originalname}`;
|
||||
// اسمِ فایل هیچوقت مستقیم از ورودیِ کاربر (file.originalname) گرفته نمیشه — فقط
|
||||
// پسوندش؛ چون multer هم برای نوشتنِ فایل نهایتاً path.join(destination, filename)
|
||||
// میزنه، یه originalname مثل ../../../etc/passwd میتونست از پوشهی مقصد بیرون بزنه
|
||||
const filename = `${Date.now()}-${crypto.randomUUID()}${path.extname(file.originalname || '')}`;
|
||||
console.log('DEBUG: Generated filename for', file.originalname, ':', filename);
|
||||
cb(null, filename);
|
||||
},
|
||||
@@ -106,10 +122,10 @@ router.post(
|
||||
},
|
||||
blockCheck,
|
||||
(req, res, next) => {
|
||||
console.log('DEBUG: reached isHalfRegister');
|
||||
console.log('DEBUG: reached requirePostExpertise');
|
||||
next();
|
||||
},
|
||||
isHalfRegister,
|
||||
requirePostExpertise,
|
||||
(req, res, next) => {
|
||||
console.log('DEBUG: reached multer');
|
||||
console.log('DEBUG: Content-Length:', req.headers['content-length']);
|
||||
@@ -135,67 +151,128 @@ router.post(
|
||||
// روت جدید برای Base64
|
||||
router.post(
|
||||
'/create-base64',
|
||||
(req, res, next) => {
|
||||
console.log('DEBUG: reached auth for base64');
|
||||
console.log('DEBUG: Raw request headers:', JSON.stringify(req.headers, null, 2));
|
||||
next();
|
||||
},
|
||||
auth,
|
||||
(req, res, next) => {
|
||||
console.log('DEBUG: reached blockCheck for base64');
|
||||
next();
|
||||
},
|
||||
blockCheck,
|
||||
(req, res, next) => {
|
||||
console.log('DEBUG: reached isHalfRegister for base64');
|
||||
next();
|
||||
},
|
||||
isHalfRegister,
|
||||
(req, res, next) => {
|
||||
console.log('DEBUG: Request body (base64):', JSON.stringify(req.body, null, 2));
|
||||
next();
|
||||
},
|
||||
requirePostExpertise,
|
||||
createPostValidationRules(),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { files, caption } = req.body;
|
||||
console.log('DEBUG: Received files (base64):', files.length);
|
||||
console.log('DEBUG: Received caption:', caption);
|
||||
const {
|
||||
files,
|
||||
caption,
|
||||
tagged_user_ids,
|
||||
linked_course_id,
|
||||
linked_project_id,
|
||||
linked_billboard_id,
|
||||
linked_shop_id,
|
||||
linked_shop_product_id,
|
||||
linked_booking_id,
|
||||
country_id,
|
||||
province_id,
|
||||
city_id,
|
||||
} = req.body;
|
||||
|
||||
// تبدیل Base64 به فایل
|
||||
const savedFiles = await Promise.all(
|
||||
files.map(async (file, index) => {
|
||||
const buffer = Buffer.from(file.data.split(',')[1], 'base64');
|
||||
const dir = file.type.startsWith('video/') ? videoDir : imageDir;
|
||||
const filename = `${Date.now()}-${index}-${file.name}`;
|
||||
const filePath = path.join(dir, filename);
|
||||
console.log('DEBUG: Saving file:', filePath);
|
||||
await fs.promises.writeFile(filePath, buffer);
|
||||
return {
|
||||
path: filePath,
|
||||
filename,
|
||||
mimetype: file.type,
|
||||
};
|
||||
})
|
||||
);
|
||||
console.log('DEBUG: create-base64', {
|
||||
filesCount: Array.isArray(files) ? files.length : 0,
|
||||
captionLen: caption ? String(caption).length : 0,
|
||||
fileTypes: Array.isArray(files)
|
||||
? files.map((f) => f?.type).filter(Boolean)
|
||||
: [],
|
||||
});
|
||||
|
||||
const savedFiles = await saveBase64PostFiles(files, imageDir, videoDir);
|
||||
|
||||
// فراخوانی createPost با فرمت مشابه
|
||||
req.files = savedFiles;
|
||||
req.body = { caption };
|
||||
console.log('DEBUG: Prepared files for createPost:', savedFiles);
|
||||
console.log('DEBUG: Prepared body for createPost:', req.body);
|
||||
req.body = {
|
||||
caption,
|
||||
tagged_user_ids,
|
||||
linked_course_id,
|
||||
linked_project_id,
|
||||
linked_billboard_id,
|
||||
linked_shop_id,
|
||||
linked_shop_product_id,
|
||||
linked_booking_id,
|
||||
country_id,
|
||||
province_id,
|
||||
city_id,
|
||||
};
|
||||
await createPost(req, res);
|
||||
} catch (err) {
|
||||
console.error('DEBUG: Base64 processing error:', err.message);
|
||||
if (!res.headersSent) {
|
||||
res.status(400).json({
|
||||
error: true,
|
||||
message: `خطای پردازش فایلها: ${err.message}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
async function handleUpdateBase64(req, res) {
|
||||
try {
|
||||
const {
|
||||
postId,
|
||||
files,
|
||||
caption,
|
||||
tagged_user_ids,
|
||||
keep_files,
|
||||
media_order,
|
||||
} = req.body;
|
||||
|
||||
console.log('DEBUG: update-base64', {
|
||||
postId,
|
||||
filesCount: Array.isArray(files) ? files.length : 0,
|
||||
keepCount: Array.isArray(keep_files)
|
||||
? keep_files.length
|
||||
: keep_files
|
||||
? 1
|
||||
: 0,
|
||||
});
|
||||
|
||||
const savedFiles = await saveBase64PostFiles(files, imageDir, videoDir, {
|
||||
allowEmpty: true,
|
||||
});
|
||||
|
||||
req.files = savedFiles;
|
||||
req.body = {
|
||||
postId,
|
||||
caption,
|
||||
tagged_user_ids,
|
||||
keep_files,
|
||||
media_order,
|
||||
};
|
||||
await updatePost(req, res);
|
||||
} catch (err) {
|
||||
console.error('DEBUG: update-base64 error:', err.message);
|
||||
if (!res.headersSent) {
|
||||
res.status(400).json({
|
||||
error: true,
|
||||
message: `خطای پردازش فایلها: ${err.message}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
router.post('/update-base64', auth, blockCheck, handleUpdateBase64);
|
||||
// مسیر جایگزین برای سرورهایی که روت قبلی را کش کردهاند
|
||||
router.put('/update-base64', auth, blockCheck, handleUpdateBase64);
|
||||
router.post('/:postId/update-base64', auth, blockCheck, (req, res) => {
|
||||
req.body = { ...(req.body || {}), postId: req.params.postId || req.body?.postId };
|
||||
return handleUpdateBase64(req, res);
|
||||
});
|
||||
|
||||
router.post('/like', [auth, blockCheck], toggleLike);
|
||||
router.post('/bookmark', [auth, blockCheck], toggleBookmark);
|
||||
router.get('/bookmarks', [auth, blockCheck], getBookmarks);
|
||||
router.get('/my-likes', [auth, blockCheck], getMyLikedPosts);
|
||||
router.get('/my-comments', [auth, blockCheck], getMyCommentedPosts);
|
||||
router.post('/send', [auth, blockCheck], sendPostToUsers);
|
||||
router.post('/share', [auth, blockCheck], sharePost);
|
||||
router.post('/follow', [auth, blockCheck], toggleFollow);
|
||||
router.post('/report', [auth, blockCheck], reportPost);
|
||||
router.get('/analytics/mine', [auth, blockCheck], getMyPostsAnalyticsList)
|
||||
router.get('/:postId/analytics', [auth, blockCheck], getPostAnalytics);
|
||||
router.get('/web/:postId', getPostByIdWeb);
|
||||
router.get('/user-posts', [auth], getUserPosts);
|
||||
router.get('/user-posts/web', getUserPostsWeb);
|
||||
|
||||
Reference in New Issue
Block a user