Ajouter une route pour récupérer un fichier vidéo par ID avec gestion des erreurs
This commit is contained in:
@@ -227,4 +227,55 @@ router.delete('/videos/:id', (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /videos/file/{video_id}:
|
||||||
|
* get:
|
||||||
|
* summary: Retrieve a video by video ID
|
||||||
|
* parameters:
|
||||||
|
* - in: path
|
||||||
|
* name: video_id
|
||||||
|
* required: true
|
||||||
|
* schema:
|
||||||
|
* type: string
|
||||||
|
* description: The video ID
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: A video file
|
||||||
|
* content:
|
||||||
|
* application/octet-stream:
|
||||||
|
* schema:
|
||||||
|
* type: string
|
||||||
|
* format: binary
|
||||||
|
* 404:
|
||||||
|
* description: Video not found
|
||||||
|
* 400:
|
||||||
|
* description: Video not yet produced
|
||||||
|
*/
|
||||||
|
router.get('/videos/file/:video_id', (req, res) => {
|
||||||
|
const videoId = req.params.video_id;
|
||||||
|
const query = 'SELECT video_path, status FROM public.videos WHERE id = $1';
|
||||||
|
db.query(query, [videoId], (err, results) => {
|
||||||
|
if (err) {
|
||||||
|
return serverError.sendError('Error getting video:', res, err);
|
||||||
|
}
|
||||||
|
if (results.rows.length === 0) {
|
||||||
|
return res.status(404).json({ error: 'Video not found' });
|
||||||
|
}
|
||||||
|
const video = results.rows[0];
|
||||||
|
if (video.status === 0) {
|
||||||
|
return res.status(400).json({ error: 'Video not yet produced' });
|
||||||
|
}
|
||||||
|
const videoPath = video.video_path;
|
||||||
|
fs.access(videoPath, fs.constants.F_OK, (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Video not found:', err);
|
||||||
|
return res.status(404).json({ error: 'Video not found' });
|
||||||
|
}
|
||||||
|
res.download(videoPath);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user