|
|
@ -22,7 +22,7 @@ const dest = path.join(basedir, 'public/csv'); |
|
|
|
function generateFilePath(filename) { |
|
|
|
return { |
|
|
|
filepath: path.join(dest, `${filename}-${Date.now()}.csv`), |
|
|
|
generatedpath: path.join('csv', `${filename}-generated-${Date.now()}.csv`), |
|
|
|
generatedpath: path.join(dest, `${filename}-generated-${Date.now()}.csv`), |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
@ -122,6 +122,8 @@ class File { |
|
|
|
// create a parse method which read the file and return a stream
|
|
|
|
parse(columns) { |
|
|
|
const stream = new PassThrough(); |
|
|
|
const fileStream = fs.createWriteStream(this.generatedpath); |
|
|
|
stream.pipe(fileStream); |
|
|
|
|
|
|
|
// check if columns is valid
|
|
|
|
if (!columns || !columns.length) { |
|
|
@ -174,11 +176,15 @@ class File { |
|
|
|
.on('error', (err) => { |
|
|
|
// Emit a parse.error event with the error
|
|
|
|
emitter.emit('parse.error', { url: this.url, filepath: this.filepath, error: err.message }); |
|
|
|
|
|
|
|
fileStream.end(); |
|
|
|
fs.unlink(this.generatedpath, () => {}); |
|
|
|
}) |
|
|
|
.on('end', () => { |
|
|
|
// Emit a parse.end event with the url and filepath
|
|
|
|
emitter.emit('parse.end', { url: this.url, filepath: this.filepath, count: count - 1 }); |
|
|
|
stream.end(); |
|
|
|
fileStream.end(); |
|
|
|
emitter.emit('parse.end', { url: this.url, filepath: this.filepath, count: count - 1, generated: path.basename(this.generatedpath) }); |
|
|
|
}); |
|
|
|
|
|
|
|
return stream; |
|
|
|