1

In our meteor app, the client will upload some files using collectionFS-filesystem which i store it to an uploads folders in root directory of my app.

//My CFS uploads collection
uploads = new FS.Collection("uploads", {
    stores: [new FS.Store.FileSystem("uploads", {path: "~/uploads"})]
});

Later, i want to save the files to the database using collectionFS-gridFS.

//My CFS grid collection
files = new FS.Collection("files", {
    stores: [new FS.Store.GridFS("files")]
});

How do i read the data from the file on server so that i can store the file to db? Can i use the file from the CFS-filesystem collection to convert it to CFS-gridFS file in anyway?

Thanks in advance.

Sudheer Jami
  • 697
  • 6
  • 15
  • Why do you need to use both filesystem and gridfs? If your question was about using one of them to achieve something, I could answer. I used both before and just converted an app to gridfs today. Much better than filesystem. – Luna Dec 21 '15 at 04:37
  • @Luna I have to store files on server and then based on the organisation of the user, i have insert the file into corresponding db instance as per some of the constraints in our project. Once i insert the file into gridfs collection on the server, the file on the filesystem will be deleted. – Sudheer Jami Dec 21 '15 at 05:43
  • are you sure that is necessary? You can restrict which info is visible to which organisation/user and you can do it secure using only one of them. – Luna Dec 21 '15 at 05:48

2 Answers2

2

I had accept the answer by @perusopersonale. However, below is my approach which i used to achieve this, based on documentation from here and here

uploads.find(document_id).forEach(function (fileObj) {
  var type = fileObj.type();
  var name = fileObj.name();
  var readStream = fileObj.createReadStream(fileObj.collectionName);
  var newFile = new FS.File();
  newFile.attachData(readStream, {type: type});
  newFile.name(name);
  files.insert(newFile);
});                
Sudheer Jami
  • 697
  • 6
  • 15
0

I don't understand why you want to use both. However I have to implement something similar (read from a cfs filesystem, do something and then reinsert in another db), here a version modified that should accomplish what you are triyng to do:

       var fileObj = uploads.findOne(objId);
       var newName = "newfilename"; //file name
       //fileObj.copies.uploads.key contains the filename for store :"uploads"
       fs.readFile( "~/uploads/"+fileObj.copies.uploads.key, function (err, data) {
           var newFile = new FS.File();
           newFile.attachData(data,{type: 'application/octet-stream'}, function(error){
              newFile.name( newName);
              file.insert(newFile);
            });
        });
perusopersonale
  • 896
  • 1
  • 8
  • 18