【问题标题】:Where are uploaded file in a deployed Meteor app?已部署的 Meteor 应用程序中的上传文件在哪里?
【发布时间】:2015-06-02 16:24:26
【问题描述】:

我对meteor 将静态文件保存在哪里(/public 文件夹中的文件)有点困惑;

我有上传文件的方法:

saveFile: function(blob, name, path, encoding) {        
    if (!Meteor.isServer) return;

    var fs = Npm.require('fs'), 
        chroot = Meteor.chroot || 'public';

    //path = cleanPath(path);
    name = cleanName(name || 'file');
    encoding = encoding || 'binary';

    path = chroot + (path ? '/' + path + '/' : '/');

    var basePath = process.env.PWD;
    var fullPath = basePath + '/' + path + name;
    var dirs = (path.split('/'));
    var currentCreatedPath = basePath;

    //create needed folders
    dirs.forEach(function(dir, index) {
        if (dir.length > 0) {
            currentCreatedPath = currentCreatedPath + '/' + dir;
            if (!fs.existsSync(currentCreatedPath)){
                fs.mkdirSync(currentCreatedPath);
            }
        }
    });

    fs.writeFile(fullPath, blob, encoding, function(err) {
        if (err) console.log(err); //throw (new Meteor.Error(500, 'Failed to save file.', err));
        else console.log('The file ' + fullPath + 'has been saved');
    }); 

    function cleanPath(str) {
        if (str) return str.replace(/\.\./g,'').replace(/\/+/g,'').replace(/^\/+/,'').replace(/\/+$/,'');
    }
    function cleanName(str) {
        return str.replace(/\.\./g,'').replace(/\//g,'');
    }

    return true;
}

然后在模板中我到达这样的文件:

    {{#each this.files}}
        <li>
            <a href="/{{path}}" target="_blank">
                {{title}}
            </a>
        </li>
    {{/each}}

路径是/public/folder/to/file/file.ext

这在本地很有效;但是一旦部署,它就无法找到上传的文件; meteor 将文件保存在部署项目的哪个文件夹中?

【问题讨论】:

  • /public 目录不用于存储上传的文件。我会推荐使用 CollectionFS 而不是原生的 Npm fs。在这种情况下,使用文件系统存储适配器,您的文件将默认上传到您的PROJECT/cfs/files/,您可以在存储创建期间指定路径stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})] CollectionFS 提供 url 帮助程序,然后获取指向这些文件的链接。另一种方法是使用基于 PHP 的上传服务器
  • 感谢您的建议;我试过 CollectionFS;它在本地工作,但一旦部署到我的服务器,应用程序就会在启动时崩溃;现在我不知道为什么
  • 这很可能是因为您正在使用 fs 存储并且 /cfs/files/_temporary 文件夹不可写。要解决此问题,运行您的流星应用程序的用户必须具有写入权限。使用meteor-up而不指定自定义路径将使该文件夹位于:/opt/&lt;yourProjectName&gt;/cfs/files/_tempstore/

标签: meteor


【解决方案1】:

不建议使用这种做法。如果您部署您的应用程序并运行多个实例或“无人机”,则每个实例都将无法看到彼此的文件。

因此,最好使用 s3 之类的东西将文件存储在第三方服务器上。

无论如何,如果您决定将文件存储在同一台机器上,您只需要使用流星项目之外的绝对路径即可。

如果您使用公用文件夹 Meteor 将因文件更改而重新启动。如果您使用捆绑的 asset 静态文件夹,您将很难找到您的文件。

这样的事情可能会有所帮助(保存在您的桌面上):

var path = Npm.require("path").resolve("~/Desktop/");

saveFile(blob, name, path, encoding)

要查看文件,您可以使用某种代理(如果您使用 Iron 路由器):

服务器端代码(可能需要一些调整):

Router.route('/file/:filename', function() {
    var self = this;
    var res = this.response;
    var filename = this.params.filename
    var path = Npm.require("path");
    var fs = Npm.require("fs");

    fs.readFile(path.join(path.resolve("~/Desktop"), filename), function(err, fileContent) {
        if(err) console.log(err);

        self.response.statusCode = 200;
        //self.response.setHeader("Content-Type", response.headers['content-type']);
        self.response.setHeader("Content-disposition", filename);
        self.response.end(fileContent);
    });

}, { where: 'server' });

为简单起见,我使用了您的 Desktop 文件夹 (OS X)。我建议使用另一个更安全的文件夹。

然后你就可以使用网址http://localhost:3000/file/your_file_name来下载了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多