0

At this point I'm literally pulling my hair out at the lack of documentation.

I'm simply trying to read a file uploaded to the server using GridFS. On the client side, it's as easy as going to http://app.url/cfs/files/collection/id. I already checked to make sure it works that way.

So I looked at the source code for CollectionFS and found this code:

var readStream = storage.adapter.createReadStream(ref.file, {start: range.start, end: range.end});

readStream.on('error', function(err) {
  // Send proper error message on get error
  if (err.message && err.statusCode) {
    self.Error(new Meteor.Error(err.statusCode, err.message));
  } else {
    self.Error(new Meteor.Error(503, 'Service unavailable'));
  }
});

readStream.pipe(self.createWriteStream());

I assumed that since that works, so would this:

// From project js file
FilesStore = new FS.Store.GridFS('files');
Files = new FS.Collection('files', {
    stores: [FilesStore]
});

// In Meteor method or Meteor shell
var file = Files.findOne();
var stream = FilesStore.adapter.createReadStream(file);
stream.on('data', function(data) {
    console.log(data);
});

Or, using fast-csv, this:

// In Meteor method or Meteor shell
var file = Files.findOne();
var stream = FilesStore.adapter.createReadStream(file);
var csv_stream = csv().on('data', function(data) {
    console.log(data);
});
stream.pipe(csv_stream);

I've tried just about everything. The file's upload progress is at 100 and the URL is accessible via web browser. But no matter what I do, the data functions will not run. The ReadStream will always indicate that all of the data has been read after it has been piped, yet the data does not actually get piped anywhere and the output functions are never run.

I'm at a loss. Any idea would be helpful.

Thanks for all readers.

Matt Mancuso
  • 143
  • 1
  • 1
  • 9
  • This answer to a related question might be of help: http://stackoverflow.com/questions/32748427/read-collectionfs-file-from-servers-filesystem-when-hosted-on-meteor-com – Michel Floyd Oct 01 '15 at 01:52
  • Thanks for that, the answer there worked. However, it's not exactly ideal. Assigning a fairly large file to a string could cause memory problems down the road. Also, it's more of a workaround than an answer. But I guess it'll have to do for now. What do you suggest I do for the answer to this question? – Matt Mancuso Oct 01 '15 at 04:39
  • You're right that it's reading the whole file into memory. I was hoping this might give you some clues but it's obviously not a complete answer to your question here. Upvote my answer to that other question if you found it helpful. – Michel Floyd Oct 01 '15 at 05:08

0 Answers0