如何列出带有Meteor
.I的文件夹中的所有文件,我已经FS
收集并cfs:filesystem
安装在我的应用程序中。我在文档中找不到它。
简短的答案是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")
}
}
这种方法的问题在于,它仅跟踪通过Collection所做的更改。如果您手动将某些内容添加到〜/ 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's执行任意UNIX命令child_process.exec()
。您可以使用process.env.PWD
(从这个问题开始)访问应用程序根目录。因此,最后,例如,如果您想列出公共目录中的所有文件,则可以执行以下操作:
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);
}
});
这将必须在服务器上运行,因此,如果要在客户端上获取信息,则必须将其放入方法中。这也很不安全,具体取决于您的结构方式,因此您需要考虑如何阻止人们列出服务器上的所有文件和文件夹,或更糟糕的是-运行任意执行程序。
您选择哪种方法可能取决于您实际要完成的工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句