Sửa lỗi xóa ảnh trong quản lí ảnh và media

This commit is contained in:
2026-06-08 18:19:59 +07:00
parent 965be4d0e7
commit f15e53c4e5
4 changed files with 88 additions and 12 deletions
+2 -2
View File
@@ -638,7 +638,7 @@ router.delete('/assets/:id', protect, async (req, res) => {
if (!asset) return res.status(404).json({ message: 'Ảnh không tồn tại' });
// Kiểm tra quyền: Người upload hoặc Admin (Chủ sở hữu)
const isOwner = asset.uploadedBy.toString() === req.user._id.toString();
const isOwner = asset.uploadedBy && asset.uploadedBy.toString() === req.user._id.toString();
const isAdmin = req.user.role === 'Chủ sở hữu' || req.user.role === 'admin';
if (!isOwner && !isAdmin) {
@@ -659,7 +659,7 @@ router.delete('/assets/:id', protect, async (req, res) => {
}
// 2. Xóa file vật lý trên disk
if (fs.existsSync(asset.filePath)) {
if (asset.filePath && fs.existsSync(asset.filePath)) {
fs.unlinkSync(asset.filePath);
}