From a5da7fc9f0535629c67a0a498ade62cb1bc87b71 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 28 Mar 2014 17:55:01 -0700 Subject: [PATCH] Separate the metadata in Container/File class --- lib/factory.js | 33 ++++++++++++++++++++++++++++++- lib/providers/filesystem/index.js | 2 ++ lib/storage-service.js | 23 ++++++++++++--------- test/storage-service.test.js | 3 +++ 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index dcea329..503c1f2 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -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 * @param options @@ -15,7 +46,7 @@ function createClient(options) { // Fall back to pkgcloud handler = require('pkgcloud').storage; } - + patchContainerAndFileClass(provider); return handler.createClient(options); } diff --git a/lib/providers/filesystem/index.js b/lib/providers/filesystem/index.js index 12a7c70..28d6176 100644 --- a/lib/providers/filesystem/index.js +++ b/lib/providers/filesystem/index.js @@ -8,6 +8,8 @@ var fs = require('fs'), File = require('./file').File, Container = require('./container').Container; +module.exports.storage = module.exports; // To make it consistent with pkgcloud + module.exports.File = File; module.exports.Container = Container; module.exports.Client = FileSystemProvider; diff --git a/lib/storage-service.js b/lib/storage-service.js index f4b3a98..621722a 100644 --- a/lib/storage-service.js +++ b/lib/storage-service.js @@ -3,9 +3,6 @@ var handler = require('./storage-handler'); var storage = require('pkgcloud').storage; -var Container = require('./models/container'); -var File = require('./models/file'); - module.exports = StorageService; /** @@ -58,7 +55,7 @@ StorageService.prototype.getContainers = function (cb) { cb(err, containers); } else { 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 || {}; - if (container) options.container = container; - if (file) options.remote = file; + if (container) { + options.container = container; + } + if (file) { + options.remote = file; + } return this.client.upload(options, cb); }; @@ -138,8 +139,12 @@ StorageService.prototype.downloadStream = function (container, file, options, cb options = {}; } options = options || {}; - if (container) options.container = container; - if (file) options.remote = file; + if (container) { + options.container = container; + } + if (file) { + options.remote = file; + } return this.client.download(options, cb); }; @@ -156,7 +161,7 @@ StorageService.prototype.getFiles = function (container, download, cb) { cb(err, files); } else { cb(err, files.map(function (f) { - return new File(map(f)); + return map(f); })); } }); diff --git a/test/storage-service.test.js b/test/storage-service.test.js index f2ea79b..fd6f348 100644 --- a/test/storage-service.test.js +++ b/test/storage-service.test.js @@ -20,6 +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); done(err, container); }); }); @@ -27,6 +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); done(err, container); }); }); @@ -97,6 +99,7 @@ describe('Storage service', function () { storageService.getFile('c1', 'f1.txt', function (err, f) { assert(!err); assert.ok(f); + assert(f.metadata); done(err, f); }); });