【问题标题】:How to list files in folder如何列出文件夹中的文件
【发布时间】:2023-03-27 12:05:02
【问题描述】:

如何列出带有Meteor 的文件夹中的所有文件。我的应用程序上安装了FS 集合和cfs:filesystem。我没有在文档中找到它。

【问题讨论】:

    标签: meteor fs


    【解决方案1】:

    另一种方法是添加 shelljs npm 模块。

    要添加 npm 模块,请参阅:https://github.com/meteorhacks/npm

    然后你只需要做类似的事情:

        var shell = Meteor.npmRequire('shelljs');
        var list = shell.ls('/yourfolder');
    

    Shelljs 文档: https://github.com/arturadib/shelljs

    【讨论】:

      【解决方案2】:

      简短的回答是 FS.Collection 创建了一个 Mongo 集合,您可以像对待任何其他集合一样对待它,即,您可以使用 find() 列出条目。

      长答案...

      使用 cfs:filesystem,您可以创建一个镜像服务器上给定文件夹的 mongo 数据库,如下所示:

      // in lib/files.js
      files = new FS.Collection("my_files", {
        stores: [new FS.Store.FileSystem("my_files", {"~/test"})] // creates a ~/test folder at the home directory of your server and will put files there on insert
      });
      

      然后您可以在客户端访问此集合,将文件上传到服务器的 ~test/ 目录:

      files.insert(new File(['Test file contents'], 'my_test_file'));
      

      然后你可以像这样列出服务器上的文件:

      files.find(); // returns [ { createdByTransform: true,
        _id: 't6NoXZZdx6hmJDEQh',
        original: 
         { name: 'my_test_file',
           updatedAt: (Date)
           size: (N),
           type: '' },
         uploadedAt: (Date),
         copies: { my_files: [Object] },
         collectionName: 'my_files' 
       }
      

      copies 对象似乎包含所创建文件的实际名称,例如,

      files.findOne().copies
      {
        "my_files" : {
        "name" : "testy1",
        "type" : "",
        "size" : 6,
        "key" : "my_files-t6NoXZZdx6hmJDEQh-my_test_file", // This is the name of the file on the server at ~/test/
        "updatedAt" : ISODate("2015-03-29T16:53:33Z"),
        "createdAt" : ISODate("2015-03-29T16:53:33Z")
        }
      }
      

      这种方法的问题在于它只跟踪通过集合所做的更改;如果您手动将某些内容添加到 ~/test 目录,它不会被镜像到集合中。例如,如果我在服务器上运行类似...

      mkfile 1k ~/test/my_files-aaaaaaaaaa-manually-created
      

      那我在集合里找,不会有的:

      files.findOne({"original.name": {$regex: ".*manually.*"}}) // returns undefined
      

      如果您只想要服务器上的文件的简单列表,您可以考虑只运行ls。从https://gentlenode.com/journal/meteor-14-execute-a-unix-command/33,您可以使用Node 的child_process.exec() 执行任意UNIX 命令。您可以使用process.env.PWD(来自this question)访问应用程序根目录。所以最后,如果你想列出公共目录中的所有文件,例如,你可以这样做:

      exec = Npm.require('child_process').exec;
      console.log("This is the root dir:"); 
      console.log(process.env.PWD); // running from localhost returns: /Users/me/meteor_apps/test
      child = exec('ls -la ' + process.env.PWD + '/public', function(error, stdout, stderr) {
        // Fill in this callback with whatever you actually want to do with the information
        console.log('stdout: ' + stdout); 
        console.log('stderr: ' + stderr);
      
        if(error !== null) {
          console.log('exec error: ' + error);
        }
      });
      

      这必须在服务器上运行,所以如果你想要客户端上的信息,你必须把它放在一个方法中。这也很不安全,具体取决于您的结构,因此您需要考虑如何阻止人们列出您服务器上的所有文件和文件夹,或者更糟——运行任意 exec。

      您选择哪种方法可能取决于您真正想要完成的任务。

      【讨论】:

      • @michelk 我想看看你是否可以使用process.env.PWD 来获取应用程序目录。它应该在服务器上工作,但它会在客户端上失败。我不确定您在设置商店时是否必须在客户端指定路径,因此您可以执行path: Meteor.isServer ? process.env.PWD + '/uploads' : '/uploads'之类的操作
      • 非常感谢,现在可以使用了。除了文件的存储。我的文件存储在我的用户文件夹中创建的文件夹中,而不是我的项目中。在不提及存储路径的情况下,文件存储在 myProject//.meteor/local/cfs/files/myFS_collection_name 中。
      • 编辑:对不起,我犯了一个错误。我又试了一次:path: Meteor.isServer ? process.env.PWD + '/uploads' : '/uploads'工作完美!再次感谢!
      猜你喜欢
      • 1970-01-01
      • 2021-12-12
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2011-10-27
      • 2021-08-12
      相关资源
      最近更新 更多