From 9ec8ff73f3f7586e9f930adc6a96b60b9733b526 Mon Sep 17 00:00:00 2001 From: dakerboul Date: Mon, 10 Mar 2025 17:42:32 +0100 Subject: [PATCH] =?UTF-8?q?Modifier=20la=20route=20de=20rendu=20vid=C3=A9o?= =?UTF-8?q?=20pour=20utiliser=20POST,=20ajouter=20la=20gestion=20des=20err?= =?UTF-8?q?eurs=20et=20d=C3=A9marrer=20le=20processus=20de=20rendu=20vid?= =?UTF-8?q?=C3=A9o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/videoRoutes.js | 88 ++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 39 deletions(-) diff --git a/routes/videoRoutes.js b/routes/videoRoutes.js index 9428b45..dbe4e2e 100644 --- a/routes/videoRoutes.js +++ b/routes/videoRoutes.js @@ -256,50 +256,60 @@ router.delete('/videos/:id', (req, res) => { * 400: * description: Video not yet produced */ -router.get('/videos/file/:video_id', (req, res) => { +router.post('/videos/render/:video_id', async (req, res) => { const videoId = req.params.video_id; - const query = 'SELECT video_file, 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_file; - const stat = fs.statSync(videoPath); - const fileSize = stat.size; - const range = req.headers.range; + const query = 'SELECT measurement_ids, project_id, duration FROM public.videos WHERE id = $1'; - if (range) { - const parts = rangeParser(fileSize, range); - const start = parts[0].start; - const end = parts[0].end; - const chunksize = (end - start) + 1; - const file = fs.createReadStream(videoPath, { start, end }); - const head = { - 'Content-Range': `bytes ${start}-${end}/${fileSize}`, - 'Accept-Ranges': 'bytes', - 'Content-Length': chunksize, - 'Content-Type': 'video/mp4', - }; - res.writeHead(206, head); - file.pipe(res); - } else { - const head = { - 'Content-Length': fileSize, - 'Content-Type': 'video/mp4', - }; - res.writeHead(200, head); - fs.createReadStream(videoPath).pipe(res); - } + db.query(query, [videoId], async (err, results) => { + if (err) { + console.error('Error getting video:', err); + return serveFallbackVideo(res); + } + if (results.rows.length === 0) { + console.error('Video not found'); + return serveFallbackVideo(res); + } + + console.log('Video found:', results.rows[0]); + const duration = results.rows[0].duration; + + console.log('Rendering video:', videoId); + + const measurementIds = results.rows[0].measurement_ids; + const project_id = results.rows[0].project_id; + console.log('Measurement IDs:', measurementIds); + console.log('Project ID:', project_id); + + try { + const pathList = await measureManager.getPathList(measurementIds, project_id); + console.log('Path list:', pathList); + res.json({ message: 'Render process started' }); + + const videoFile = await videoManager.createVideoWithList(project_id, pathList, duration, videoId); + console.log('Video file:', videoFile); + + await videoManager.updateVideoFile(videoId, videoFile); + + console.log('Video rendering complete'); + } catch (err) { + console.error('Error during video rendering:', err); + return serveFallbackVideo(res); + } }); }); +function serveFallbackVideo(res) { + const videoPath = dbTester.getCatVideo(); + fs.access(videoPath, fs.constants.F_OK, (err) => { + if (err) { + console.error('Fallback video not found:', err); + return res.status(500).json({ error: 'Fallback video not found' }); + } + res.download(videoPath); + }); +} + + /** * @swagger * /videos/render/{video_id}: