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:
43
src/data/storageManager.js
Normal file
43
src/data/storageManager.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const PROJECTS_DIR = path.join('.');
|
||||
|
||||
function createFolder(name){
|
||||
const projectDir = path.join(PROJECTS_DIR, `${name}`);
|
||||
if (!fs.existsSync(projectDir)) {
|
||||
fs.mkdirSync(projectDir, { recursive: true });
|
||||
}
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
function deleteFolder(name){
|
||||
const projectDir = path.join(PROJECTS_DIR, `${name
|
||||
}`);
|
||||
if (fs.existsSync
|
||||
(projectDir)) {
|
||||
fs.rmSync(projectDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function saveFile(filePath, content) {
|
||||
// Ensure content is a buffer
|
||||
if (Buffer.isBuffer(content)) {
|
||||
fs.writeFileSync(filePath, content);
|
||||
} else {
|
||||
throw new Error('Content must be a buffer');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getFile(name){
|
||||
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
||||
return fs
|
||||
.readFileSync(filePath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createFolder,
|
||||
deleteFolder,
|
||||
saveFile,
|
||||
getFile
|
||||
};
|
||||
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
|
||||
}
|
||||
74
src/project/projectManager.js
Normal file
74
src/project/projectManager.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import storageManager from '../data/storageManager.js';
|
||||
import db from '../../db.js';
|
||||
|
||||
function createProjectDirectory(projectId) {
|
||||
const projectPath = `${projectId}`;
|
||||
storageManager.createFolder(projectPath);
|
||||
storageManager.createFolder(`${projectPath}/images`);
|
||||
storageManager.createFolder(`${projectPath}/videos`);
|
||||
}
|
||||
|
||||
function deleteProjectDirectory(projectId) {
|
||||
const projectPath = `${projectId}`;
|
||||
storageManager.deleteFolder(projectPath);
|
||||
}
|
||||
|
||||
async function getAllProjects() {
|
||||
const query = 'SELECT * FROM public.projects';
|
||||
const res = await db.query(query);
|
||||
console.log('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);
|
||||
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);
|
||||
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);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async function deleteProjectById(projectId) {
|
||||
const query = 'DELETE FROM public.projects WHERE id = $1';
|
||||
const values = [projectId];
|
||||
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);
|
||||
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);
|
||||
return res.rows;
|
||||
}
|
||||
|
||||
export {
|
||||
createProjectDirectory,
|
||||
deleteProjectDirectory,
|
||||
getAllProjects,
|
||||
getProjectById,
|
||||
createProject,
|
||||
editProjectById,
|
||||
deleteProjectById,
|
||||
getVideosByProjectId,
|
||||
getMeasurementsByProjectId
|
||||
};
|
||||
Reference in New Issue
Block a user