Handling FS.collection in meteor

194 views Asked by At

I want to filter my result from the subscription to my images fs.collection.

The collection is declared like this :

 Images = new FS.Collection('images', {
  stores: [ImagesStore],
  filter: {
    //maxSize: 1048576 * 1, //in bytes //1MB
    maxSize: 512 * 1024, //in bytes
      allow: {
        contentTypes: ['image/*'],
        extensions: ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF' ]
      },
    onInvalid: function (message) {
      if (Meteor.isClient) {
        alert("Your file must be a ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF' ], and must not exceed 512KBytes");
          sAlert.error(message, {effect: 'stackslide', position: 'left-top', timeout: '3000'});
      } else {
        console.log(message);
      }
    }
  }
});

FS.HTTP.setBaseUrl('/usrImg');

Images.deny({
  insert: function(userId, fileObj) {
    return false;
  },
  update: function(userId, fileObj) {
    return false;
  },
  remove: function(userId, fileObj) {
    return false;
  },
  download: function(userId, fileObj /*, shareId*/) {
    return false;
  },
  fetch: []
});


Images.allow({
  insert: function(userId, fileObj) {
    return true;
  },
  update: function(userId, fileObj) {
    return true;
  },
  remove: function(userId, fileObj) {
    return true;
  },
  download: function(userId, fileObj /*, shareId*/) {
    return true;
  },
  fetch: []   //  ['owner']
});

The subscription is like this :

var i = -1,
array = [],
usrAvatarId = Meteor.users.find({"profile.avatar.type": "doc"}, {fields: {"profile.avatar.data": 1}}).fetch(),
nbAvatar = Meteor.users.find({"profile.avatar.type": "doc"}).count();
console.log(nbAvatar);
console.log(usrAvatarId);
while(++i < nbAvatar)
    array.push(usrAvatarId[i]._id);
console.log(array);
Meteor.subscribe('images', array);

And finally, the publication is like this :

Meteor.publish('images', function(array){
    console.log(array);
    return Images.find({});
});

The goal is currently to pass as parameter the array containing the allowed files ID. As it is now, I always download all the collection documents.

When I log the colelction client side, I do well have an array of files with the _id property.

But when I try this :

Meteor.publish('images', function(array){
    console.log(array);
    return Images.find({_id: {$in: array}});
});

I do not get any document, thought that the server log me correctly the passed array.

I'm probably missing something : I'm not used to FS.collection, and its doc is not the most easy to understand I've seen so far...

Concerning the Allow / Deny, I do not thinks it changes something here, but I didn't changed them form the source I used to start.

Could someone help ? It's getting boring to download 200 files per connection :/

Thank you

1

There are 1 answers

0
Michel Floyd On

Basically you're trying to do a join to only download the images corresponding to the users that you are publishing to the client.

The much better, simpler, faster way is to use the reywood:publish-composite package to fetch the images in the same publication in which you are publishing the users.

I've been doing this for a long time with an FS.collection in my own app, it works really well.