mhlavacka
mhlavacka

Reputation: 701

Meteor FS.Collection access collection on server

I'm using FS.Collection to upload short video file on a server and then send it as an attachment in email.

Inserting to the collection on server works and I can access collection items on the client, also, stream it directly with a path to the file Url - localhost:3000/cfs/files/videos/{{item_id}}

I wonder how to access collection on the server. I want to send an email with attachment in the following form and need to access path to file and filename on the server. I tried doing:

Email.send({
  to: to,
  from: from,
  subject: subject,
  text: text,
  attachments:[{fileName:"video.mp4", filePath:"/cfs/files/videos/{{item_id}}"}]
});

It displays the attachment video player in the email, but with an error message, so I assume I'm not accessing a file correctly.

My Collection.js is simple:

var videoStore = new FS.Store.GridFS("videos");

Videos = new FS.Collection("videos", {
  stores: [videoStore]
});

Upvotes: 1

Views: 208

Answers (1)

Ryan
Ryan

Reputation: 826

You can not use attachment by filePath of collectionFS. "/cfs/files/videos/{{item_id}}" is a virtual path, i.e. files don't exist in /cfs/files/videos, neither there is folder '/cfs/files/videos'.

You can use http path instead:

var ROOT_URL = process.env.ROOT_URL;
var rootUrl;
if (ROOT_URL.indexOf('/', ROOT_URL.length - 1) != -1) {
    rootUrl = ROOT_URL.substring(0, ROOT_URL.length - 1);
} else {
    rootUrl = ROOT_URL;
}

var attachments = [];
attachment = {
    fileName: fileName(url),
    filePath: rootUrl + "/cfs/files/videos/{{item_id}}"
};
attachments.push(attachment);

Email.send({
    to: to,
    from: from,
    subject: subject,
    text: text,
    attachments: attachments
});

Upvotes: 1

Related Questions