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:
38
src/measure/measureManager.js
Normal file
38
src/measure/measureManager.js
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user