|
@ -1,7 +1,10 @@ |
|
|
const csv = require('csv-parser'); |
|
|
const csv = require('csv-parser'); |
|
|
|
|
|
const fs = require('fs'); |
|
|
|
|
|
const path = require('path'); |
|
|
const File = require('../models/File'); |
|
|
const File = require('../models/File'); |
|
|
const emitter = require('./eventEmitter'); |
|
|
const emitter = require('./eventEmitter'); |
|
|
|
|
|
const { basedir } = require('../config/constants'); |
|
|
|
|
|
const { emit } = require('process'); |
|
|
|
|
|
|
|
|
// Create a class FileService that extends EventEmitter
|
|
|
// Create a class FileService that extends EventEmitter
|
|
|
class FileService { |
|
|
class FileService { |
|
@ -35,6 +38,73 @@ class FileService { |
|
|
emitter.emit('parseFromUrl.end', { url, columns, filepath }); |
|
|
emitter.emit('parseFromUrl.end', { url, columns, filepath }); |
|
|
return Promise.resolve(stream); |
|
|
return Promise.resolve(stream); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Delete old files from public/csv directory |
|
|
|
|
|
* @returns {void} |
|
|
|
|
|
*/ |
|
|
|
|
|
deleteOldFiles() { |
|
|
|
|
|
const directoryPath = path.join(basedir, 'public/csv'); |
|
|
|
|
|
const files = fs.readdirSync(directoryPath); |
|
|
|
|
|
const oneDay = 24 * 60 * 60 * 1000; // Un jour en millisecondes
|
|
|
|
|
|
const currentDate = new Date(); |
|
|
|
|
|
|
|
|
|
|
|
emitter.emit('deleteOldFiles.start', { files, directoryPath }); |
|
|
|
|
|
|
|
|
|
|
|
for (const file of files) { |
|
|
|
|
|
const filePath = path.join(directoryPath, file); |
|
|
|
|
|
const stats = fs.statSync(filePath); |
|
|
|
|
|
|
|
|
|
|
|
if (currentDate - stats.mtime >= oneDay) { |
|
|
|
|
|
fs.unlinkSync(filePath); |
|
|
|
|
|
emitter.emit('deleteOldFiles.deleted', { file, filePath }); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
emitter.emit('deleteOldFiles.end', { directoryPath }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Check if the last operation date is older than 24 hours |
|
|
|
|
|
* @returns {void} |
|
|
|
|
|
*/ |
|
|
|
|
|
checkLastOperationDate() { |
|
|
|
|
|
const dateFilePath = path.join(basedir, 'src/config/lastOperationDate.txt'); |
|
|
|
|
|
|
|
|
|
|
|
emitter.emit('checkLastOperationDate.start', { dateFilePath }); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
const oneDay = 24 * 60 * 60 * 1000; // Un jour en millisecondes
|
|
|
|
|
|
const currentDate = new Date(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if file exists and create it if not with a default date of 48 hours ago
|
|
|
|
|
|
if (!fs.existsSync(dateFilePath)) { |
|
|
|
|
|
emitter.emit('checkLastOperationDate.created', { dateFilePath }); |
|
|
|
|
|
const twodaysdate = new Date(currentDate - (2*oneDay)); |
|
|
|
|
|
fs.writeFileSync(dateFilePath, twodaysdate.toISOString(), 'utf-8'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const lastOperationDate = fs.readFileSync(dateFilePath, 'utf-8'); |
|
|
|
|
|
|
|
|
|
|
|
if (!lastOperationDate || currentDate - new Date(lastOperationDate) >= oneDay) { |
|
|
|
|
|
// Aucune opération dans les dernières 24 heures ou aucune date enregistrée
|
|
|
|
|
|
this.deleteOldFiles(); |
|
|
|
|
|
|
|
|
|
|
|
// Enregistrez la date de la dernière opération
|
|
|
|
|
|
fs.writeFileSync(dateFilePath, currentDate.toISOString(), 'utf-8'); |
|
|
|
|
|
|
|
|
|
|
|
emitter.emit('checkLastOperationDate.updated', { dateFilePath, date: new Date(lastOperationDate) }); |
|
|
|
|
|
} else { |
|
|
|
|
|
emitter.emit('checkLastOperationDate.skipped', { dateFilePath, date: new Date(lastOperationDate) }); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
emitter.emit('checkLastOperationDate.error', { dateFilePath, error: err.message }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
emitter.emit('checkLastOperationDate.end', { dateFilePath, date: new Date() }); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
module.exports = FileService; |
|
|
module.exports = FileService; |