问题
I'm trying to add a new REST API method to the loopback-component-storage for downloading all photos from a Container. (see Strongloop loopback-storage-service: how to use StorageService.downloadStream() to download a ZIP of all photos in container?)
The following code seems to work, but I'd like to know how to load the storage-handler, handler
, and filesystem provider, factory
, correctly within the strongloop framework. Also, I shouldn't have to copy the data in datasources.json
Any suggestions?
// 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'
}
});
回答1:
From within the container.js
model file, you can easily access the StorageService you have defined in datasources.json
. Once you have that, you can just call its download()
method. No need to instantiate (redundantly and with every request) factory
or handler
in your code.
service = Container.app.dataSources.{SourceName}.connector
service.download(container, file, res, cb)
来源:https://stackoverflow.com/questions/29140084/strongloop-loopback-how-to-add-remote-method-to-loopback-component-storage-cont