From c00b48cc6f0bb7e26dfc0cefbc04c742fb67e24b Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 28 Mar 2014 22:07:46 -0700 Subject: [PATCH] Filter out senstive information from files --- lib/providers/filesystem/index.js | 51 ++++++++++++++++++++++--------- test/fs.test.js | 14 +++++++++ 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/lib/providers/filesystem/index.js b/lib/providers/filesystem/index.js index 28d6176..85043eb 100644 --- a/lib/providers/filesystem/index.js +++ b/lib/providers/filesystem/index.js @@ -53,7 +53,24 @@ function validateName(name, cb) { } } -// Container related functions +/*! + * Populate the metadata from file stat into props + * @param {fs.Stats} stat The file stat instance + * @param {Object} props The metadata object + */ +function populateMetadata(stat, props) { + for (var p in stat) { + switch (p) { + case 'size': + case 'atime': + case 'mtime': + case 'ctime': + props[p] = stat[p]; + break; + } + } +} + FileSystemProvider.prototype.getContainers = function (cb) { var self = this; fs.readdir(self.root, function (err, files) { @@ -70,9 +87,7 @@ FileSystemProvider.prototype.getContainers = function (cb) { if (stat.isDirectory()) { var name = files[index]; var props = {name: name}; - for (var p in stat) { - props[p] = stat[p]; - } + populateMetadata(stat, props); var container = new Container(self, props); containers.push(container); } @@ -86,8 +101,20 @@ FileSystemProvider.prototype.getContainers = function (cb) { FileSystemProvider.prototype.createContainer = function (options, cb) { var self = this; var name = options.name; - validateName(name, cb) && fs.mkdir(path.join(this.root, name), options, function (err) { - cb && cb(err, new Container(self, {name: name})); + var dir = path.join(this.root, name); + validateName(name, cb) && fs.mkdir(dir, options, function (err) { + if(err) { + return cb && cb(err); + } + fs.stat(dir, function (err, stat) { + var container = null; + if (!err) { + var props = {name: name}; + populateMetadata(stat, props); + container = new Container(self, props); + } + cb && cb(err, container); + }); }); }; @@ -118,9 +145,7 @@ FileSystemProvider.prototype.getContainer = function (containerName, cb) { var container = null; if (!err) { var props = {name: containerName}; - for (var p in stat) { - props[p] = stat[p]; - } + populateMetadata(stat, props); container = new Container(self, props); } cb && cb(err, container); @@ -186,9 +211,7 @@ FileSystemProvider.prototype.getFiles = function (container, download, cb) { stats.forEach(function (stat, index) { if (stat.isFile()) { var props = {container: container, name: entries[index]}; - for (var p in stat) { - props[p] = stat[p]; - } + populateMetadata(stat, props); var file = new File(self, props); files.push(file); } @@ -208,9 +231,7 @@ FileSystemProvider.prototype.getFile = function (container, file, cb) { var f = null; if (!err) { var props = {container: container, name: file}; - for (var p in stat) { - props[p] = stat[p]; - } + populateMetadata(stat, props); f = new File(self, props); } cb && cb(err, f); diff --git a/test/fs.test.js b/test/fs.test.js index 27f19f3..f853efb 100644 --- a/test/fs.test.js +++ b/test/fs.test.js @@ -3,6 +3,17 @@ var FileSystemProvider = require('../lib/providers/filesystem/index.js').Client; 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'); +} + describe('FileSystem based storage provider', function () { describe('container apis', function () { @@ -33,6 +44,7 @@ describe('FileSystem based storage provider', function () { it('should create a new container', function (done) { client.createContainer({name: 'c1'}, function (err, container) { assert(!err); + verifyMetadata(container, 'c1'); done(err, container); }); }); @@ -40,6 +52,7 @@ describe('FileSystem based storage provider', function () { it('should get a container c1', function (done) { client.getContainer('c1', function (err, container) { assert(!err); + verifyMetadata(container, 'c1'); done(err, container); }); }); @@ -114,6 +127,7 @@ describe('FileSystem based storage provider', function () { client.getFile('c1', 'f1.txt', function (err, f) { assert(!err); assert.ok(f); + verifyMetadata(f, 'f1.txt'); done(err, f); }); });