diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..5e82b51 --- /dev/null +++ b/example/.gitignore @@ -0,0 +1 @@ +providers-private.json diff --git a/example/app-cloud.js b/example/app-cloud.js index fe9fddc..1a9b625 100644 --- a/example/app-cloud.js +++ b/example/app-cloud.js @@ -1,11 +1,17 @@ var StorageService = require('../').StorageService; var path = require('path'); -var providers = require('./providers.json'); +var providers = null; +try { + providers = require('./providers-private.json'); +} catch(err) { + providers = require('./providers.json'); +} var rs = StorageService({ provider: 'rackspace', username: providers.rackspace.username, - apiKey: providers.rackspace.apiKey + apiKey: providers.rackspace.apiKey, + region: providers.rackspace.region }); // Container @@ -16,29 +22,16 @@ rs.getContainers(function (err, containers) { return; } containers.forEach(function (c) { - console.log('rackspace: ', c.name); + console.log('rackspace: ', c.toJSON()); c.getFiles(function (err, files) { files.forEach(function (f) { - console.log('....', f.name); + console.log('....', f.toJSON()); }); }); }); }); /* - client.createContainer(options, function (err, container) { }); - client.destroyContainer(containerName, function (err) { }); - client.getContainer(containerName, function (err, container) { }); - - // File - - client.upload(options, function (err) { }); - client.download(options, function (err) { }); - client.getFiles(container, function (err, files) { }); - client.getFile(container, file, function (err, server) { }); - client.removeFile(container, file, function (err) { }); - */ - var s3 = StorageService({ provider: 'amazon', key: providers.amazon.key, @@ -60,10 +53,12 @@ s3.getContainers(function (err, containers) { }); }); + var fs = require('fs'); var path = require('path'); var stream = s3.uploadStream('con1', 'test.jpg'); -var input = fs.createReadStream(path.join(__dirname, 'test.jpg')).pipe(stream); +fs.createReadStream(path.join(__dirname, 'test.jpg')).pipe(stream); +*/ var local = StorageService({ provider: 'filesystem', diff --git a/example/providers.json b/example/providers.json index 14f76bf..15dd4ac 100644 --- a/example/providers.json +++ b/example/providers.json @@ -1,7 +1,8 @@ { "rackspace": { - "username": "strongloop", - "apiKey": "your-rackspace-api-key" + "username": "your-rackspace-username", + "apiKey": "your-rackspace-api-key", + "region": "DFW" }, "amazon": { "key": "your-amazon-key", diff --git a/lib/factory.js b/lib/factory.js index 503c1f2..daf23f1 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -1,3 +1,53 @@ +var pkgcloud = require('pkgcloud'); + +/*! + * Patch the prototype for a given subclass of Container or File + * @param {Function} cls The subclass + */ +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 = {}; + 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 + } + + proto.toJSON = function () { + return this._metadata; + }; + + proto.getMetadata = function () { + return this._metadata; + }; + + proto.getRawMetadata = function () { + return this._rawMetadata; + }; + +} /*! * Patch the pkgcloud Container/File classes so that the metadata are separately * stored for JSON serialization @@ -7,27 +57,8 @@ function patchContainerAndFileClass(provider) { var storageProvider = getProvider(provider).storage; - var Container = storageProvider.Container; - var m1 = Container.prototype._setProperties; - Container.prototype._setProperties = function (details) { - this.metadata = details; - m1.call(this, details); - } - - Container.prototype.toJSON = function() { - return this.metadata; - }; - - var File = storageProvider.File; - var m2 = File.prototype._setProperties; - File.prototype._setProperties = function (details) { - this.metadata = details; - m2.call(this, details); - }; - - File.prototype.toJSON = function() { - return this.metadata; - }; + patchBaseClass(storageProvider.Container); + patchBaseClass(storageProvider.File); } /** * Create a client instance based on the options diff --git a/test/fs.test.js b/test/fs.test.js index f853efb..e177e0a 100644 --- a/test/fs.test.js +++ b/test/fs.test.js @@ -4,14 +4,14 @@ var assert = require('assert'); var path = require('path'); function verifyMetadata(fileOrContainer, name) { - assert(fileOrContainer.metadata); - assert.equal(fileOrContainer.metadata.name, name); - assert(fileOrContainer.metadata.uid === undefined); - assert(fileOrContainer.metadata.gid === undefined); - assert(fileOrContainer.metadata.atime); - assert(fileOrContainer.metadata.ctime); - assert(fileOrContainer.metadata.mtime); - assert.equal(typeof fileOrContainer.metadata.size, 'number'); + assert(fileOrContainer.getMetadata()); + assert.equal(fileOrContainer.getMetadata().name, name); + assert(fileOrContainer.getMetadata().uid === undefined); + assert(fileOrContainer.getMetadata().gid === undefined); + assert(fileOrContainer.getMetadata().atime); + assert(fileOrContainer.getMetadata().ctime); + assert(fileOrContainer.getMetadata().mtime); + assert.equal(typeof fileOrContainer.getMetadata().size, 'number'); } describe('FileSystem based storage provider', function () { diff --git a/test/storage-service.test.js b/test/storage-service.test.js index fd6f348..2cdf2cb 100644 --- a/test/storage-service.test.js +++ b/test/storage-service.test.js @@ -20,7 +20,7 @@ describe('Storage service', function () { it('should create a new container', function (done) { storageService.createContainer({name: 'c1'}, function (err, container) { assert(!err); - assert(container.metadata); + assert(container.getMetadata()); done(err, container); }); }); @@ -28,7 +28,7 @@ describe('Storage service', function () { it('should get a container c1', function (done) { storageService.getContainer('c1', function (err, container) { assert(!err); - assert(container.metadata); + assert(container.getMetadata()); done(err, container); }); }); @@ -99,7 +99,7 @@ describe('Storage service', function () { storageService.getFile('c1', 'f1.txt', function (err, f) { assert(!err); assert.ok(f); - assert(f.metadata); + assert(f.getMetadata()); done(err, f); }); });