loopback-component-storage/lib/factory.js

106 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-03-31 23:30:35 +00:00
var pkgcloud = require('pkgcloud');
/*!
2014-03-31 23:30:35 +00:00
* Patch the prototype for a given subclass of Container or File
* @param {Function} cls The subclass
*/
2014-03-31 23:30:35 +00:00
function patchBaseClass(cls) {
var proto = cls.prototype;
var found = false;
// Find the prototype that owns the _setProperties method
while (proto
&& proto.constructor !== pkgcloud.storage.Container
&& proto.constructor !== pkgcloud.storage.File) {
if (proto.hasOwnProperty('_setProperties')) {
found = true;
break;
} else {
proto = Object.getPrototypeOf(proto);
}
}
if (!found) {
proto = cls.prototype;
}
var m1 = proto._setProperties;
proto._setProperties = function (details) {
// Use an empty object to receive the calculated properties from details
var receiver = {};
// Pass in some context as non-enumerable properties
Object.defineProperties(receiver, {
client: {value: this.client},
files: {value: this.files}
});
2014-03-31 23:30:35 +00:00
m1.call(receiver, details);
// Apply the calculated properties to this
for (var p in receiver) {
this[p] = receiver[p];
}
// Keep references to raw and the calculated properties
this._rawMetadata = details;
this._metadata = receiver; // Use _metadata to avoid conflicts
}
2014-03-31 23:30:35 +00:00
proto.toJSON = function () {
return this._metadata;
};
2014-03-31 23:30:35 +00:00
proto.getMetadata = function () {
return this._metadata;
};
2014-03-31 23:30:35 +00:00
proto.getRawMetadata = function () {
return this._rawMetadata;
};
2014-03-31 23:30:35 +00:00
}
/*!
* Patch the pkgcloud Container/File classes so that the metadata are separately
* stored for JSON serialization
*
* @param {String} provider The name of the storage provider
*/
function patchContainerAndFileClass(provider) {
var storageProvider = getProvider(provider).storage;
patchBaseClass(storageProvider.Container);
patchBaseClass(storageProvider.File);
}
/**
* Create a client instance based on the options
* @param options
* @returns {*}
*/
function createClient(options) {
2014-01-24 17:44:58 +00:00
options = options || {};
var provider = options.provider || 'filesystem';
var handler;
2013-06-24 23:57:16 +00:00
2014-01-24 17:44:58 +00:00
try {
// Try to load the provider from providers folder
handler = require('./providers/' + provider);
} catch (err) {
// Fall back to pkgcloud
handler = require('pkgcloud').storage;
}
patchContainerAndFileClass(provider);
2014-01-24 17:44:58 +00:00
return handler.createClient(options);
}
2013-06-24 23:57:16 +00:00
/**
* Look up a provider by name
* @param provider
* @returns {*}
*/
function getProvider(provider) {
2014-01-24 17:44:58 +00:00
try {
// Try to load the provider from providers folder
return require('./providers/' + provider);
} catch (err) {
// Fall back to pkgcloud
return require('pkgcloud').providers[provider];
}
2013-07-02 14:54:58 +00:00
}
module.exports.createClient = createClient;
module.exports.getProvider = getProvider;