2016-05-03 23:18:18 +00:00
|
|
|
// Copyright IBM Corp. 2013,2014. All Rights Reserved.
|
|
|
|
// Node module: loopback-component-storage
|
|
|
|
// This file is licensed under the Artistic License 2.0.
|
|
|
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
|
|
|
|
2014-03-31 23:30:35 +00:00
|
|
|
var pkgcloud = require('pkgcloud');
|
|
|
|
|
2014-03-29 00:55:01 +00:00
|
|
|
/*!
|
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-29 00:55:01 +00:00
|
|
|
*/
|
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;
|
2016-10-18 22:06:55 +00:00
|
|
|
proto._setProperties = function(details) {
|
2014-03-31 23:30:35 +00:00
|
|
|
// Use an empty object to receive the calculated properties from details
|
|
|
|
var receiver = {};
|
2014-06-24 22:53:26 +00:00
|
|
|
// Pass in some context as non-enumerable properties
|
|
|
|
Object.defineProperties(receiver, {
|
|
|
|
client: {value: this.client},
|
2016-10-18 22:06:55 +00:00
|
|
|
files: {value: this.files},
|
2014-06-24 22:53:26 +00:00
|
|
|
});
|
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
|
2016-10-18 22:06:55 +00:00
|
|
|
};
|
2014-03-29 00:55:01 +00:00
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
proto.toJSON = function() {
|
2014-03-31 23:30:35 +00:00
|
|
|
return this._metadata;
|
2014-03-29 00:55:01 +00:00
|
|
|
};
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
proto.getMetadata = function() {
|
2014-03-31 23:30:35 +00:00
|
|
|
return this._metadata;
|
2014-03-29 00:55:01 +00:00
|
|
|
};
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
proto.getRawMetadata = function() {
|
2014-03-31 23:30:35 +00:00
|
|
|
return this._rawMetadata;
|
2014-03-29 00:55:01 +00:00
|
|
|
};
|
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);
|
2014-03-29 00:55:01 +00:00
|
|
|
}
|
2013-07-24 19:07:46 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2014-03-29 00:55:01 +00:00
|
|
|
patchContainerAndFileClass(provider);
|
2014-01-24 17:44:58 +00:00
|
|
|
return handler.createClient(options);
|
2013-07-24 19:07:46 +00:00
|
|
|
}
|
2013-06-24 23:57:16 +00:00
|
|
|
|
2013-07-24 19:07:46 +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
|
|
|
}
|
|
|
|
|
2013-07-24 19:07:46 +00:00
|
|
|
module.exports.createClient = createClient;
|
2013-10-30 00:54:32 +00:00
|
|
|
module.exports.getProvider = getProvider;
|