Meteor-files: (Question) addFile with gridfs

Created on 21 Apr 2021  路  8Comments  路  Source: veliovgroup/Meteor-Files

It seems that addFile API is not firing onAfterUpload callback so that the files are served on the location on the server.
So I simulated after use it to maintain a file at the same collection.

Is there a better way to accomplish this?

DB_CSV_FILES.addFileSync = Meteor.wrapAsync DB_CSV_FILES.addFile, DB_CSV_FILES
_file = DB_CSV_FILES.addFileSync('/Users/kakadais/xmrig.sh')
Object.keys(_file.versions).forEach (versionName) ->
  metadata = {versionName, fileId: _file._id, createdAt: new Date()};
  writeStream = gfs.createWriteStream({filename: _file.name, metadata});
  fs.createReadStream(_file.versions[versionName].path).pipe(writeStream);
  writeStream.on 'close', Meteor.bindEnvironment (file) ->
    property = "versions.#{versionName}.meta.gridFsFileId"
    DB_CSV_FILES.update(_file._id, {$set: {"#{property}": file._id.toString()}})

question

All 8 comments

@kakadais how did you setup your GridFS? Using gridfs-stream or using gridfs-bucket?

Hello @kakadais,

As per addFile docs you got to set 4th argument to true, which is after callback argument. So it isn't compatible with Meteor.wrapAsync method as it assume to have a callback as the last argument.

You can build synchronous function using Fibers/Future

@jankapunkt stream!
@dr-dimitru Yeah I knew you have the answer ;) I missed that option. Thanks-

@kakadais I'm glad we solved it quickly 馃帀

@kakadais just a follow-up message here: gridfs-stream is deprecated, you should use Mongo-Internal gridFS buckets.

The wiki covers this topic: https://github.com/veliovgroup/Meteor-Files/wiki/GridFS-Bucket-Integration
There is an example repo, too: https://github.com/veliovgroup/files-gridfs-autoform-example

I finally wrote a factory that creates FilesCollections with out-of-the-box gridFS bucket integration (will be updated the next days to work with zero-config): https://github.com/leaonline/grid-factory

@jankapunkt Great movement- I should try the bucket in a new project, the on going one treats too many files already ;)

For curious, is there a way to avoid async working even I set the callback for third to set the fourth parameter?
I used wrapAsync to work sync, but the callback makes it async back.
Sorry to ask a bit different scope of question ;)

@kakadais that would be something like:

import Future     from 'fibers/future';

{
  methodCall() {
    const fut = new Future();

    asyncCall(arg1, arg2, function (error, res) {
       fut.return(res);
    }, arg4);

    const asyncCallResult = fut.wait();
  }
}

__Note:__ Fibers are incompatible with future node.js and meteor.js. You should write async/await style method wrapping async calls into Promise in the similar way as with Fibers example above

Was this page helpful?
0 / 5 - 0 ratings