Browse Source

Refactor the File class to use a separate event emitter module for better code organization and separation of concerns

node16
Onja 12 months ago
parent
commit
f408226daa
  1. 25
      src/models/file.js

25
src/models/file.js

@ -7,11 +7,10 @@ const http = require('http');
const https = require('https');
const exec = util.promisify(require('child_process').exec);
const EventEmitter = require('events');
const csvParser = require('csv-parser');
const { PassThrough } = require('stream');
const slugify = require('slugify');
const e = require('express');
const emitter = require('../services/eventEmitter');
// Load project path from config/constants.js
const { basedir } = require('../config/constants');
@ -29,7 +28,7 @@ function generateFilePath(filename) {
// Create a class File that extends EventEmitter
class File extends EventEmitter {
class File {
/**
* Create a new File instance
@ -57,13 +56,13 @@ class File extends EventEmitter {
const request = url.protocol === 'https:' ? https : http;
// Emit a download.start event with the url and filepath
this.emit('download.start', { url: this.url, filepath: this.filepath });
emitter.emit('download.start', { url: this.url, filepath: this.filepath });
// Handle file errors
file.on('error', (err) => {
fs.unlink(filepath, () => {
// Emit a download.error event with the error
this.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'file' });
emitter.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'file' });
});
});
@ -73,7 +72,7 @@ class File extends EventEmitter {
// Check if response is valid
if (response.statusCode !== 200) {
// Emit a download.error event with the error
this.emit('download.error', { url: this.url, filepath: this.filepath, error: response.statusMessage, type: 'response' });
emitter.emit('download.error', { url: this.url, filepath: this.filepath, error: response.statusMessage, type: 'response' });
return reject(response.statusMessage);
}
@ -93,7 +92,7 @@ class File extends EventEmitter {
// Check if the downloaded size is bigger than the step limit
if (downloaded > stepLimit) {
// Emit a download.progress event with the url, filepath and downloaded size
this.emit('download.progress', { url: this.url, filepath: this.filepath, percentage: Math.floor((downloaded / contentLength) * 100) });
emitter.emit('download.progress', { url: this.url, filepath: this.filepath, percentage: Math.floor((downloaded / contentLength) * 100) });
stepLimit += step;
}
});
@ -102,7 +101,7 @@ class File extends EventEmitter {
file.on('finish', () => {
// Emit a download.end event with the url and filepath
this.emit('download.end', { url: this.url, filepath: this.filepath });
emitter.emit('download.end', { url: this.url, filepath: this.filepath });
file.close(() => {
resolve(filepath);
});
@ -111,7 +110,7 @@ class File extends EventEmitter {
.on('error', (err) => {
fs.unlink(filepath, () => {
// Emit a download.error event with the error
this.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'request' });
emitter.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'request' });
reject(err.message);
});
});
@ -145,11 +144,11 @@ class File extends EventEmitter {
.pipe(csvParser({ separator: ';' }))
.on('headers', (headers) => {
// Emit a parse.start event with the url and filepath
this.emit('parse.start', { url: this.url, filepath: this.filepath, headers });
emitter.emit('parse.start', { url: this.url, filepath: this.filepath, headers });
})
.on('data', (row) => {
// Emit a parse.data event with the url, filepath and data
this.emit('parse.data', { url: this.url, filepath: this.filepath, data: row, index: i });
emitter.emit('parse.data', { url: this.url, filepath: this.filepath, data: row, index: i });
let result = [];
if ( i === 0 ) {
@ -170,11 +169,11 @@ class File extends EventEmitter {
})
.on('error', (err) => {
// Emit a parse.error event with the error
this.emit('parse.error', { url: this.url, filepath: this.filepath, error: err.message });
emitter.emit('parse.error', { url: this.url, filepath: this.filepath, error: err.message });
})
.on('end', () => {
// Emit a parse.end event with the url and filepath
this.emit('parse.end', { url: this.url, filepath: this.filepath, count: i });
emitter.emit('parse.end', { url: this.url, filepath: this.filepath, count: i });
stream.end();
});

Loading…
Cancel
Save