2

/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.

fuzzybabybunny
  • 5,146
  • 6
  • 32
  • 58

3 Answers3

7

I have resolved this issue using meteor-root package: https://atmospherejs.com/ostrio/meteor-root

Files = new FS.Collection("files", {
    stores: [new FS.Store.FileSystem("images", {path: Meteor.absolutePath + '/public/uploads'})]
});

So now it stores files in app/public/uploads/

Hope this helps!

5

/ 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'

halbgut
  • 2,368
  • 17
  • 22
  • Thanks. What does `process.env.PWD` mean btw? It seems like `PWD` stands for `password` but that doesn't make any sense in this context. – fuzzybabybunny Jul 20 '15 at 06:28
  • 1
    It's a unix think. It stands for "print working directory". – halbgut Jul 20 '15 at 06:36
  • If a store an image in the public directory, then the message " =>Client modified -- refreshing" appears on server console and the web page is refreshed.... @fuzzybabybunny have you encountered the same thing ? – Rushikesh Gomekar Oct 21 '15 at 11:40
  • Interestingly, in an Android Cordova environment, `process.env.PWD` yields the path `.meteor/local/cordova-build` which is relative to the path of the Meteor app. Your solution would thus not work on a mobile environment (I guess it's the same for iOS). I solved the problem by adding a line to `settings.json` at the root of my app structure. The line is : `"root" : "my/local/absolute/path/meteor_project_name"`(you can read it by running `pwd -P` inside a terminal in Unix/Linux/OS X) It is not automated, so I have to configure it for prod. I haven't tried the solution of Andrew Tolochka. – Sumi Straessle Feb 23 '17 at 15:28
0

You can use

 var homeDir = process.env.HOMEPATH;
 tmpDir: homeDir + '/uploads/tmp'
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Levan Lacroix
  • 25
  • 1
  • 8
  • 2
    Please add some comments about your solution on why and how it solves the problem – Bhavesh Odedra Nov 20 '15 at 15:21
  • You'd have to set up the environment variable `HOMEPATH` first, which is not standard. You're not saying how you do that. Meteor recommends setting environment variables from a file in `lib/server` or a file in `server/lib`, with `process.env.VARIABLE_NAME = some_variable_value`. This would only be an additional indirection and does not solve the problem. – Sumi Straessle Feb 23 '17 at 15:22