Fix the metadata interception

This commit is contained in:
Raymond Feng 2014-03-31 16:30:35 -07:00
parent 73474ce67a
commit f9d9fe4061
6 changed files with 80 additions and 52 deletions

1
example/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
providers-private.json

View File

@ -1,11 +1,17 @@
var StorageService = require('../').StorageService; var StorageService = require('../').StorageService;
var path = require('path'); 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({ var rs = StorageService({
provider: 'rackspace', provider: 'rackspace',
username: providers.rackspace.username, username: providers.rackspace.username,
apiKey: providers.rackspace.apiKey apiKey: providers.rackspace.apiKey,
region: providers.rackspace.region
}); });
// Container // Container
@ -16,29 +22,16 @@ rs.getContainers(function (err, containers) {
return; return;
} }
containers.forEach(function (c) { containers.forEach(function (c) {
console.log('rackspace: ', c.name); console.log('rackspace: ', c.toJSON());
c.getFiles(function (err, files) { c.getFiles(function (err, files) {
files.forEach(function (f) { 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({ var s3 = StorageService({
provider: 'amazon', provider: 'amazon',
key: providers.amazon.key, key: providers.amazon.key,
@ -60,10 +53,12 @@ s3.getContainers(function (err, containers) {
}); });
}); });
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var stream = s3.uploadStream('con1', 'test.jpg'); 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({ var local = StorageService({
provider: 'filesystem', provider: 'filesystem',

View File

@ -1,7 +1,8 @@
{ {
"rackspace": { "rackspace": {
"username": "strongloop", "username": "your-rackspace-username",
"apiKey": "your-rackspace-api-key" "apiKey": "your-rackspace-api-key",
"region": "DFW"
}, },
"amazon": { "amazon": {
"key": "your-amazon-key", "key": "your-amazon-key",

View File

@ -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 * Patch the pkgcloud Container/File classes so that the metadata are separately
* stored for JSON serialization * stored for JSON serialization
@ -7,27 +57,8 @@
function patchContainerAndFileClass(provider) { function patchContainerAndFileClass(provider) {
var storageProvider = getProvider(provider).storage; var storageProvider = getProvider(provider).storage;
var Container = storageProvider.Container; patchBaseClass(storageProvider.Container);
var m1 = Container.prototype._setProperties; patchBaseClass(storageProvider.File);
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;
};
} }
/** /**
* Create a client instance based on the options * Create a client instance based on the options

View File

@ -4,14 +4,14 @@ var assert = require('assert');
var path = require('path'); var path = require('path');
function verifyMetadata(fileOrContainer, name) { function verifyMetadata(fileOrContainer, name) {
assert(fileOrContainer.metadata); assert(fileOrContainer.getMetadata());
assert.equal(fileOrContainer.metadata.name, name); assert.equal(fileOrContainer.getMetadata().name, name);
assert(fileOrContainer.metadata.uid === undefined); assert(fileOrContainer.getMetadata().uid === undefined);
assert(fileOrContainer.metadata.gid === undefined); assert(fileOrContainer.getMetadata().gid === undefined);
assert(fileOrContainer.metadata.atime); assert(fileOrContainer.getMetadata().atime);
assert(fileOrContainer.metadata.ctime); assert(fileOrContainer.getMetadata().ctime);
assert(fileOrContainer.metadata.mtime); assert(fileOrContainer.getMetadata().mtime);
assert.equal(typeof fileOrContainer.metadata.size, 'number'); assert.equal(typeof fileOrContainer.getMetadata().size, 'number');
} }
describe('FileSystem based storage provider', function () { describe('FileSystem based storage provider', function () {

View File

@ -20,7 +20,7 @@ describe('Storage service', function () {
it('should create a new container', function (done) { it('should create a new container', function (done) {
storageService.createContainer({name: 'c1'}, function (err, container) { storageService.createContainer({name: 'c1'}, function (err, container) {
assert(!err); assert(!err);
assert(container.metadata); assert(container.getMetadata());
done(err, container); done(err, container);
}); });
}); });
@ -28,7 +28,7 @@ describe('Storage service', function () {
it('should get a container c1', function (done) { it('should get a container c1', function (done) {
storageService.getContainer('c1', function (err, container) { storageService.getContainer('c1', function (err, container) {
assert(!err); assert(!err);
assert(container.metadata); assert(container.getMetadata());
done(err, container); done(err, container);
}); });
}); });
@ -99,7 +99,7 @@ describe('Storage service', function () {
storageService.getFile('c1', 'f1.txt', function (err, f) { storageService.getFile('c1', 'f1.txt', function (err, f) {
assert(!err); assert(!err);
assert.ok(f); assert.ok(f);
assert(f.metadata); assert(f.getMetadata());
done(err, f); done(err, f);
}); });
}); });