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

@@ -0,0 +1,38 @@
import db from '../../db.js';
import path from 'path';
import storageManager from '../data/storageManager.js';
async function uploadMeasureImage(image, projectId, orderId) {
// Create the project directory if it doesn't exist
const projectDir = storageManager.createFolder(projectId.toString());
// Create the images directory within the project directory
const imagesDir = storageManager.createFolder(path.join(projectDir, 'images'));
// Define the path where the image will be saved
const imagePath = path.join(imagesDir, `${orderId}.jpg`);
// Save the uploaded image to the defined path
storageManager.saveFile(imagePath, image.buffer);
return imagePath;
}
async function getMeasureImage(projectId, orderId) {
const projectPath = `${projectId}`;
const imagePath = `${projectPath}/${orderId}.jpg`;
return storageManager.getFile(imagePath);
}
async function addMeasureToProject(projectId, timestamp, imagePath, temperature, humidity, completed) {
const query = 'INSERT INTO public.measurements (project_id, timestamp, image_path, temperature, humidity, completed) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *';
const values = [projectId, timestamp, imagePath, temperature, humidity, completed];
const res = await db.query(query, values);
return res.rows[0];
}
export {
uploadMeasureImage,
addMeasureToProject
}