@ -18,15 +18,6 @@ const { basedir } = require('../config/constants');
const dest = path . join ( basedir , 'public/csv' ) ;
const dest = path . join ( basedir , 'public/csv' ) ;
// Create a generateFilePath function witch returns a path with a filename and datetime
function generateFilePath ( filename ) {
return {
filepath : path . join ( dest , ` ${ filename } - ${ Date . now ( ) } .csv ` ) ,
generatedpath : path . join ( dest , ` ${ filename } -generated- ${ Date . now ( ) } .csv ` ) ,
} ;
}
// Create a class File that extends EventEmitter
// Create a class File that extends EventEmitter
class File {
class File {
@ -38,7 +29,14 @@ class File {
this . url = url ;
this . url = url ;
}
}
// Create a generateFilePath function witch returns a path with a filename and datetime
generateFilePath ( filename ) {
const date = new Date ( ) ;
return {
filepath : path . join ( dest , ` ${ filename } - ${ date . toISOString ( ) . split ( 'T' ) [ 0 ] } .csv ` ) ,
generatedpath : path . join ( dest , ` ${ filename } -generated- ${ date . toISOString ( ) . split ( 'T' ) [ 0 ] } .csv ` ) ,
} ;
}
/ * *
/ * *
* Download a file from a url
* Download a file from a url
@ -47,7 +45,7 @@ class File {
async download ( ) {
async download ( ) {
const url = URL . parse ( this . url ) ;
const url = URL . parse ( this . url ) ;
this . filename = slugify ( url . hostname , { lower : true } ) ;
this . filename = slugify ( url . hostname , { lower : true } ) ;
const { filepath , generatedpath } = generateFilePath ( this . filename ) ;
const { filepath , generatedpath } = this . generateFilePath ( this . filename ) ;
this . filepath = filepath ;
this . filepath = filepath ;
this . generatedpath = generatedpath ;
this . generatedpath = generatedpath ;
@ -121,13 +119,14 @@ class File {
// create a parse method which read the file and return a stream
// create a parse method which read the file and return a stream
parse ( columns ) {
parse ( columns ) {
const stream = new PassThrough ( ) ;
return new Promise ( ( resolve , reject ) => {
const fileStream = fs . createWriteStream ( this . generatedpath ) ;
const fileStream = fs . createWriteStream ( this . generatedpath ) ;
// check if columns is valid
// check if columns is valid
if ( ! columns || ! columns . length ) {
if ( ! columns || ! columns . length ) {
// return Promise.reject(new Error('Invalid columns'));
// return Promise.reject(new Error('Invalid columns'));
emitter . emit ( 'parse.error' , { url : this . url , filepath : this . filepath , error : 'Invalid columns' } ) ;
emitter . emit ( 'parse.error' , { url : this . url , filepath : this . filepath , error : 'Invalid columns' } ) ;
reject ( new Error ( 'Invalid columns' ) ) ;
return false ;
return false ;
}
}
@ -163,14 +162,12 @@ class File {
// Emit a parse.start event with the url and filepath
// Emit a parse.start event with the url and filepath
emitter . emit ( 'parse.start' , { url : this . url , filepath : this . filepath , headers , result : result } ) ;
emitter . emit ( 'parse.start' , { url : this . url , filepath : this . filepath , headers , result : result } ) ;
stream . write ( result . join ( ';' ) + "\n" ) ;
fileStream . write ( result . join ( ';' ) + "\n" ) ;
fileStream . write ( result . join ( ';' ) + "\n" ) ;
} )
} )
. on ( 'data' , ( row ) => {
. on ( 'data' , ( row ) => {
// Emit a parse.data event with the url, filepath and data
// Emit a parse.data event with the url, filepath and data
let result = this . processRow ( row , columnsIndex , columnsFiltered ) ;
let result = this . processRow ( row , columnsIndex , columnsFiltered ) ;
emitter . emit ( 'parse.data' , { url : this . url , filepath : this . filepath , data : row , result , index : count } ) ;
emitter . emit ( 'parse.data' , { url : this . url , filepath : this . filepath , data : row , result , index : count } ) ;
stream . write ( result . join ( ';' ) + "\n" ) ;
fileStream . write ( result . join ( ';' ) + "\n" ) ;
fileStream . write ( result . join ( ';' ) + "\n" ) ;
count ++ ;
count ++ ;
} )
} )
@ -179,16 +176,16 @@ class File {
emitter . 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 } ) ;
fileStream . close ( ) ;
fileStream . close ( ) ;
reject ( err ) ;
fs . unlink ( this . generatedpath , ( ) => { } ) ;
fs . unlink ( this . generatedpath , ( ) => { } ) ;
} )
} )
. on ( 'end' , ( ) => {
. on ( 'end' , ( ) => {
// Emit a parse.end event with the url and filepath
// Emit a parse.end event with the url and filepath
stream . end ( ) ;
fileStream . close ( ) ;
fileStream . close ( ) ;
resolve ( this . generatedpath ) ;
emitter . emit ( 'parse.end' , { url : this . url , filepath : this . filepath , count : count - 1 , generated : path . basename ( this . generatedpath ) } ) ;
emitter . emit ( 'parse.end' , { url : this . url , filepath : this . filepath , count : count - 1 , generated : path . basename ( this . generatedpath ) } ) ;
} ) ;
} ) ;
} ) ;
return stream ;
}
}
/ * *
/ * *