|
@ -1,24 +1,24 @@ |
|
|
const EventEmitter = require('events'); |
|
|
|
|
|
const csv = require('csv-parser'); |
|
|
const csv = require('csv-parser'); |
|
|
|
|
|
|
|
|
const File = require('../models/File'); |
|
|
const File = require('../models/File'); |
|
|
|
|
|
const emitter = require('./eventEmitter'); |
|
|
|
|
|
|
|
|
// Create a class FileService that extends EventEmitter
|
|
|
// Create a class FileService that extends EventEmitter
|
|
|
class FileService extends EventEmitter { |
|
|
class FileService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async parseFromUrl(url, columns) { |
|
|
async parseFromUrl(url, columns) { |
|
|
this.emit('parseFromUrl.start', { url, columns }); |
|
|
emitter.emit('parseFromUrl.start', { url, columns }); |
|
|
|
|
|
|
|
|
// Check if url is valid
|
|
|
// Check if url is valid
|
|
|
if (!url) { |
|
|
if (!url) { |
|
|
this.emit('parseFromUrl.error', { url, columns, error: 'Invalid url' }); |
|
|
emitter.emit('parseFromUrl.error', { url, columns, error: 'Invalid url' }); |
|
|
return Promise.reject(new Error('Invalid url')); |
|
|
return Promise.reject(new Error('Invalid url')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Check columns
|
|
|
// Check columns
|
|
|
if (!columns || !Array.isArray(columns) || columns.length === 0) { |
|
|
if (!columns || !Array.isArray(columns) || columns.length === 0) { |
|
|
this.emit('parseFromUrl.error', { url, columns, error: 'Invalid columns' }); |
|
|
emitter.emit('parseFromUrl.error', { url, columns, error: 'Invalid columns' }); |
|
|
return Promise.reject(new Error('Invalid columns')); |
|
|
return Promise.reject(new Error('Invalid columns')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -28,11 +28,11 @@ class FileService extends EventEmitter { |
|
|
|
|
|
|
|
|
const stream = file.parse(columns); |
|
|
const stream = file.parse(columns); |
|
|
if ( !stream ) { |
|
|
if ( !stream ) { |
|
|
this.emit('parseFromUrl.error', { url, columns, error: 'Invalid stream' }); |
|
|
emitter.emit('parseFromUrl.error', { url, columns, error: 'Invalid stream' }); |
|
|
return Promise.reject(new Error('Invalid stream')); |
|
|
return Promise.reject(new Error('Invalid stream')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.emit('parseFromUrl.end', { url, columns, filepath }); |
|
|
emitter.emit('parseFromUrl.end', { url, columns, filepath }); |
|
|
return Promise.resolve(stream); |
|
|
return Promise.resolve(stream); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|