Merge pull request 'Ajout des fonctionnalités de traitement vidéo' (#2) from dev2 into main

Reviewed-on: https://gitea.kerboul.me/timelapse/timelapse-backend/pulls/2
This commit was merged in pull request #2.
This commit is contained in:
2025-02-11 21:51:35 +00:00
10 changed files with 227 additions and 70 deletions

4
db.js
View File

@@ -1,6 +1,6 @@
const { Client } = require('pg'); const { Client } = require('pg');
const local = false; const local = true;
// Connexion à la base de données PostgreSQL // Connexion à la base de données PostgreSQL
const client = new Client({ const client = new Client({
host: local ? 'mikoshi' : '172.30.0.2', host: local ? 'mikoshi' : '172.30.0.2',
@@ -16,7 +16,7 @@ function connectWithRetry() {
console.error('Erreur de connexion à la base de données:', err); console.error('Erreur de connexion à la base de données:', err);
setTimeout(connectWithRetry, 30000); // Réessayer après 30 secondes setTimeout(connectWithRetry, 30000); // Réessayer après 30 secondes
} else { } else {
console.log('Connecté à la base de données PostgreSQL.'); console.log('[DB] Connecté à la base de données PostgreSQL.');
} }
}); });
} }

View File

@@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router(); const router = express.Router();
const db = require('../db'); const db = require('../db');
const serverError = require('../utils/serverError'); const serverError = require('../utils/serverError');
const videoManager = require('../src/video/videoManager');
/** /**
* @swagger * @swagger

View File

@@ -60,6 +60,6 @@ app.get('/', (req, res) => {
// Démarrer le serveur // Démarrer le serveur
app.listen(port, () => { app.listen(port, () => {
console.log(`Serveur démarré sur http://localhost:${port}`); console.log(`[SERVER] Serveur démarré sur http://localhost:${port}`);
console.log(`Swagger documentation disponible sur http://localhost:${port}/api-docs`); console.log(`[SERVER] Swagger documentation disponible sur http://localhost:${port}/api-docs`);
}); });

View File

@@ -3,17 +3,20 @@ import path from 'path';
import storageManager from '../data/storageManager.js'; import storageManager from '../data/storageManager.js';
import fs from 'fs'; import fs from 'fs';
let localCounter = 0;
async function checkAndRemoveInvalidEntries() { async function checkAndRemoveInvalidEntries() {
console.log('Checking for invalid entries...'); localCounter = 0;
console.log('[INFO] Vérification et suppression des entrées invalides...');
try { try {
const measurementsRes = await db.query('SELECT id, path FROM measurements'); const measurementsRes = await db.query('SELECT id, path FROM measurements');
//console.log('Fetched measurements:', measurementsRes.rows); //console.log('Fetched measurements:', measurementsRes.rows);
for (const row of measurementsRes.rows) { for (const row of measurementsRes.rows) {
//console.log('Checking file path:', row.path); //console.log('Checking file path:', row.path);
if (!fs.existsSync(row.path)) { if (!fs.existsSync(row.path)) {
// Remove invalid entry
await db.query('DELETE FROM measurements WHERE id = $1', [row.id]); await db.query('DELETE FROM measurements WHERE id = $1', [row.id]);
console.log(`Deleted invalid measurement entry with id: ${row.id}`); console.log(`Deleted invalid measurement entry with id: ${row.id}`);
localCounter++;
} }
} }
@@ -26,8 +29,14 @@ async function checkAndRemoveInvalidEntries() {
// Remove the file if the entry does not exist // Remove the file if the entry does not exist
fs.unlinkSync(imagePath); fs.unlinkSync(imagePath);
console.log(`Deleted file at path: ${imagePath} as its database entry does not exist`); console.log(`Deleted file at path: ${imagePath} as its database entry does not exist`);
localCounter++; // Increment counter if entry is deleted
} }
} }
if (localCounter > 0) {
console.log(`[INFO] ${localCounter} entrées ont été modifiées`);
} else {
console.log('[INFO] Aucune entrée n\'a été modifiée.');
}
} catch (err) { } catch (err) {
console.error('Error checking and removing invalid entries:', err); console.error('Error checking and removing invalid entries:', err);
} }
@@ -36,7 +45,8 @@ async function checkAndRemoveInvalidEntries() {
// Run the check periodically // Run the check periodically
setInterval(checkAndRemoveInvalidEntries, 10000); // Every second console.log('[INFO] Activation du FileWatcher pour surveiller les fichiers invalides...')
setInterval(checkAndRemoveInvalidEntries, 10000); // Every 10 seconds
// Initial run // Initial run
checkAndRemoveInvalidEntries(); checkAndRemoveInvalidEntries();

View File

@@ -1,64 +1,80 @@
const fs = require('fs'); const fs = require('fs').promises;
const path = require('path'); const path = require('path');
const PROJECTS_DIR = path.join('.'); const PROJECTS_DIR = path.join('.');
function createFolder(name){ async function createFolder(name) {
const projectDir = path.join(PROJECTS_DIR, `${name}`); const projectDir = path.join(PROJECTS_DIR, `${name}`);
if (!fs.existsSync(projectDir)) { try {
fs.mkdirSync(projectDir, { recursive: true }); await fs.access(projectDir);
} catch (error) {
if (error.code === 'ENOENT') {
await fs.mkdir(projectDir, { recursive: true });
} else {
throw error;
}
} }
return projectDir; return projectDir;
} }
function deleteFolder(name){ async function deleteFolder(name) {
const projectDir = path.join(PROJECTS_DIR, `${name const projectDir = path.join(PROJECTS_DIR, `${name}`);
}`); try {
if (fs.existsSync await fs.access(projectDir);
(projectDir)) { await fs.rm(projectDir, { recursive: true, force: true });
fs.rmSync(projectDir, { recursive: true, force: true }); } catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
} }
} }
function scanAllImages(dir = 'storage') { async function scanAllImages(dir = 'storage') {
const projectDir = path.join(PROJECTS_DIR, dir); const projectDir = path.join(PROJECTS_DIR, dir);
let results = []; let results = [];
function scanDirectory(directory) { async function scanDirectory(directory) {
const files = fs.readdirSync(directory); const files = await fs.readdir(directory);
files.forEach(file => { for (const file of files) {
const filePath = path.join(directory, file); const filePath = path.join(directory, file);
const stat = fs.statSync(filePath); const stat = await fs.stat(filePath);
if (stat.isDirectory()) { if (stat.isDirectory()) {
scanDirectory(filePath); await scanDirectory(filePath);
} else if (file.endsWith('.jpg')) { } else if (file.endsWith('.jpg')) {
results.push(filePath); results.push(filePath);
} }
}); }
} }
scanDirectory(projectDir); await scanDirectory(projectDir);
return results; return results;
} }
function saveFile(filePath, content) { async function saveFile(filePath, content) {
// Ensure content is a buffer
if (Buffer.isBuffer(content)) { if (Buffer.isBuffer(content)) {
fs.writeFileSync(filePath, content); await fs.writeFile(filePath, content);
} else { } else {
throw new Error('Content must be a buffer'); throw new Error('Content must be a buffer');
} }
} }
function getFile(name){ async function getFile(name) {
const filePath = path.join(PROJECTS_DIR, `${name}`); const filePath = path.join(PROJECTS_DIR, `${name}`);
return fs return await fs.readFile(filePath);
.readFileSync(filePath);
} }
function deleteFile(name){ async function deleteFile(name) {
const filePath = path.join(PROJECTS_DIR, `${name}`); const filePath = path.join(PROJECTS_DIR, `${name}`);
if (fs.existsSync(filePath)) try {
fs.rmSync(filePath); await fs.access(filePath); // Vérifie si le fichier existe
await fs.rm(filePath); // Supprime le fichier
return `File ${filePath} deleted successfully.`;
} catch (error) {
if (error.code === 'ENOENT') {
return `File ${filePath} does not exist.`;
} else {
throw error; // Relance l'erreur si ce n'est pas une erreur de fichier introuvable
}
}
} }
module.exports = { module.exports = {

View File

@@ -7,13 +7,14 @@ async function uploadMeasureImage(image, projectId, orderId) {
const imagesDir = storageManager.createFolder(path.join(projectDir, 'images')); const imagesDir = storageManager.createFolder(path.join(projectDir, 'images'));
var imagePath = path.join(imagesDir, `${orderId}.jpg`); var imagePath = path.join(imagesDir, `${orderId}.jpg`);
storageManager.saveFile(imagePath, image.buffer); storageManager.saveFile(imagePath, image.buffer);
console.log("[FILE] uploadMeasureImage - Image saved to: " + imagePath);
return imagePath; return imagePath;
} }
async function getMeasureImage(projectId, orderId) { async function getMeasureImage(projectId, orderId) {
const projectPath = `${projectId}`; const projectPath = `${projectId}`;
const imagePath = `${projectPath}/${orderId}.jpg`; const imagePath = `${projectPath}/${orderId}.jpg`;
console.log("[FILE] getMeasureImage - Image path: " + imagePath);
return storageManager.getFile(imagePath); return storageManager.getFile(imagePath);
} }
@@ -21,6 +22,7 @@ async function getNextOrderId(projectId) {
const query = 'SELECT MAX(order_id) FROM public.measurements WHERE project_id = $1'; const query = 'SELECT MAX(order_id) FROM public.measurements WHERE project_id = $1';
const values = [projectId]; const values = [projectId];
const res = await db.query(query, values); const res = await db.query(query, values);
console.log("[DB] getNextOrderId - Max order_id: " + res.rows[0].max);
return res.rows[0].max + 1; return res.rows[0].max + 1;
} }
@@ -73,6 +75,13 @@ async function deleteMeasurement(id) {
return res.rows[0]; return res.rows[0];
} }
async function getPathFromIds(projectId, orderId) {
const query = 'SELECT path FROM public.measurements WHERE project_id = $1 AND order_id = $2';
const values = [projectId, orderId];
const res = await db.query(query, values);
return res.rows[0].path;
}
export { export {
uploadMeasureImage, uploadMeasureImage,
addMeasureToProject, addMeasureToProject,
@@ -83,5 +92,6 @@ export {
deleteMeasurement, deleteMeasurement,
getMeasureImage, getMeasureImage,
getMeasurementById, getMeasurementById,
updateMeasurementById updateMeasurementById,
getPathFromIds
} }

View File

@@ -6,17 +6,19 @@ function createProjectDirectory(projectId) {
storageManager.createFolder(projectPath); storageManager.createFolder(projectPath);
storageManager.createFolder(`${projectPath}/images`); storageManager.createFolder(`${projectPath}/images`);
storageManager.createFolder(`${projectPath}/videos`); storageManager.createFolder(`${projectPath}/videos`);
console.log("[FILE] createProjectDirectory : " + projectPath);
} }
function deleteProjectDirectory(projectId) { function deleteProjectDirectory(projectId) {
const projectPath = `${projectId}`; const projectPath = `${projectId}`;
storageManager.deleteFolder(projectPath); storageManager.deleteFolder(projectPath);
console.log("[FILE] deleteProjectDirectory : " + projectPath);
} }
async function getAllProjects() { async function getAllProjects() {
const query = 'SELECT * FROM public.projects'; const query = 'SELECT * FROM public.projects';
const res = await db.query(query); const res = await db.query(query);
console.log('getAllProjects:', res.rows); console.log("[DB] getAllProjects : ", res.rows);
return res.rows; return res.rows;
} }
@@ -24,6 +26,7 @@ async function getProjectById(projectId) {
const query = 'SELECT * FROM public.projects WHERE id = $1'; const query = 'SELECT * FROM public.projects WHERE id = $1';
const values = [projectId]; const values = [projectId];
const res = await db.query(query, values); const res = await db.query(query, values);
console.log("[DB] getProjectById : ", res.rows[0]);
return res.rows[0]; return res.rows[0];
} }
@@ -31,6 +34,7 @@ async function createProject(name, description, start_date, status) {
const query = 'INSERT INTO public.projects (name, description, start_date, status) VALUES ($1, $2, $3, $4) RETURNING *'; const query = 'INSERT INTO public.projects (name, description, start_date, status) VALUES ($1, $2, $3, $4) RETURNING *';
const values = [name, description, start_date, status]; const values = [name, description, start_date, status];
const res = await db.query(query, values); const res = await db.query(query, values);
console.log("[DB] createProject : ", res.rows[0]);
return res.rows[0]; return res.rows[0];
} }
@@ -38,12 +42,14 @@ async function editProjectById(projectID, name, description, startDate, status)
const query = 'UPDATE public.projects SET name = $1, description = $2, start_date = $3, status = $4 WHERE id = $5 RETURNING *'; const query = 'UPDATE public.projects SET name = $1, description = $2, start_date = $3, status = $4 WHERE id = $5 RETURNING *';
const values = [name, description, startDate, status, projectID]; const values = [name, description, startDate, status, projectID];
const res = await db.query(query, values); const res = await db.query(query, values);
console.log("[DB] editProjectById : ", res.rows[0]);
return res.rows[0]; return res.rows[0];
} }
async function deleteProjectById(projectId) { async function deleteProjectById(projectId) {
const query = 'DELETE FROM public.projects WHERE id = $1'; const query = 'DELETE FROM public.projects WHERE id = $1';
const values = [projectId]; const values = [projectId];
console.log("[DB] deleteProjectById : ", values);
await db.query(query, values); await db.query(query, values);
} }
@@ -51,6 +57,7 @@ async function getVideosByProjectId(projectId) {
const query = 'SELECT * FROM public.videos WHERE project_id = $1'; const query = 'SELECT * FROM public.videos WHERE project_id = $1';
const values = [projectId]; const values = [projectId];
const res = await db.query(query, values); const res = await db.query(query, values);
console.log("[DB] getVideosByProjectId : ", res.rows);
return res.rows; return res.rows;
} }
@@ -58,6 +65,7 @@ async function getMeasurementsByProjectId(projectId) {
const query = 'SELECT * FROM public.measurements WHERE project_id = $1'; const query = 'SELECT * FROM public.measurements WHERE project_id = $1';
const values = [projectId]; const values = [projectId];
const res = await db.query(query, values); const res = await db.query(query, values);
console.log("[DB] getMeasurementsByProjectId : ", res.rows);
return res.rows; return res.rows;
} }

111
src/video/videoManager.js Normal file
View File

@@ -0,0 +1,111 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const serverError = require('../../utils/serverError');
const db = require('../../db');
const storageManager = require('../data/storageManager');
const measureManager = require('../measure/measureManager');
const PROJECTS_DIR = path.join('.');
async function createVideoWithList(projectId, pathList) {
//pathList étant la liste des chemins déjà triés
const tempFile = path.join('temp.txt');
try {
// Trouver tous les fichiers image pour le projet donné
const workdir = path.join(PROJECTS_DIR, 'storage', `${projectId}`);
const dir = path.join(PROJECTS_DIR, 'storage', `${projectId}`, 'images');
console.log('dir:', dir);
const images = pathList;
console.log('images:', images);
// Trier les images numériquement
const sortedImages = images.sort((a, b) => {
const numA = parseInt(path.basename(a).match(/\d+/)[0], 10);
const numB = parseInt(path.basename(b).match(/\d+/)[0], 10);
return numA - numB;
});
// En déduire l'id de la première et dernière image utilisée
const firstImageId = parseInt(path.basename(sortedImages[0]).match(/\d+/)[0], 10);
const lastImageId = parseInt(path.basename(sortedImages[sortedImages.length - 1]).match(/\d+/)[0], 10);
console.log('firstImageId:', firstImageId);
console.log('lastImageId:', lastImageId);
// Créer un fichier temporaire pour la liste des images
fs.writeFileSync(tempFile, sortedImages.map(image => `file '${image}'`).join('\n'));
const frameRate = 10;
// le fichier final prend cette forme : {projectId}_{firstImageId}_{lastImageId}-{timestamp}.mp4
const timestamp = new Date().getTime();
const outputVideo = path.join(workdir, `${projectId}_${firstImageId}_${lastImageId}-${timestamp}.mp4`);
// Commande ffmpeg pour créer la vidéo
const ffmpegCommand = `ffmpeg -r ${frameRate} -f concat -safe 0 -i ${tempFile} -vsync vfr -pix_fmt yuv420p ${outputVideo}`;
console.log('Running ffmpeg command:', ffmpegCommand);
execSync(ffmpegCommand);
console.log('Video created successfully:', outputVideo);
return outputVideo;
} catch (error) {
console.error('Error creating video:', error);
serverError(error);
} finally {
// Supprimer le fichier temporaire
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
console.log('Temporary file deleted:', tempFile);
}
}
}
async function createVideo(projectId) {
const tempFile = path.join('temp.txt');
try {
// Trouver tous les fichiers image pour le projet donné
const workdir = path.join(PROJECTS_DIR, 'storage', `${projectId}`);
const dir = path.join(PROJECTS_DIR, 'storage', `${projectId}`, 'images');
console.log('dir:', dir);
const images = storageManager.scanAllImages(dir);
console.log('images:', images);
// Trier les images numériquement
const sortedImages = images.sort((a, b) => {
const numA = parseInt(path.basename(a).match(/\d+/)[0], 10);
const numB = parseInt(path.basename(b).match(/\d+/)[0], 10);
return numA - numB;
});
// En déduire l'id de la première et dernière image utilisée
const firstImageId = parseInt(path.basename(sortedImages[0]).match(/\d+/)[0], 10);
const lastImageId = parseInt(path.basename(sortedImages[sortedImages.length - 1]).match(/\d+/)[0], 10);
console.log('firstImageId:', firstImageId);
console.log('lastImageId:', lastImageId);
// Créer un fichier temporaire pour la liste des images
fs.writeFileSync(tempFile, sortedImages.map(image => `file '${image}'`).join('\n'));
const frameRate = 10;
const outputVideo = path.join(workdir, 'video.mp4');
// Commande ffmpeg pour créer la vidéo
const ffmpegCommand = `ffmpeg -r ${frameRate} -f concat -safe 0 -i ${tempFile} -vsync vfr -pix_fmt yuv420p ${outputVideo}`;
console.log('Running ffmpeg command:', ffmpegCommand);
execSync(ffmpegCommand);
console.log('Video created successfully:', outputVideo);
} catch (error) {
console.error('Error creating video:', error);
serverError(error);
} finally {
// Supprimer le fichier temporaire
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
console.log('Temporary file deleted:', tempFile);
}
}
}
module.exports = { createVideo, createVideoWithList };

View File

@@ -1,7 +1,9 @@
const storageManager = require('../src/data/storageManager'); const storageManager = require('../src/data/storageManager');
const videoManager = require('../src/video/videoManager');
const measureManager = require('../src/measure/measureManager');
const path = require('path'); const path = require('path');
console.log('Testing database functions...'); // console.log('Testing database functions...');
try { try {
storageManager.createFolder('test_folder'); storageManager.createFolder('test_folder');
@@ -16,4 +18,34 @@ function getSmileImage() {
return path.join(__dirname, '../sample/smile.png'); return path.join(__dirname, '../sample/smile.png');
} }
//test de lancement d'une création de vidéo sur le projet 1
// videoManager.createVideo(1).then(res => {
// console.log('3 - Video created:', res);
// }).catch(err => {
// console.error('Error creating video:', err);
// });
// async function run() {
// var Path = await measureManager.getPathFromIds(1, 1);
// console.log(Path);
// }
// run().catch(err => {
// console.error('Error:', err);
// });
var pathList = [
'storage/1/images/1.jpg',
'storage/1/images/10.jpg',
'storage/1/images/20.jpg',
'storage/1/images/30.jpg',
];
videoManager.createVideoWithList(1, pathList).then(res => {
console.log('3 - Video created:', res);
return storageManager.deleteFile(res);
}).then(res => {
console.log('4 - Video deleted:', res);
}).catch(err => {
console.error('Error:', err);
});
exports.getSmileImage = getSmileImage; exports.getSmileImage = getSmileImage;

View File

@@ -1,31 +0,0 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const serverError = require('../utils/serverError');
async function createVideo(projectId) {
const imageDir = `/storage/${projectId}`;
const outputVideo = `/storage/videos/output_${projectId}_video.mp4`;
const frameRate = 24;
const tempFile = `/storage/${projectId}/temp_file.txt`;
try {
const images = fs.readdirSync(imageDir).filter(file => file.endsWith('.jpg'));
if (images.length === 0) {
throw new Error('No images found for this project');
}
const tempFileContent = images.map(img => `file '${path.join(imageDir, img)}'`).join('\n');
fs.writeFileSync(tempFile, tempFileContent);
const ffmpegCommand = `ffmpeg -r ${frameRate} -f concat -safe 0 -i ${tempFile} -vsync vfr -pix_fmt yuv420p ${outputVideo}`;
execSync(ffmpegCommand);
fs.unlinkSync(tempFile);
return { message: 'Video created successfully', videoPath: outputVideo };
} catch (error) {
throw new Error(`Error creating video: ${error.message}`);
}
}
module.exports = { createVideo };