Separate the metadata in Container/File class

This commit is contained in:
Raymond Feng 2014-03-28 17:55:01 -07:00
parent d419e16169
commit a5da7fc9f0
4 changed files with 51 additions and 10 deletions

View File

@ -1,3 +1,34 @@
/*!
* 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;
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;
};
}
/** /**
* Create a client instance based on the options * Create a client instance based on the options
* @param options * @param options
@ -15,7 +46,7 @@ function createClient(options) {
// Fall back to pkgcloud // Fall back to pkgcloud
handler = require('pkgcloud').storage; handler = require('pkgcloud').storage;
} }
patchContainerAndFileClass(provider);
return handler.createClient(options); return handler.createClient(options);
} }

View File

@ -8,6 +8,8 @@ var fs = require('fs'),
File = require('./file').File, File = require('./file').File,
Container = require('./container').Container; Container = require('./container').Container;
module.exports.storage = module.exports; // To make it consistent with pkgcloud
module.exports.File = File; module.exports.File = File;
module.exports.Container = Container; module.exports.Container = Container;
module.exports.Client = FileSystemProvider; module.exports.Client = FileSystemProvider;

View File

@ -3,9 +3,6 @@ var handler = require('./storage-handler');
var storage = require('pkgcloud').storage; var storage = require('pkgcloud').storage;
var Container = require('./models/container');
var File = require('./models/file');
module.exports = StorageService; module.exports = StorageService;
/** /**
@ -58,7 +55,7 @@ StorageService.prototype.getContainers = function (cb) {
cb(err, containers); cb(err, containers);
} else { } else {
cb(err, containers.map(function (c) { cb(err, containers.map(function (c) {
return new Container(map(c)); return map(c);
})); }));
} }
}); });
@ -119,8 +116,12 @@ StorageService.prototype.uploadStream = function (container, file, options, cb)
options = {}; options = {};
} }
options = options || {}; options = options || {};
if (container) options.container = container; if (container) {
if (file) options.remote = file; options.container = container;
}
if (file) {
options.remote = file;
}
return this.client.upload(options, cb); return this.client.upload(options, cb);
}; };
@ -138,8 +139,12 @@ StorageService.prototype.downloadStream = function (container, file, options, cb
options = {}; options = {};
} }
options = options || {}; options = options || {};
if (container) options.container = container; if (container) {
if (file) options.remote = file; options.container = container;
}
if (file) {
options.remote = file;
}
return this.client.download(options, cb); return this.client.download(options, cb);
}; };
@ -156,7 +161,7 @@ StorageService.prototype.getFiles = function (container, download, cb) {
cb(err, files); cb(err, files);
} else { } else {
cb(err, files.map(function (f) { cb(err, files.map(function (f) {
return new File(map(f)); return map(f);
})); }));
} }
}); });

View File

@ -20,6 +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);
done(err, container); done(err, container);
}); });
}); });
@ -27,6 +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);
done(err, container); done(err, container);
}); });
}); });
@ -97,6 +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);
done(err, f); done(err, f);
}); });
}); });