Reading an uploaded file with CollectionFS


#1

How can I read a CollectionFS file as if it were on the filesystem?

Using I’m trying to parse a CSV that has successfully uploaded to CollectionFS – using babyparse():

//Server-side
Collection.on('uploaded', function (fileObj) {
     file = fileObj.createReadStream();
     parsed = Baby.parseFiles(file);

(source: https://github.com/CollectionFS/Meteor-CollectionFS/wiki/How-to:-Convert-a-file-already-stored)

I know the original CSV file is valid. I’m getting:

errors: [ { type: '', code: '', message: 'Unsupported file type.', row: '' }

I must be misusing the streams. What’s the best practice way of doing this?


#2

Baby.parseFiles() expect a file whereas you have a stream (text) so you should do Baby.parse(file) or alternatively create a write stream, pipe the readstream into that, and pass that into Baby.parseFiles().


#3

Thanks @serkandurusoy ! I’m unfamiliar with streams. Can you give me quick example?


#4

@serkandurusoy The issue is that I’m getting a bug trying this approach.

Collection.on('uploaded', function (fileObj) {
  //create files from streams as per: http://www.sitepoint.com/basics-node-js-streams/#piping
  readStream = fileObj.createReadStream('whatever.csv');
  writeStream = fileObj.createWriteStream('newWhatever.csv');
  readStream.pipe(writeStream)
  test = Baby.parseFiles(writeStream)
  console.log(test);
});

Outputs:

W20150819-14:34:08.713(-4)? (STDERR) events.js:72
W20150819-14:34:08.713(-4)? (STDERR)         throw er; // Unhandled 'error' event
W20150819-14:34:08.713(-4)? (STDERR)               ^
W20150819-14:34:08.714(-4)? (STDERR) Error: ENOENT, open '/Users/USER/Desktop/app/uploads/images-3iDjmDJPsoWs4TkPo-test.csv'

Any clues?


#5

Try this: console.log( Baby.parse( fileObj.createReadStream() ) )


#6

@serkandurusoy
I Get a crash:

W20150819-15:12:29.056(-4)? (STDERR) 
W20150819-15:12:29.058(-4)? (STDERR) /Users/USER/.meteor/packages/meteor-tool/.1.1.4.2l3p0l++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20150819-15:12:29.059(-4)? (STDERR) 				throw(ex);
W20150819-15:12:29.059(-4)? (STDERR) 				      ^
W20150819-15:12:29.180(-4)? (STDERR) TypeError: Object [object Object] has no method 'substr'
W20150819-15:12:29.180(-4)? (STDERR)     at guessLineEndings (packages/modweb:baby-parse/baby-parse.js:489:1)
W20150819-15:12:29.181(-4)? (STDERR)     at ParserHandle.parse (packages/modweb:baby-parse/baby-parse.js:292:1)
W20150819-15:12:29.181(-4)? (STDERR)     at Object.CsvToJson [as parse] (packages/modweb:baby-parse/baby-parse.js:80:1)
W20150819-15:12:29.181(-4)? (STDERR)     at EventEmitter.<anonymous> (app/server/serverUploader.js:13:21)
W20150819-15:12:29.181(-4)? (STDERR)     at EventEmitter.emit (events.js:95:17)
W20150819-15:12:29.181(-4)? (STDERR)     at EventEmitter.<anonymous> (packages/cfs:collection/common.js:143:1)
W20150819-15:12:29.181(-4)? (STDERR)     at EventEmitter.emit (events.js:98:17)
W20150819-15:12:29.181(-4)? (STDERR)     at packages/cfs:tempstore/tempStore.js:322:1
W20150819-15:12:29.182(-4)? (STDERR)     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1)

#7

Sorry, I guess I was not paying attention. Try this:

var parsed;
var csv = '';
// get the readable stream from the file object
var stream = fileObj.createReadStream();

// read the stream and concat it into a string variable
stream.on('data', function(chunk) {
  csv += chunk.read().toString();
});

// when the stream ends, you have your csv file contents
stream.on('end', function() {
  parsed = Baby.parse(csv);
  rows = parsed.data;
  console.log(rows);
});