From 1e7ae35c8a33c9265daba25cb1c77ef7808624c7 Mon Sep 17 00:00:00 2001 From: Kerboul Date: Wed, 12 Feb 2025 10:51:40 +0100 Subject: [PATCH] =?UTF-8?q?Ajouter=20une=20route=20pour=20r=C3=A9cup=C3=A9?= =?UTF-8?q?rer=20un=20fichier=20vid=C3=A9o=20par=20ID=20avec=20gestion=20d?= =?UTF-8?q?es=20erreurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/videoRoutes.js | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/routes/videoRoutes.js b/routes/videoRoutes.js index 7008ef9..8690bdc 100644 --- a/routes/videoRoutes.js +++ b/routes/videoRoutes.js @@ -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;