Easy way to get link from uploaded files using Veliov MeteorFiles


#1

Hi,

I use the VeliovGroup/Meteor-Files-plugin to upload files. After the upload I want to show the files directly.
I noticed that Meteor only allows the cursor of a FilesCollection to be send.
So I have a Meteor-call to get the link for my file. Is there some way to get the link in the data during the subscribe or is my meteor-call the only way to get the link to the client?

For the code see my github example, especially my getImageUrl-Method and my template helpers which work but I think are cringeworthy.

Andreas


#2

Hi Andreas!

You can just use file.link() on the client!

Or even easier, for Blaze, Meteor Files comes will a built in Template Helper fileURL.
Which you can use in your template like so:

 <td><img src="{{ fileURL file }}" alt="{{ file.name }}" /></td>

More details:


#3

Hi coagmano,

I tried that and it does not work:

const Images = new FilesCollection({...});

if (Meteor.isServer) {
  Meteor.publish('files.images.all', function () {
    return Images.find({});
  });
}

gets this Exception:

Exception from sub files.images.all id jTzHg3fcZPH6kY2NJ Error: Publish function can only return a Cursor or an array of Cursors.


#4

Ah, I got it!
I can not just return “Images.find()” like i would with a normal collection but rather this:

  Meteor.publish('files.images.all', function () {
    return Images.collection.find({});
  });

or as I did before this:

  Meteor.publish('files.images.all', function () {
    return Images.find({}).cursor;
  });

which works for the publish but it does not provide the link-function.

strange API, I’d not have expected that “Images” has a special collection-attribute that has a find as well as Images itself, or at least rename one of the two “find” methods to something else so it does not confuse the API user :smile: