Search code examples
javascriptmeteorcollectionfs

Convert a collectionFS file from filesystem to gridFS on serverside


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.


Solution

  • 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);
                });
            });