Ajout de la gestion des chemins d'images et amélioration des messages de log dans plusieurs modules
This commit is contained in:
@@ -3,17 +3,20 @@ import path from 'path';
|
||||
import storageManager from '../data/storageManager.js';
|
||||
import fs from 'fs';
|
||||
|
||||
let localCounter = 0;
|
||||
|
||||
async function checkAndRemoveInvalidEntries() {
|
||||
console.log('Checking for invalid entries...');
|
||||
localCounter = 0;
|
||||
console.log('[INFO] Vérification et suppression des entrées invalides...');
|
||||
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}`);
|
||||
localCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +29,14 @@ async function checkAndRemoveInvalidEntries() {
|
||||
// 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`);
|
||||
localCounter++; // Increment counter if entry is deleted
|
||||
}
|
||||
}
|
||||
if (localCounter > 0) {
|
||||
console.log(`[INFO] ${localCounter} entrées ont été modifiées`);
|
||||
} else {
|
||||
console.log('[INFO] Aucune entrée n\'a été modifiée.');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error checking and removing invalid entries:', err);
|
||||
}
|
||||
@@ -36,7 +45,8 @@ async function checkAndRemoveInvalidEntries() {
|
||||
|
||||
|
||||
// Run the check periodically
|
||||
setInterval(checkAndRemoveInvalidEntries, 10000); // Every second
|
||||
console.log('[INFO] Activation du FileWatcher pour surveiller les fichiers invalides...')
|
||||
setInterval(checkAndRemoveInvalidEntries, 10000); // Every 10 seconds
|
||||
|
||||
// Initial run
|
||||
checkAndRemoveInvalidEntries();
|
||||
@@ -1,64 +1,80 @@
|
||||
const fs = require('fs');
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const PROJECTS_DIR = path.join('.');
|
||||
|
||||
function createFolder(name){
|
||||
async function createFolder(name) {
|
||||
const projectDir = path.join(PROJECTS_DIR, `${name}`);
|
||||
if (!fs.existsSync(projectDir)) {
|
||||
fs.mkdirSync(projectDir, { recursive: true });
|
||||
try {
|
||||
await fs.access(projectDir);
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
await fs.mkdir(projectDir, { recursive: true });
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
function deleteFolder(name){
|
||||
const projectDir = path.join(PROJECTS_DIR, `${name
|
||||
}`);
|
||||
if (fs.existsSync
|
||||
(projectDir)) {
|
||||
fs.rmSync(projectDir, { recursive: true, force: true });
|
||||
async function deleteFolder(name) {
|
||||
const projectDir = path.join(PROJECTS_DIR, `${name}`);
|
||||
try {
|
||||
await fs.access(projectDir);
|
||||
await fs.rm(projectDir, { recursive: true, force: true });
|
||||
} catch (error) {
|
||||
if (error.code !== 'ENOENT') {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function scanAllImages(dir = 'storage') {
|
||||
async function scanAllImages(dir = 'storage') {
|
||||
const projectDir = path.join(PROJECTS_DIR, dir);
|
||||
let results = [];
|
||||
|
||||
function scanDirectory(directory) {
|
||||
const files = fs.readdirSync(directory);
|
||||
files.forEach(file => {
|
||||
|
||||
async function scanDirectory(directory) {
|
||||
const files = await fs.readdir(directory);
|
||||
for (const file of files) {
|
||||
const filePath = path.join(directory, file);
|
||||
const stat = fs.statSync(filePath);
|
||||
const stat = await fs.stat(filePath);
|
||||
if (stat.isDirectory()) {
|
||||
scanDirectory(filePath);
|
||||
await scanDirectory(filePath);
|
||||
} else if (file.endsWith('.jpg')) {
|
||||
results.push(filePath);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
scanDirectory(projectDir);
|
||||
await scanDirectory(projectDir);
|
||||
return results;
|
||||
}
|
||||
|
||||
function saveFile(filePath, content) {
|
||||
// Ensure content is a buffer
|
||||
async function saveFile(filePath, content) {
|
||||
if (Buffer.isBuffer(content)) {
|
||||
fs.writeFileSync(filePath, content);
|
||||
await fs.writeFile(filePath, content);
|
||||
} else {
|
||||
throw new Error('Content must be a buffer');
|
||||
}
|
||||
}
|
||||
|
||||
function getFile(name){
|
||||
async function getFile(name) {
|
||||
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
||||
return fs
|
||||
.readFileSync(filePath);
|
||||
return await fs.readFile(filePath);
|
||||
}
|
||||
|
||||
function deleteFile(name){
|
||||
async function deleteFile(name) {
|
||||
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
||||
if (fs.existsSync(filePath))
|
||||
fs.rmSync(filePath);
|
||||
try {
|
||||
await fs.access(filePath); // Vérifie si le fichier existe
|
||||
await fs.rm(filePath); // Supprime le fichier
|
||||
return `File ${filePath} deleted successfully.`;
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
return `File ${filePath} does not exist.`;
|
||||
} else {
|
||||
throw error; // Relance l'erreur si ce n'est pas une erreur de fichier introuvable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user