Filter out senstive information from files

This commit is contained in:
Raymond Feng 2014-03-28 22:07:46 -07:00
parent a5da7fc9f0
commit c00b48cc6f
2 changed files with 50 additions and 15 deletions

View File

@ -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);

View File

@ -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);
});
});