Ajout de la gestion des images avec scan, suppression et mise à jour des mesures dans la base de données
This commit is contained in:
42
src/data/filewatcher.js
Normal file
42
src/data/filewatcher.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import db from '../../db.js';
|
||||
import path from 'path';
|
||||
import storageManager from '../data/storageManager.js';
|
||||
import fs from 'fs';
|
||||
|
||||
async function checkAndRemoveInvalidEntries() {
|
||||
console.log('Checking for invalid entries...');
|
||||
try {
|
||||
const measurementsRes = await db.query('SELECT id, path FROM measurements');
|
||||
console.log('Fetched measurements:', measurementsRes.rows);
|
||||
for (const row of measurementsRes.rows) {
|
||||
console.log('Checking file path:', row.path);
|
||||
if (!fs.existsSync(row.path)) {
|
||||
// Remove invalid entry
|
||||
await db.query('DELETE FROM measurements WHERE id = $1', [row.id]);
|
||||
console.log(`Deleted invalid measurement entry with id: ${row.id}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Scan all images in storage
|
||||
const allImages = await storageManager.scanAllImages();
|
||||
console.log('Scanned all images:', allImages);
|
||||
for (const imagePath of allImages) {
|
||||
const entryRes = await db.query('SELECT id FROM measurements WHERE path = $1', [imagePath]);
|
||||
if (entryRes.rows.length === 0) {
|
||||
// Remove the file if the entry does not exist
|
||||
fs.unlinkSync(imagePath);
|
||||
console.log(`Deleted file at path: ${imagePath} as its database entry does not exist`);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error checking and removing invalid entries:', err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Run the check periodically
|
||||
setInterval(checkAndRemoveInvalidEntries, 10000); // Every second
|
||||
|
||||
// Initial run
|
||||
checkAndRemoveInvalidEntries();
|
||||
@@ -19,6 +19,27 @@ function deleteFolder(name){
|
||||
}
|
||||
}
|
||||
|
||||
function scanAllImages(dir = 'storage') {
|
||||
const projectDir = path.join(PROJECTS_DIR, dir);
|
||||
let results = [];
|
||||
|
||||
function scanDirectory(directory) {
|
||||
const files = fs.readdirSync(directory);
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(directory, file);
|
||||
const stat = fs.statSync(filePath);
|
||||
if (stat.isDirectory()) {
|
||||
scanDirectory(filePath);
|
||||
} else if (file.endsWith('.jpg')) {
|
||||
results.push(filePath);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
scanDirectory(projectDir);
|
||||
return results;
|
||||
}
|
||||
|
||||
function saveFile(filePath, content) {
|
||||
// Ensure content is a buffer
|
||||
if (Buffer.isBuffer(content)) {
|
||||
@@ -28,16 +49,23 @@ function saveFile(filePath, content) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getFile(name){
|
||||
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
||||
return fs
|
||||
.readFileSync(filePath);
|
||||
}
|
||||
|
||||
function deleteFile(name){
|
||||
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
||||
if (fs.existsSync(filePath))
|
||||
fs.rmSync(filePath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createFolder,
|
||||
deleteFolder,
|
||||
scanAllImages,
|
||||
saveFile,
|
||||
getFile
|
||||
getFile,
|
||||
deleteFile
|
||||
};
|
||||
@@ -11,21 +11,77 @@ async function uploadMeasureImage(image, projectId, orderId) {
|
||||
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];
|
||||
async function getNextOrderId(projectId) {
|
||||
const query = 'SELECT MAX(order_id) FROM public.measurements WHERE project_id = $1';
|
||||
const values = [projectId];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows[0].max + 1;
|
||||
}
|
||||
|
||||
async function addMeasureToProject(projectId, orderId, timestamp, path, temperature, humidity) {
|
||||
const query = 'INSERT INTO public.measurements (project_id, timestamp, path, temperature, humidity, order_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *';
|
||||
const values = [projectId, orderId, timestamp, path, temperature, humidity];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async function getMeasurements(projectId) {
|
||||
const query = 'SELECT * FROM public.measurements WHERE project_id = $1';
|
||||
const values = [projectId];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows;
|
||||
}
|
||||
|
||||
async function getMeasurement(projectId, orderId) {
|
||||
const query = 'SELECT * FROM public.measurements WHERE project_id = $1 AND order_id = $2';
|
||||
const values = [projectId, orderId];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async function getMeasurementById(id) {
|
||||
const query = 'SELECT * FROM public.measurements WHERE id = $1';
|
||||
const values = [id];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async function updateMeasurement(projectId, orderId, timestamp, path, temperature, humidity) {
|
||||
const query = 'UPDATE public.measurements SET timestamp = $3, path = $4, temperature = $5, humidity = $6 WHERE project_id = $1 AND order_id = $2 RETURNING *';
|
||||
const values = [projectId, orderId, timestamp, path, temperature, humidity];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async function updateMeasurementById(id, timestamp, path, temperature, humidity) {
|
||||
const query = 'UPDATE public.measurements SET timestamp = $2, path = $3, temperature = $4, humidity = $5 WHERE id = $1 RETURNING *';
|
||||
const values = [id, timestamp, path, temperature, humidity];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
async function deleteMeasurement(id) {
|
||||
const query = 'DELETE FROM public.measurements WHERE id = $1';
|
||||
const values = [id];
|
||||
const res = await db.query(query, values);
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
export {
|
||||
uploadMeasureImage,
|
||||
addMeasureToProject
|
||||
}
|
||||
addMeasureToProject,
|
||||
getNextOrderId,
|
||||
getMeasurements,
|
||||
getMeasurement,
|
||||
updateMeasurement,
|
||||
deleteMeasurement,
|
||||
getMeasureImage,
|
||||
getMeasurementById,
|
||||
updateMeasurementById
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user