Modifier la route de rendu vidéo pour utiliser POST, ajouter la gestion des erreurs et démarrer le processus de rendu vidéo
This commit is contained in:
@@ -256,50 +256,60 @@ router.delete('/videos/:id', (req, res) => {
|
|||||||
* 400:
|
* 400:
|
||||||
* description: Video not yet produced
|
* 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 videoId = req.params.video_id;
|
||||||
const query = 'SELECT video_file, status FROM public.videos WHERE id = $1';
|
const query = 'SELECT measurement_ids, project_id, duration 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;
|
|
||||||
|
|
||||||
if (range) {
|
db.query(query, [videoId], async (err, results) => {
|
||||||
const parts = rangeParser(fileSize, range);
|
if (err) {
|
||||||
const start = parts[0].start;
|
console.error('Error getting video:', err);
|
||||||
const end = parts[0].end;
|
return serveFallbackVideo(res);
|
||||||
const chunksize = (end - start) + 1;
|
}
|
||||||
const file = fs.createReadStream(videoPath, { start, end });
|
if (results.rows.length === 0) {
|
||||||
const head = {
|
console.error('Video not found');
|
||||||
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
return serveFallbackVideo(res);
|
||||||
'Accept-Ranges': 'bytes',
|
}
|
||||||
'Content-Length': chunksize,
|
|
||||||
'Content-Type': 'video/mp4',
|
console.log('Video found:', results.rows[0]);
|
||||||
};
|
const duration = results.rows[0].duration;
|
||||||
res.writeHead(206, head);
|
|
||||||
file.pipe(res);
|
console.log('Rendering video:', videoId);
|
||||||
} else {
|
|
||||||
const head = {
|
const measurementIds = results.rows[0].measurement_ids;
|
||||||
'Content-Length': fileSize,
|
const project_id = results.rows[0].project_id;
|
||||||
'Content-Type': 'video/mp4',
|
console.log('Measurement IDs:', measurementIds);
|
||||||
};
|
console.log('Project ID:', project_id);
|
||||||
res.writeHead(200, head);
|
|
||||||
fs.createReadStream(videoPath).pipe(res);
|
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
|
* @swagger
|
||||||
* /videos/render/{video_id}:
|
* /videos/render/{video_id}:
|
||||||
|
|||||||
Reference in New Issue
Block a user