Renommer le gestionnaire de stockage et mettre à jour les références dans les fichiers concernés. Supprimer les fichiers obsolètes et ajouter un nouveau fichier de gestion de stockage.
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const projectManager = require('../src/project/projectManager');
|
||||
const serverError = require('../utils/serverError');
|
||||
const database_manager = require('../src/database/database_manager');
|
||||
const storage_manager = require('../src/data/storage_manager');
|
||||
|
||||
router.get('/projects', async (req, res) => {
|
||||
try {
|
||||
const projects = await projectManager.getAllProjects();
|
||||
const projects = await database_manager.project.get_all_projects();
|
||||
res.json(projects);
|
||||
} catch (error) {
|
||||
serverError.sendError('Error getting all projects:', res, error, 500);
|
||||
@@ -18,7 +19,7 @@ router.get('/projects/:id', async (req, res) => {
|
||||
return res.status(400).json({ error: 'Invalid project ID' });
|
||||
}
|
||||
try {
|
||||
const project = await projectManager.getProjectById(projectId);
|
||||
const project = await database_manager.project.get_project_by_id(projectId);
|
||||
res.json(project);
|
||||
} catch (error) {
|
||||
serverError.sendError('Error getting project by ID:', res, error, 500);
|
||||
@@ -31,7 +32,10 @@ router.get('/projects/:id/videos', async (req, res) => {
|
||||
return res.status(400).json({ error: 'Invalid project ID' });
|
||||
}
|
||||
try {
|
||||
const videos = await projectManager.getVideosByProjectId(projectId);
|
||||
const videos = await database_manager.video.get_videos_by_project_id(projectId);
|
||||
if (videos.length === 0) {
|
||||
return res.status(404).json({ error: 'No videos found for this project' });
|
||||
}
|
||||
res.json(videos);
|
||||
} catch (error) {
|
||||
serverError.sendError('Error getting videos by project ID:', res, error, 500);
|
||||
@@ -44,7 +48,10 @@ router.get('/projects/:id/measurements', async (req, res) => {
|
||||
return res.status(400).json({ error: 'Invalid project ID' });
|
||||
}
|
||||
try {
|
||||
const measurements = await projectManager.getMeasurementsByProjectId(projectId);
|
||||
const measurements = await database_manager.measurement.get_measurements_by_project_id(projectId);
|
||||
if (measurements.length === 0) {
|
||||
return res.status(404).json({ error: 'No measurements found for this project' });
|
||||
}
|
||||
res.json(measurements);
|
||||
} catch (error) {
|
||||
serverError.sendError('Error getting measurements by project ID:', res, error, 500);
|
||||
@@ -57,8 +64,10 @@ router.post('/projects', async (req, res) => {
|
||||
return res.status(400).json({ error: 'Name and description are required' });
|
||||
}
|
||||
try {
|
||||
const project = await projectManager.createProject(name, description, new Date(), 0);
|
||||
projectManager.createProjectDirectory(project.id);
|
||||
const date = new Date();
|
||||
const default_status = 0;
|
||||
const project = await database_manager.project.create_project(name, description, date, default_status);
|
||||
storage_manager.createProjectDirectory(project.id);
|
||||
res.status(201).json({ message: 'Project added successfully', id: project.id });
|
||||
} catch (error) {
|
||||
serverError.sendError('Error creating project:', res, error, 500);
|
||||
@@ -71,8 +80,8 @@ router.delete('/projects/:id', async (req, res) => {
|
||||
return res.status(400).json({ error: 'Invalid project ID' });
|
||||
}
|
||||
try {
|
||||
projectManager.deleteProjectDirectory(projectId);
|
||||
projectManager.deleteProjectById(projectId);
|
||||
storage_manager.deleteProjectDirectory(projectId);
|
||||
await database_manager.project.delete_project(projectId);
|
||||
res.status(200).json({ message: 'Project deleted successfully', id: projectId });
|
||||
} catch (error) {
|
||||
serverError.sendError('Error deleting project:', res, error, 500);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const db = require('../../db.js');
|
||||
const storageManager = require('../data/storageManager.js');
|
||||
const storage_manager = require('./storage_manager.js');
|
||||
const fs = require('fs');
|
||||
|
||||
let localCounter = 0;
|
||||
@@ -16,7 +16,7 @@ async function checkAndRemoveInvalidEntries() {
|
||||
}
|
||||
}
|
||||
|
||||
const allImages = await storageManager.scanAllImages();
|
||||
const allImages = await storage_manager.scanAllImages();
|
||||
for (const imagePath of allImages) {
|
||||
const entryRes = await db.query('SELECT id FROM measurements WHERE path = $1', [imagePath]);
|
||||
if (entryRes.rows.length === 0) {
|
||||
|
||||
@@ -89,11 +89,27 @@ async function deleteFile(name) {
|
||||
}
|
||||
}
|
||||
|
||||
function createProjectDirectory(projectId) {
|
||||
const projectPath = `${projectId}`;
|
||||
createFolder(projectPath);
|
||||
createFolder(`${projectPath}/images`);
|
||||
createFolder(`${projectPath}/videos`);
|
||||
console.log("[FILE] createProjectDirectory : " + projectPath);
|
||||
}
|
||||
|
||||
function deleteProjectDirectory(projectId) {
|
||||
const projectPath = `${projectId}`;
|
||||
deleteFolder(projectPath);
|
||||
console.log("[FILE] deleteProjectDirectory : " + projectPath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createFolder,
|
||||
deleteFolder,
|
||||
scanAllImages,
|
||||
saveFile,
|
||||
getFile,
|
||||
deleteFile
|
||||
deleteFile,
|
||||
createProjectDirectory,
|
||||
deleteProjectDirectory,
|
||||
};
|
||||
@@ -126,19 +126,126 @@ init_function()
|
||||
.then(() => console.log('Database initialization completed.'))
|
||||
.catch(err => console.error('Error during database initialization:', err));
|
||||
|
||||
|
||||
const database_manager = {
|
||||
createVideoProject: async (project_id, measurement_ids, name, resolution, duration) => {
|
||||
const query = `INSERT INTO public.videos (project_id, measurement_ids, name, resolution, duration) VALUES ($1, $2, $3, $4, $5) RETURNING id`;
|
||||
const values = [project_id, measurement_ids, name, resolution, duration];
|
||||
// Fonctions pour les projets
|
||||
function handleDatabaseOperation(operation) {
|
||||
return async (...args) => {
|
||||
try {
|
||||
const result = await db.query(query, values);
|
||||
return result.rows[0].id;
|
||||
return await operation(...args);
|
||||
} catch (err) {
|
||||
console.error('Error creating video project:', err);
|
||||
console.error(`Error during database operation: ${operation.name}`, err);
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const project = {
|
||||
get_all_projects: handleDatabaseOperation(async () => {
|
||||
const query = `SELECT * FROM projects;`;
|
||||
return (await db.query(query)).rows;
|
||||
}),
|
||||
get_project_by_id: handleDatabaseOperation(async (id) => {
|
||||
const query = `SELECT * FROM projects WHERE id = $1;`;
|
||||
return (await db.query(query, [id])).rows[0];
|
||||
}),
|
||||
create_project: handleDatabaseOperation(async (name, description, start_date, status) => {
|
||||
const query = `INSERT INTO projects (name, description, start_date, status) VALUES ($1, $2, $3, $4) RETURNING *;`;
|
||||
return (await db.query(query, [name, description, start_date, status])).rows[0];
|
||||
}),
|
||||
edit_project_by_id: handleDatabaseOperation(async (id, updates) => {
|
||||
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
||||
const values = [id, ...Object.values(updates)];
|
||||
const query = `UPDATE projects SET ${fields} WHERE id = $1 RETURNING *;`;
|
||||
return (await db.query(query, values)).rows[0];
|
||||
}),
|
||||
delete_project_by_id: handleDatabaseOperation(async (id) => {
|
||||
const query = `DELETE FROM projects WHERE id = $1;`;
|
||||
await db.query(query, [id]);
|
||||
})
|
||||
};
|
||||
|
||||
module.exports = database_manager;
|
||||
const measurement = {
|
||||
get_all_measurements: handleDatabaseOperation(async () => {
|
||||
const query = `SELECT * FROM measurements;`;
|
||||
return (await db.query(query)).rows;
|
||||
}),
|
||||
get_measurement_by_id: handleDatabaseOperation(async (id) => {
|
||||
const query = `SELECT * FROM measurements WHERE id = $1;`;
|
||||
return (await db.query(query, [id])).rows[0];
|
||||
}),
|
||||
get_measurement_by_project_and_order_id: handleDatabaseOperation(async (project_id, order_id) => {
|
||||
const query = `SELECT * FROM measurements WHERE project_id = $1 AND order_id = $2;`;
|
||||
return (await db.query(query, [project_id, order_id])).rows[0];
|
||||
}),
|
||||
get_measurements_by_project_id: handleDatabaseOperation(async (project_id) => {
|
||||
const query = `SELECT * FROM measurements WHERE project_id = $1;`;
|
||||
return (await db.query(query, [project_id])).rows;
|
||||
}),
|
||||
create_measurement: handleDatabaseOperation(async (project_id, timestamp, path, temperature, humidity, order_id) => {
|
||||
const query = `INSERT INTO measurements (project_id, timestamp, path, temperature, humidity, order_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;`;
|
||||
return (await db.query(query, [project_id, timestamp, path, temperature, humidity, order_id])).rows[0];
|
||||
}),
|
||||
edit_measurement_by_id: handleDatabaseOperation(async (id, updates) => {
|
||||
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
||||
const values = [id, ...Object.values(updates)];
|
||||
const query = `UPDATE measurements SET ${fields} WHERE id = $1 RETURNING *;`;
|
||||
return (await db.query(query, values)).rows[0];
|
||||
}),
|
||||
delete_measurement_by_id: handleDatabaseOperation(async (id) => {
|
||||
const query = `DELETE FROM measurements WHERE id = $1;`;
|
||||
await db.query(query, [id]);
|
||||
})
|
||||
};
|
||||
|
||||
const video = {
|
||||
get_all_videos: handleDatabaseOperation(async () => {
|
||||
const query = `SELECT * FROM videos;`;
|
||||
return (await db.query(query)).rows;
|
||||
}),
|
||||
get_video_by_id: handleDatabaseOperation(async (id) => {
|
||||
const query = `SELECT * FROM videos WHERE id = $1;`;
|
||||
return (await db.query(query, [id])).rows[0];
|
||||
}),
|
||||
get_videos_by_project_id: handleDatabaseOperation(async (project_id) => {
|
||||
const query = `SELECT * FROM videos WHERE project_id = $1;`;
|
||||
return (await db.query(query, [project_id])).rows;
|
||||
}),
|
||||
create_video: handleDatabaseOperation(async (project_id, measurement_ids, video_file, resolution, duration, status, name, progress, started_at, updated_at, eta) => {
|
||||
const query = `INSERT INTO videos (project_id, measurement_ids, video_file, resolution, duration, status, name, progress, started_at, updated_at, eta) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *;`;
|
||||
return (await db.query(query, [project_id, measurement_ids, video_file, resolution, duration, status, name, progress, started_at, updated_at, eta])).rows[0];
|
||||
}),
|
||||
edit_video_by_id: handleDatabaseOperation(async (id, updates) => {
|
||||
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
||||
const values = [id, ...Object.values(updates)];
|
||||
const query = `UPDATE videos SET ${fields} WHERE id = $1 RETURNING *;`;
|
||||
return (await db.query(query, values)).rows[0];
|
||||
}),
|
||||
delete_video_by_id: handleDatabaseOperation(async (id) => {
|
||||
const query = `DELETE FROM videos WHERE id = $1;`;
|
||||
await db.query(query, [id]);
|
||||
})
|
||||
};
|
||||
|
||||
const camera = {
|
||||
get_camera: handleDatabaseOperation(async () => {
|
||||
const query = `SELECT * FROM camera;`;
|
||||
return (await db.query(query)).rows;
|
||||
}),
|
||||
edit_camera: handleDatabaseOperation(async (id, updates) => {
|
||||
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
||||
const values = [id, ...Object.values(updates)];
|
||||
const query = `UPDATE camera SET ${fields} WHERE id = $1 RETURNING *;`;
|
||||
return (await db.query(query, values)).rows[0];
|
||||
}),
|
||||
delete_camera: handleDatabaseOperation(async (id) => {
|
||||
const query = `DELETE FROM camera WHERE id = $1;`;
|
||||
await db.query(query, [id]);
|
||||
})
|
||||
};
|
||||
|
||||
// Export des modules
|
||||
module.exports = {
|
||||
project,
|
||||
measurement,
|
||||
video,
|
||||
camera
|
||||
};
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
const db = require('../../db.js');
|
||||
const path = require('path');
|
||||
const storageManager = require('../data/storageManager.js');
|
||||
const storage_manager = require('../data/storage_manager.js');
|
||||
|
||||
async function uploadMeasureImage(image, projectId, orderId) {
|
||||
try {
|
||||
// Ensure that folder creation and file saving are awaited
|
||||
const projectDir = await storageManager.createFolder('./storage/' + projectId.toString());
|
||||
const imagesDir = await storageManager.createFolder(path.join(projectDir, 'images'));
|
||||
const projectDir = await storage_manager.createFolder('./storage/' + projectId.toString());
|
||||
const imagesDir = await storage_manager.createFolder(path.join(projectDir, 'images'));
|
||||
const imagePath = path.join(imagesDir, `${orderId}.jpg`);
|
||||
|
||||
// Save the file and await completion
|
||||
await storageManager.saveFile(imagePath, image.buffer);
|
||||
await storage_manager.saveFile(imagePath, image.buffer);
|
||||
|
||||
console.log("[FILE] uploadMeasureImage - Image saved to: " + imagePath);
|
||||
return imagePath;
|
||||
@@ -24,7 +24,7 @@ async function getMeasureImage(projectId, orderId) {
|
||||
const projectPath = `${projectId}`;
|
||||
const imagePath = `${projectPath}/${orderId}.jpg`;
|
||||
console.log("[FILE] getMeasureImage - Image path: " + imagePath);
|
||||
return storageManager.getFile(imagePath);
|
||||
return storage_manager.getFile(imagePath);
|
||||
}
|
||||
|
||||
async function getNextOrderId(projectId) {
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
const storageManager = require('../data/storageManager.js');
|
||||
const db = require('../../db.js');
|
||||
|
||||
function createProjectDirectory(projectId) {
|
||||
const projectPath = `${projectId}`;
|
||||
storageManager.createFolder(projectPath);
|
||||
storageManager.createFolder(`${projectPath}/images`);
|
||||
storageManager.createFolder(`${projectPath}/videos`);
|
||||
console.log("[FILE] createProjectDirectory : " + projectPath);
|
||||
}
|
||||
|
||||
function deleteProjectDirectory(projectId) {
|
||||
const projectPath = `${projectId}`;
|
||||
storageManager.deleteFolder(projectPath);
|
||||
console.log("[FILE] deleteProjectDirectory : " + projectPath);
|
||||
}
|
||||
|
||||
async function getAllProjects() {
|
||||
const query = 'SELECT * FROM public.projects';
|
||||
const res = await db.query(query);
|
||||
//console.log("[DB] getAllProjects : ", res.rows);
|
||||
return res.rows;
|
||||
}
|
||||
|
||||
async function getProjectById(projectId) {
|
||||
const query = 'SELECT * FROM public.projects WHERE id = $1';
|
||||
const values = [projectId];
|
||||
const res = await db.query(query, values);
|
||||
//console.log("[DB] getProjectById : ", res.rows[0]);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
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 values = [name, description, start_date, status];
|
||||
const res = await db.query(query, values);
|
||||
//console.log("[DB] createProject : ", res.rows[0]);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
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 values = [name, description, startDate, status, projectID];
|
||||
const res = await db.query(query, values);
|
||||
//console.log("[DB] editProjectById : ", res.rows[0]);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async function deleteProjectById(projectId) {
|
||||
const query = 'DELETE FROM public.projects WHERE id = $1';
|
||||
const values = [projectId];
|
||||
//console.log("[DB] deleteProjectById : ", values);
|
||||
await db.query(query, values);
|
||||
}
|
||||
|
||||
async function getVideosByProjectId(projectId) {
|
||||
const query = 'SELECT * FROM public.videos WHERE project_id = $1';
|
||||
const values = [projectId];
|
||||
const res = await db.query(query, values);
|
||||
//console.log("[DB] getVideosByProjectId : ", res.rows);
|
||||
return res.rows;
|
||||
}
|
||||
|
||||
async function getMeasurementsByProjectId(projectId) {
|
||||
const query = 'SELECT * FROM public.measurements WHERE project_id = $1';
|
||||
const values = [projectId];
|
||||
const res = await db.query(query, values);
|
||||
//console.log("[DB] getMeasurementsByProjectId : ", res.rows);
|
||||
return res.rows;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createProjectDirectory,
|
||||
deleteProjectDirectory,
|
||||
getAllProjects,
|
||||
getProjectById,
|
||||
createProject,
|
||||
editProjectById,
|
||||
deleteProjectById,
|
||||
getVideosByProjectId,
|
||||
getMeasurementsByProjectId
|
||||
};
|
||||
@@ -4,7 +4,7 @@ const { spawn } = require('child_process');
|
||||
|
||||
const serverError = require('../../utils/serverError');
|
||||
const db = require('../../db');
|
||||
const storageManager = require('../data/storageManager');
|
||||
const storage_manager = require('../data/storage_manager');
|
||||
|
||||
const PROJECTS_DIR = path.join('.');
|
||||
|
||||
@@ -219,7 +219,7 @@ async function createVideo(projectId) {
|
||||
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);
|
||||
const images = storage_manager.scanAllImages(dir);
|
||||
console.log('images:', images);
|
||||
|
||||
// Trier les images numériquement
|
||||
|
||||
Reference in New Issue
Block a user