Ajout de la gestion des fichiers avec création et suppression de dossiers, sauvegarde et récupération d'images pour les projets

This commit is contained in:
2025-02-11 16:45:13 +01:00
parent d2a24b22ce
commit bccf3ddf23
9 changed files with 391 additions and 88 deletions

View File

@@ -6,9 +6,12 @@ const fs = require('fs');
const ffmpeg = require('../ffmpeg');
const fileUtils = require('../utils/fileUtils');
const serverError = require('../utils/serverError');
const multer = require('multer');
const video = require('../utils/video');
const dbTester = require('../test/dbTester');
const dbTester = require('../test/tester');
const cors = require('cors'); // Import the cors package
const projectManager = require('../src/project/projectManager.js');
const measureManager = require('../src/measure/measureManager.js');
router.use(cors({
origin: ['http://127.0.0.1:5500', 'http://localhost:5500', 'http://localhost:3000'],
@@ -30,44 +33,10 @@ router.use(cors({
*/
router.get('/projects', async (req, res) => {
try {
const projects = await db.query('SELECT * FROM public.projects');
res.json(projects.rows);
const projects = await projectManager.getAllProjects();
res.json(projects);
} catch (error) {
serverError.sendError('Error getting projects:', res, error);
}
});
/**
* @swagger
* /projects/{id}/create-video:
* get:
* description: Create a video from images of a specific project by project id
* parameters:
* - in: path
* name: id
* required: true
* description: Numeric ID of the project to create video for.
* schema:
* type: integer
* responses:
* 200:
* description: Video created successfully
* 404:
* description: No images found for this project
* 500:
* description: Internal server error
*/
router.get('/projects/:id/create-video', async (req, res) => {
const projectId = req.params.id;
if (!projectId || isNaN(projectId)) {
return res.status(400).json({ error: 'Invalid project ID' });
}
try {
const videoPath = await video.createVideo(projectId);
res.json(videoPath);
res.status(200).json({ message: 'Video created successfully', videoPath });
} catch (error) {
serverError.sendError('Error creating video:', res, error);
serverError.sendError('Error getting all projects:', res, error);
}
});
@@ -95,13 +64,10 @@ router.get('/projects/:id', async (req, res) => {
return res.status(400).json({ error: 'Invalid project ID' });
}
try {
const project = await db.query('SELECT * FROM public.projects WHERE id = $1', [projectId]);
if (project.rows.length === 0) {
return res.status(404).json({ error: 'Project not found' });
}
res.json(project.rows[0]);
const project = await projectManager.getProjectById(projectId);
res.json(project);
} catch (error) {
serverError.sendError('Error getting project:', res, error);
serverError.sendError('Error getting project by ID:', res, error);
}
});
@@ -123,18 +89,18 @@ router.get('/projects/:id', async (req, res) => {
* 500:
* description: Internal server error
*/
router.get('/projects/:id/videos', (req, res) => {
router.get('/projects/:id/videos', async (req, res) => {
const projectId = req.params.id;
if (!projectId || isNaN(projectId)) {
return res.status(400).json({ error: 'Invalid project ID' });
}
const query = 'SELECT * FROM public.videos WHERE project_id = $1';
db.query(query, [projectId], (err, results) => {
if (err) {
serverError.sendError('Erreur lors de la récupération des vidéos pour le projet:', res, err);
}
res.json(results.rows);
});
try {
const videos = await projectManager.getVideosByProjectId(projectId);
res.json(videos);
}
catch (error) {
serverError.sendError('Error getting videos by project ID:', res, error);
}
});
/**
@@ -155,18 +121,18 @@ router.get('/projects/:id/videos', (req, res) => {
* 500:
* description: Internal server error
*/
router.get('/projects/:id/measurements', (req, res) => {
router.get('/projects/:id/measurements', async (req, res) => {
const projectId = req.params.id;
if (!projectId || isNaN(projectId)) {
return res.status(400).json({ error: 'Invalid project ID' });
}
const query = 'SELECT * FROM public.measurements WHERE project_id = $1';
db.query(query, [projectId], (err, results) => {
if (err) {
serverError.sendError('Erreur lors de la récupération des mesures pour le projet:', res, err);
}
res.json(results.rows);
});
try {
const measurements = await projectManager.getMeasurementsByProjectId(projectId);
res.json(measurements);
}
catch (error) {
serverError.sendError('Error getting measurements by project ID:', res, error);
}
});
/**
@@ -196,15 +162,12 @@ router.post('/projects', async (req, res) => {
if (!name || !description) {
return res.status(400).json({ error: 'Name and description are required' });
}
try {
const query = `INSERT INTO public.projects (name, description, status) VALUES ($1, $2, 0) RETURNING id`;
const result = await db.query(query, [name, description]);
const projectId = result.rows[0].id;
fileUtils.createProjectDirectory(projectId);
res.status(201).json({ message: 'Project created successfully', projectId });
} catch (error) {
project = await projectManager.createProject(name, description, new Date(), 0);
projectManager.createProjectDirectory(project.id);
res.status(201).json({ message: 'Project added successfully', id: project.id });
}
catch (error) {
serverError.sendError('Error creating project:', res, error);
}
});
@@ -236,12 +199,8 @@ router.delete('/projects/:id', async (req, res) => {
}
try {
const result = await db.query('DELETE FROM public.projects WHERE id = $1 RETURNING id', [projectId]);
if (result.rowCount === 0) {
return res.status(404).json({ error: 'No project found with this ID' });
}
fileUtils.deleteProjectDirectory(projectId);
projectManager.deleteProjectDirectory(projectId);
projectManager.deleteProjectById(projectId);
res.status(200).json({ message: 'Project deleted successfully', id: projectId });
} catch (error) {
serverError.sendError('Error deleting project:', res, error);
@@ -533,7 +492,7 @@ router.delete('/videos/:id', (req, res) => {
* description: Image not found
*/
router.get('/smile', (req, res) => {
const imagePath = path.join('/storage/smile.jpg');
const imagePath = dbTester.getSmileImage();
fs.access(imagePath, fs.constants.F_OK, (err) => {
if (err) {
console.error('Image not found:', err);
@@ -573,4 +532,50 @@ router.get('/image/:filename', (req, res) => {
});
});
module.exports = router;
const upload = multer({ storage: multer.memoryStorage() });
/**
* @swagger
* /upload:
* post:
* description: Use to upload an image
* requestBody:
* required: true
* content:
* multipart/form-data:
* schema:
* type: object
* properties:
* image:
* type: string
* format: binary
* projectId:
* type: integer
* orderId:
* type: integer
* responses:
* 200:
* description: Image uploaded successfully
* 400:
* description: All fields are required
* 500:
* description: Internal server error
*/
router.post('/upload', upload.single('image'), async (req, res) => {
const { projectId, orderId } = req.body;
const image = req.file; // Multer adds the file to req.file
if (!image || !projectId || !orderId) {
return res.status(400).json({ error: 'All fields are required' });
}
try {
const imagePath = await measureManager.uploadMeasureImage(image, projectId, orderId);
res.json({ message: 'Image uploaded successfully', path: imagePath });
} catch (error) {
serverError.sendError('Error uploading image:', res, error);
}
});
module.exports = router;