CollectionFS after upload


#1

Is there a posibility to install a server side hook to handle things after uploading an image? For example when the user changed his profile picture, I want to update the user document and some othe documents that include the profile picture.
I think the workaround doing a second call from client is insecure and could cause inconsistency.


#2

Don’t know if it works with CollectionFS, but maybe you can try it:


#3

I already tried collection-hooks but unfortunaltely they doesn’t work with FS.Collection objects.


#4

Mhh okay, and what about observers?

if(Meteor.isServer) {

MyCollection.find().observe({
added: function(item) {
// …
}
}


#5

This is what I’m using. It’s from a CSV importer which runs a server side function on the CSV after import. You should be able to adapt it for your needs.

both/collections/imports.js

const importCSV = function(fileObj, readStream, writeStream) {

  if (Meteor.isServer) {
    // Do stuff with fileObj
  }
};

const Imports = new FS.Collection('imports', {
  stores: [
    new FS.Store.FileSystem('imports', {
      path: '/Users/tim/code/personal/leadiq-uploads/imports',
      transformWrite: importCSV
    })
  ]
});

_.extend(Collections, { Imports });

#6

It absolutely does, but you should access it like this:

If your collection is called Files:

Files = new FS.Collection("files", {
    stores: [fileStore]
});

A collection hook for this would be:

Files.files.after.insert(function(userId, doc){
    //Something something darkside
})

We’re using this in production and would recommend this approach as well.


#7

Thank you guys for your help.