Thêm tính năng hiển thị ảnh ở quản lí ảnh và media

This commit is contained in:
2026-06-08 18:08:49 +07:00
parent 9e2b4e636a
commit 965be4d0e7
3 changed files with 255 additions and 1 deletions
+83 -1
View File
@@ -583,13 +583,95 @@ router.get('/me/scenes', protect, async (req, res) => {
*/
router.get('/me/assets', protect, async (req, res) => {
try {
const assets = await Asset.find({ uploadedBy: req.user._id }).sort({ createdAt: -1 });
// Sử dụng Aggregation để lấy Asset kèm thông tin Scene và Parent Scene
const query = req.user.role === 'Chủ sở hữu' ? {} : { uploadedBy: req.user._id };
const assets = await Asset.aggregate([
{ $match: query },
{
$lookup: {
from: 'scenes', // Tên collection trong DB (thường là số nhiều)
localField: '_id',
foreignField: 'assetId',
as: 'linkedScene'
}
},
{ $unwind: { path: '$linkedScene', preserveNullAndEmptyArrays: true } },
{
$lookup: {
from: 'hotspots',
localField: 'linkedScene._id',
foreignField: 'target_scene_id',
as: 'incomingHotspots'
}
},
{
$lookup: {
from: 'scenes',
localField: 'incomingHotspots.parent_scene_id',
foreignField: '_id',
as: 'parentScenes'
}
},
{
$project: {
filePath: 0 // Bảo mật: Không trả về đường dẫn vật lý đầy đủ
}
},
{ $sort: { createdAt: -1 } }
]);
res.json(assets);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route DELETE /api/assets/:id
* @desc Xóa Asset + Xóa Scene liên quan (nếu có) + Xóa file vật lý
* @access Private (Chỉ người upload hoặc Admin)
*/
router.delete('/assets/:id', protect, async (req, res) => {
try {
const asset = await Asset.findById(req.params.id);
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 isAdmin = req.user.role === 'Chủ sở hữu' || req.user.role === 'admin';
if (!isOwner && !isAdmin) {
return res.status(403).json({ message: 'Bạn không có quyền xóa tập tin này' });
}
// 1. Tìm và xóa Scene liên quan nếu có
const linkedScene = await Scene.findOne({ assetId: asset._id });
if (linkedScene) {
// Xóa toàn bộ hotspot trỏ đến hoặc xuất phát từ scene này
await Hotspot.deleteMany({
$or: [
{ parent_scene_id: linkedScene._id },
{ target_scene_id: linkedScene._id }
]
});
await Scene.findByIdAndDelete(linkedScene._id);
}
// 2. Xóa file vật lý trên disk
if (fs.existsSync(asset.filePath)) {
fs.unlinkSync(asset.filePath);
}
// 3. Xóa Asset trong DB
await Asset.findByIdAndDelete(req.params.id);
res.json({ message: 'Đã xóa ảnh và các dữ liệu liên quan thành công' });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route GET /api/admin/users
* @desc Lấy toàn bộ danh sách người dùng (Chỉ Admin)