Meteor-CollectionFS createReadStream() error: FS.Utility.safeStream requires a NodeJS Stream

The repo is archived, and when I try to write a migration tool I find I can't get the files stored with Meteor-CollectionFS.

The API says I can use fs.createReadStream() to get the file, but when I do this on a running database, I get this error:

Error: FS.Utility.safeStream requires a NodeJS Stream
    at Object.FS.Utility.safeStream (packages/cfs_base-package.js:418:11)
    at Object.self.adapter.createReadStream (packages/cfs_storage-adapter.js:114:23)
    at FS.File.createReadStream (packages/cfs_file.js:833:30)
    at CFSAttachments.find.forEach.file (server/migrations.js:1049:24)
    at SynchronousCursor.forEach (packages/mongo/mongo_driver.js:1107:16)
    at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:887:44)
    at Object.Migrations.add [as migrationCallback] (server/migrations.js:1047:24)
    at packages/idmontie_migrations.js:238:17
    at Function.time (/programs/server/profile.js:273:30)
    at /programs/server/boot.js:412:15
    at /programs/server/boot.js:462:7
    at Function.run (/programs/server/profile.js:280:14)
    at /programs/server/boot.js:460:13
** HTTP-BRIDGE: App server exited with status code: 1

I have totally no idea about how can this happen. I'm trying to migrate a wekan grain in sandstorm system. Can anyone help, or give me a workaround to extract file stored in a mongoDB? Thank you.

Upvotes: 0

Views: 143

Answers (1)

As @Jankapunkt mentioned, I can just retrieve the data from MongoDB gridFS system like this:

  import { MongoInternals } from 'meteor/mongo';

  const http = require('http');
  const fs = require('fs');
  CFSAttachments.find().forEach(file => {
    const bucket = new MongoInternals.NpmModule.GridFSBucket(
       MongoInternals.defaultRemoteCollectionDriver().mongo.db, 
       {bucketName: 'cfs_gridfs.attachments'}
    );
    const gfsId = new MongoInternals.NpmModule.ObjectID(file.copies.attachments.key);
    const reader = bucket.openDownloadStream(gfsId);
    const path = `/var/attachments/${file.name()}`;
    const fd = fs.createWriteStream(path);
    reader.pipe(fd);
    Attachments.addFile(path, opts, (err, fileRef) => {
      if (err) {
        console.log('error when migrating ', fileName, err);
      } else {
        file.remove();
      }
    });
  });

Upvotes: 0

Related Questions