Sửa lỗi xóa ảnh trong quản lí ảnh và media
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user