【问题标题】:How to save to /public in Meteor with cfs:filesystem and collectionfs?如何使用 cfs:filesystem 和 collectionfs 在 Meteor 中保存到 /public?
【发布时间】:2016-01-06 00:18:39
【问题描述】:

/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
        }
      });
    });
  },

});

对于路径,如果我使用:

path: "/public/assets/images",

错误:EACCES,权限被拒绝'/public'

path: "/assets/images",

错误:EACCES,权限被拒绝'/assets'

path: "~/assets/images",

这可行,但它会将图像保存到我的 Linux 机器上的/home/assets/images。该路径根本与 Meteor 项目无关。

【问题讨论】:

    标签: meteor collectionfs


    【解决方案1】:

    我已经使用meteor-root包解决了这个问题: https://atmospherejs.com/ostrio/meteor-root

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

    所以现在它将文件存储在 app/public/uploads/ 中

    希望这会有所帮助!

    【讨论】:

    • 完美运行。这应该是公认的答案。
    【解决方案2】:

    / 不代表您网站的根目录。 / 代表系统的根目录。流星应用以您的用户身份运行。

    您想要做的是使用相对路径。 collectionFS fs.write 操作可能不是从应用程序根目录完成的。所以你也可以使用path: process.env.PWD + '/public/assets/images'

    【讨论】:

    • 谢谢。 process.env.PWD 是什么意思顺便说一句?看起来PWD 代表password,但这在这种情况下没有任何意义。
    • 这是一个unix的想法。它代表“打印工作目录”。
    • 如果将图像存储在公共目录中,则服务器控制台上会出现消息“=>Client modified --正在刷新”并刷新网页......@fuzzybabybunny 你遇到过同样的事情?
    • 有趣的是,在 Android Cordova 环境中,process.env.PWD 生成路径 .meteor/local/cordova-build,它与 Meteor 应用程序的路径相关。因此,您的解决方案不适用于移动环境(我猜 iOS 也是一样)。我通过在我的应用程序结构的根目录处向settings.json 添加一行来解决了这个问题。该行是:"root" : "my/local/absolute/path/meteor_project_name"(您可以通过在 Unix/Linux/OS X 的终端内运行pwd -P 来阅读它)它不是自动化的,所以我必须为 prod 配置它。我还没有尝试过 Andrew Tolochka 的解决方案。
    【解决方案3】:

    你可以使用

     var homeDir = process.env.HOMEPATH;
     tmpDir: homeDir + '/uploads/tmp'
    

    【讨论】:

    • 请添加一些关于您的解决方案的 cmet,说明它为什么以及如何解决问题
    • 你必须先设置环境变量HOMEPATH,这不是标准的。你不是说你是怎么做到的。 Meteor 建议从lib/server 中的文件或server/lib 中的文件设置环境变量,使用process.env.VARIABLE_NAME = some_variable_value。这只是一个额外的间接,并不能解决问题。
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多