我正在尝试向loopback-component-storage添加一个新的REST API方法,以便从Container下载所有照片。 (见Strongloop loopback-storage-service: how to use StorageService.downloadStream() to download a ZIP of all photos in container?)
以下代码似乎有效,但我想知道如何在strongloop框架内正确加载存储处理程序handler
和文件系统提供程序factory
。另外,我不必复制datasources.json
有什么建议吗?
// http://localhost/api/containers/container1/downloadContainer/IMG_0799.PNG
// file: ./server/models/container.js
loopback_component_storage_path = "../../node_modules/loopback-component-storage/lib/";
datasources_json_storage = {
"name": "storage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "svc/storage",
"_options": {
"getFileName": "",
"allowedContentTypes": "",
"maxFileSize": "",
"acl": ""
}
};
handler = require(loopback_component_storage_path + './storage-handler');
factory = require(loopback_component_storage_path + './factory');
module.exports = function(Container) {
Container.downloadContainer = function(container, files, res, ctx, cb) {
var provider;
provider = factory.createClient(datasources_json_storage);
return handler.download(provider, null, res, container, files, cb);
};
Container.remoteMethod('downloadContainer', {
shared: true,
accepts: [
{arg: 'container', type: 'string', 'http': {source: 'path'}},
{arg: 'files', type: 'string', 'http': {source: 'path'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
],
returns: [],
http: {
verb: 'get',
path: '/:container/downloadContainer/:files'
}
});
答案 0 :(得分:0)
在container.js
模型文件中,您可以轻松访问datasources.json
中定义的StorageService。完成后,您可以调用其download()
方法。无需在代码中实例化(冗余和每个请求)factory
或handler
。
service = Container.app.dataSources.{SourceName}.connector
service.download(container, file, res, cb)