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

207 lines
6.4 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;
var Container = require('./models/container');
var File = require('./models/file');
module.exports = StorageService;
/**
* @param options The options to create a provider
* @returns {StorageService}
* @constructor
*/
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
}
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) {
2014-01-10 19:34:37 +00:00
return new Container(map(c));
}));
}
});
};
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));
});
};
StorageService.prototype.destroyContainer = function (container, cb) {
return this.client.destroyContainer(container, cb);
};
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));
});
};
// File related functions
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;
return this.client.upload(options, cb);
};
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;
return this.client.download(options, cb);
};
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) {
2014-01-10 19:34:37 +00:00
return new File(map(f));
}));
}
});
};
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 = [
{arg: 'options', type: 'object'}
];
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'};