Search code examples
meteorcollectionfs

How to save to /public in Meteor with cfs:filesystem and collectionfs?


/lib/collections/images.js

var imageStore = new FS.Store.FileSystem("images", {
  // what should the path be if I want to save to /public/assets?
  // this does not work
  path: "/assets/images/", 
  maxTries: 1
});

Images = new FS.Collection("images", {
  stores: [imageStore]
});

Images.deny({
  insert: function() {
    return false;
  },
  update: function() {
    return false;
  },
  remove: function() {
    return false;
  },
  download: function() {
    return false;
  }
});

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

/client/test.html

<template name="test">

    <input type="file" name="myFileInput" class="myFileInput">

</template>

/client/test.js

Template.test.events({

  'change .myFileInput': function(event, template) {
    FS.Utility.eachFile(event, function(file) {
      Images.insert(file, function (err, fileObj) {
        if (err){
           // handle error
        } else {
           // handle success
        }
      });
    });
  },

});

For the path, if I use:

path: "/public/assets/images",

Error: EACCES, permission denied '/public'

path: "/assets/images",

Error: EACCES, permission denied '/assets'

path: "~/assets/images",

This works, but it saves the image to /home/assets/images on my Linux machine. The path isn't relative to the Meteor project at all.


Solution

  • / doesn't stand for root of your site. / stands for root of your system. The meteor app runs as your user.

    What you'll want do, is use a relative path. It's possible that the collectionFS fs.write operation isn't done from the apps root. So alternatively you could use path: process.env.PWD + '/public/assets/images'