Meteor collectionfs insert server side

前端 未结 2 1157
滥情空心
滥情空心 2020-12-30 17:17

hi everyone i using collectionfs + gridfs + cfs filesystem, on collectionfs documentation i find how to insert file on client side like this :

Template.myFo         


        
相关标签:
2条回答
  • 2020-12-30 18:04

    make sure to apply allow and deny rules like so:

    Images.allow({
      insert: function() { return true },
      update: function() { return true },
      remove: function() { return false }
    });
    

    Update must also be applied if using streaming

    0 讨论(0)
  • 2020-12-30 18:07

    An example for you. I didnt tested it but it shows the way you have to go.

    First define a collection:

    I think this step is already clear to you.

    var postImagesStoreFS = new FS.Store.FileSystem("postImages", {
      path: "~/workspace/uploads/"
    });
    

    Add added some filters. Just in case you need something like that.

    PostImages = new FS.Collection('postImages', {
      stores: [postImagesStoreFS ],
      filter: {
      maxSize: 3145728,
      allow: {
        contentTypes: ['image/*'],
        extensions: ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG']
      }
    });
    

    Now you can define in the same *.js file your allow and deny functions. If you remove the insecure package all inserts/updates/removes have to pass allow/deny functions. If a command passes the allow callback it can be inserted into your collection (If there is no deny function that invalidates it)

    Well in this example i just like to insert an image if there is a user and if the metadata user of the image is the user itself. You have to set the metadata user on your own. For testing just return true in every allow function, like shown in the example of Pent. Check the meteor documentation to read more about allow/deny http://docs.meteor.com/#allow

    PostImages.allow({
      insert: function(userId, doc) {
        return (userId && doc.metadata.owner === userId);
      },
      update: function(userId, doc, fieldNames, modifier) {
        return (userId === doc.metadata.owner);
      },
      remove: function(userId, doc) {
        return false;
      },
      download: function(userId) {
        return !!userId;
      }
    });
    

    The client template should work as you posted. Just in case you want to use some metadata i added a bigger example.

    Template.myForm.events({
      'change .myFileInput': function(event, template) {
        FS.Utility.eachFile(event, function(file) {
          var fsFile = new FS.File(file);
          fsFile.metadata = {owner: Meteor.userId()};
          Images.insert(fsFile, function (err, fileObj) {
    
          });
        });
      }
    });
    

    This should be everything you need to have.

    0 讨论(0)
提交回复
热议问题