Search code examples
javascriptmeteorangular-meteorcollectionfs

Upload files in dynamic subfolders in Meteor CFS filesystem


I am using Meteor Local filesystem to upload my assets using FS.Store.FileSystem API in a specific folder. But, I want to upload those assets category wise in seperate folders based on their type metadata. I can't figure out how to do this in Meteor. The original documentation suggest to use fileKeyMaker method. Can someone please explain it, how to use it to store assets in seperate folders?

https://github.com/CollectionFS/Meteor-CollectionFS/wiki/How-to:-Customize-the-folders-on-the-filesystem

    AssetFiles = new FS.Collection("assets",{
    stores : [
            new FS.Store.FileSystem("AssetBundle",{path : '~/uploads'})
    ],
    filter : {
        maxSize: 5048576,
        allow : {
                extensions: ['pdf','FBX','cad','jpeg','gif','png','jpg']
        }
    }
 });

Solution

  • If you create the folders as a hack, it isn't difficult. Simply do something like the following:

    AssetFiles = new FS.Collection("assets",{
    stores : [
            new FS.Store.FileSystem("AssetBundle",{path : '~/uploads',
               fileKeyMaker: function (fileObj) {
                   // Lookup the copy
                   var store = fileObj && fileObj._getInfo("assets");
                   // If the store and key is found return the key
                   if (store && store.key) return store.key;
    
                   var filename = fileObj.name();
                   if(filename.indexOf("pdf")>-1){
                       // If no store key found we resolve / generate a key
                       // this should be: "~/uploads/pdf/<filename>"
                       return "pdf/"+filename;
                   }
    
               }
            })
    ],
    filter : {
        maxSize: 5048576,
        allow : {
                extensions: ['pdf','FBX','cad','jpeg','gif','png','jpg']
        }
    }
    });
    

    I am trying to do something similar, with no luck, but you have limited number of folders. Hopefully the above works.