loopback-component-storage/lib/storage-service.js

278 lines
8.5 KiB
JavaScript
Raw Normal View History

2014-01-10 19:34:37 +00:00
var factory = require('./factory');
var handler = require('./storage-handler');
var storage = require('pkgcloud').storage;
module.exports = StorageService;
/**
2014-03-20 21:03:27 +00:00
* Storage service constructor. Properties of options object depend on the storage service provider.
*
2014-04-07 23:18:51 +00:00
* @options {Object} options The options to create a provider; see below.
2014-04-07 23:32:12 +00:00
* @prop {String} provider Storage service provider. Must be one of:
* <ul><li>'filesystem' - local file system.</li>
* <li>'amazon'</li>
* <li>'rackspace'</li>
* <li>'azure'</li>
* <li>'openstack'</li>
* </ul>
*
* Other supported values depend on the provider.
* See the [documentation](http://docs.strongloop.com/display/DOC/Storage+service) for more information.
2014-03-20 21:03:27 +00:00
* @class
2014-01-10 19:34:37 +00:00
*/
function StorageService(options) {
if (!(this instanceof StorageService)) {
return new StorageService(options);
}
this.provider = options.provider;
this.client = factory.createClient(options);
}
function map(obj) {
2014-01-14 18:39:02 +00:00
return obj;
/*
2014-01-24 17:44:58 +00:00
if (!obj || typeof obj !== 'object') {
return obj;
}
var data = {};
for (var i in obj) {
if (obj.hasOwnProperty(i) && typeof obj[i] !== 'function'
&& typeof obj[i] !== 'object') {
if (i === 'newListener' || i === 'delimiter' || i === 'wildcard') {
// Skip properties from the base class
continue;
}
data[i] = obj[i];
}
}
return data;
*/
2014-01-10 19:34:37 +00:00
}
2014-03-20 21:03:27 +00:00
/**
* List all storage service containers.
2014-04-07 23:18:51 +00:00
* @callback {Function} callback Callback function. See below.
* @param err {String} Error message
* @param containers {Object} object holding all containers.
2014-03-20 21:03:27 +00:00
*/
2014-01-10 19:34:37 +00:00
StorageService.prototype.getContainers = function (cb) {
2014-01-14 18:26:09 +00:00
this.client.getContainers(function (err, containers) {
if (err) {
2014-01-10 19:34:37 +00:00
cb(err, containers);
} else {
2014-01-14 18:26:09 +00:00
cb(err, containers.map(function (c) {
return map(c);
2014-01-10 19:34:37 +00:00
}));
}
});
};
2014-03-20 21:03:27 +00:00
/**
* Create a new storage service container. Other option properties depend on the provider.
*
2014-04-07 23:18:51 +00:00
* @options {Object} options The options to create a provider; see below.
* @prop {String} provider Storage service provider. Must be one of:
* <ul><li>'filesystem' - local file system.</li>
* <li>'amazon'</li>
* <li>'rackspace'</li>
* <li>'azure'</li>
* <li>'openstack'</li>
* </ul>
*
2014-04-07 23:32:12 +00:00
* Other supported values depend on the provider.
* See the [documentation](http://docs.strongloop.com/display/DOC/Storage+service) for more information.
2014-04-07 23:18:51 +00:00
* @callback {Function} callback Callback function.
2014-03-20 21:03:27 +00:00
*/
2014-01-10 19:34:37 +00:00
StorageService.prototype.createContainer = function (options, cb) {
options = options || {};
if ('object' === typeof options && !(options instanceof storage.Container)) {
var Container = factory.getProvider(this.provider).Container;
options = new Container(this.client, options);
}
2014-01-14 18:26:09 +00:00
return this.client.createContainer(options, function (err, container) {
2014-01-10 19:34:37 +00:00
return cb(err, map(container));
});
};
2014-03-20 21:03:27 +00:00
/**
* Destroy an existing storage service container.
* @param {Object} container Container object.
2014-04-07 23:18:51 +00:00
* @callback {Function} callback Callback function.
2014-03-20 21:03:27 +00:00
*/
2014-01-10 19:34:37 +00:00
StorageService.prototype.destroyContainer = function (container, cb) {
return this.client.destroyContainer(container, cb);
};
2014-03-20 21:03:27 +00:00
/**
* Look up a container by name.
* @param {Object} container Container object.
2014-04-07 23:18:51 +00:00
* @callback {Function} callback Callback function.
2014-03-20 21:03:27 +00:00
*/
2014-01-10 19:34:37 +00:00
StorageService.prototype.getContainer = function (container, cb) {
2014-01-14 18:26:09 +00:00
return this.client.getContainer(container, function (err, container) {
2014-01-10 19:34:37 +00:00
return cb(err, map(container));
});
};
2014-03-20 21:03:27 +00:00
/**
* Get the stream for uploading
* @param {Object} container Container object.
2014-04-07 23:18:51 +00:00
* @param {String} file <!-- IS THIS PATH TO A FILE OR FILE OBJ? -->
2014-03-20 21:03:27 +00:00
* @options options See below.
2014-04-07 23:18:51 +00:00
* @prop TBD
* @callback callback Callback function
2014-03-20 21:03:27 +00:00
*/
2014-01-10 19:34:37 +00:00
StorageService.prototype.uploadStream = function (container, file, options, cb) {
if (!cb && typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
if (container) {
options.container = container;
}
if (file) {
options.remote = file;
}
2014-01-10 19:34:37 +00:00
return this.client.upload(options, cb);
};
2014-03-20 21:03:27 +00:00
/**
* Get the stream for downloading.
* @param {Object} container Container object.
* @param {String} file Path to file.
2014-04-07 23:18:51 +00:00
* @options {Object} options See below.
* @prop TBD <!-- What are the options? -->
2014-03-20 21:03:27 +00:00
* @param {Function} callback Callback function
*/
2014-01-10 19:34:37 +00:00
StorageService.prototype.downloadStream = function (container, file, options, cb) {
if (!cb && typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
if (container) {
options.container = container;
}
if (file) {
options.remote = file;
}
2014-01-10 19:34:37 +00:00
return this.client.download(options, cb);
};
2014-03-20 21:03:27 +00:00
/**
* List all files within the given container.
* @param {Object} container Container object.
2014-04-07 23:18:51 +00:00
* @param {Function} download <!-- What is this? -->
* @callback {Function} callback Callback function
2014-03-20 21:03:27 +00:00
*/
2014-01-10 19:34:37 +00:00
StorageService.prototype.getFiles = function (container, download, cb) {
2014-01-14 18:26:09 +00:00
return this.client.getFiles(container, download, function (err, files) {
if (err) {
2014-01-10 19:34:37 +00:00
cb(err, files);
} else {
2014-01-14 18:26:09 +00:00
cb(err, files.map(function (f) {
return map(f);
2014-01-10 19:34:37 +00:00
}));
}
});
};
StorageService.prototype.getFile = function (container, file, cb) {
2014-01-14 18:26:09 +00:00
return this.client.getFile(container, file, function (err, f) {
2014-01-10 19:34:37 +00:00
return cb(err, map(f));
});
};
StorageService.prototype.removeFile = function (container, file, cb) {
return this.client.removeFile(container, file, cb);
};
StorageService.prototype.upload = function (req, res, cb) {
return handler.upload(this.client, req, res, req.params.container, cb);
};
2014-01-14 18:26:09 +00:00
StorageService.prototype.download = function (container, file, res, cb) {
return handler.download(this.client, null, res, container, file, cb);
2014-01-10 19:34:37 +00:00
};
StorageService.modelName = 'storage';
StorageService.prototype.getContainers.shared = true;
StorageService.prototype.getContainers.accepts = [];
StorageService.prototype.getContainers.returns = {arg: 'containers', type: 'array', root: true};
2014-01-14 18:26:09 +00:00
StorageService.prototype.getContainers.http =
{verb: 'get', path: '/'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.getContainer.shared = true;
StorageService.prototype.getContainer.accepts = [
{arg: 'container', type: 'string'}
];
StorageService.prototype.getContainer.returns = {arg: 'container', type: 'object', root: true};
2014-01-14 18:26:09 +00:00
StorageService.prototype.getContainer.http =
{verb: 'get', path: '/:container'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.createContainer.shared = true;
StorageService.prototype.createContainer.accepts = [
2014-02-03 18:58:37 +00:00
{arg: 'options', type: 'object', http: {source: 'body'}}
2014-01-10 19:34:37 +00:00
];
StorageService.prototype.createContainer.returns = {arg: 'container', type: 'object', root: true};
2014-01-14 18:26:09 +00:00
StorageService.prototype.createContainer.http =
{verb: 'post', path: '/'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.destroyContainer.shared = true;
StorageService.prototype.destroyContainer.accepts = [
{arg: 'container', type: 'string'}
];
StorageService.prototype.destroyContainer.returns = {};
2014-01-14 18:26:09 +00:00
StorageService.prototype.destroyContainer.http =
{verb: 'delete', path: '/:container'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.getFiles.shared = true;
StorageService.prototype.getFiles.accepts = [
{arg: 'container', type: 'string'}
];
StorageService.prototype.getFiles.returns = {arg: 'files', type: 'array', root: true};
2014-01-14 18:26:09 +00:00
StorageService.prototype.getFiles.http =
{verb: 'get', path: '/:container/files'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.getFile.shared = true;
StorageService.prototype.getFile.accepts = [
{arg: 'container', type: 'string'},
{arg: 'file', type: 'string'}
];
StorageService.prototype.getFile.returns = {arg: 'file', type: 'object', root: true};
2014-01-14 18:26:09 +00:00
StorageService.prototype.getFile.http =
{verb: 'get', path: '/:container/files/:file'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.removeFile.shared = true;
StorageService.prototype.removeFile.accepts = [
{arg: 'container', type: 'string'},
{arg: 'file', type: 'string'}
];
StorageService.prototype.removeFile.returns = {};
2014-01-14 18:26:09 +00:00
StorageService.prototype.removeFile.http =
{verb: 'delete', path: '/:container/files/:file'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.upload.shared = true;
StorageService.prototype.upload.accepts = [
2014-01-14 18:26:09 +00:00
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
2014-01-10 19:34:37 +00:00
];
StorageService.prototype.upload.returns = {arg: 'result', type: 'object'};
2014-01-14 18:26:09 +00:00
StorageService.prototype.upload.http =
{verb: 'post', path: '/:container/upload'};
2014-01-10 19:34:37 +00:00
StorageService.prototype.download.shared = true;
StorageService.prototype.download.accepts = [
2014-01-14 18:26:09 +00:00
{arg: 'container', type: 'string', 'http': {source: 'path'}},
{arg: 'file', type: 'string', 'http': {source: 'path'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
2014-01-10 19:34:37 +00:00
];
2014-01-14 18:26:09 +00:00
StorageService.prototype.download.http =
{verb: 'get', path: '/:container/download/:file'};