Skip to content

Commit

Permalink
by default, try to retrieve the contents of an unversioned file from …
Browse files Browse the repository at this point in the history
…file system
  • Loading branch information
rksm committed Dec 4, 2014
1 parent b3a37d6 commit 8e7f984
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lively-davfs",
"version": "0.2.6",
"version": "0.2.7",
"description": "Manager for versioning and reading / writing resources.",
"main": "request-handler.js",
"repository": {
Expand Down
43 changes: 33 additions & 10 deletions request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var async = require("async");
var util = require("util");
var Url = require("url");
var Path = require("path");
var fs = require("fs");

var Repository = require('./repository');
var d = require('./domain');
Expand Down Expand Up @@ -35,12 +36,13 @@ util._extend(LivelyFsHandler.prototype, d.bindMethods({
this.resetDatabase = !!options.resetDatabase;
this.repository = new Repository(options);
this.timemachineSettings = (function tmSetup(tmOptions) {
if (!tmOptions) return null;
var allowFileSystemFallback = tmOptions.hasOwnProperty("allowFileSystemFallback") ?
tmOptions.allowFileSystemFallback : true;
var path = tmOptions.path;
if (!path) return null;
if (path[0] !== '/') path = '/' + path;
if (path[path.length-1] !== '/') path += '/';
return {path: path};
return {allowFileSystemFallback: allowFileSystemFallback, path: path};
})(options.timemachine || {path: '/timemachine/'});
},

Expand Down Expand Up @@ -125,30 +127,51 @@ util._extend(LivelyFsHandler.prototype, d.bindMethods({
handleTimemachineRequest: function(req, res, next) {
// req.url is something like '/timemachine/2010-08-07%2015%3A33%3A22/foo/bar.js'
// tmPath = '/timemachine/'

if (req.method.toLowerCase() !== 'get') {
res.status(400).end('timemachine request to ' + req.url + ' not supported.');
return;
}

var tmPath = this.timemachineSettings.path,
allowFileSystemFallback = this.timemachineSettings.allowFileSystemFallback,
repo = this.repository,
versionedPath = req.url.slice(tmPath.length),
version = versionedPath.slice(0, versionedPath.indexOf('/'));

if (!version) {
res.status(400).end('cannot read version from path: ' + req.url);
return;
}

var path = versionedPath.slice(version.length);
if (path[0] === '/') path = path.slice(1);
var ts = this.makeDateAndTime(version);
console.log('timemachine into %s, %sing path %s', ts, req.method, path);
this.repository.getRecords({
paths: [path],
older: ts,
attributes: ['version', 'date', 'author', 'content'],
limit: 1
}, function(err, records) {
if (err) { res.status(500).end(String(err)); return; }
if (!records.length) { res.status(404).end(util.format('Nothing stored for %s at %s', path, ts)); return; }

async.waterfall([
function retrieveVersion(next) {
repo.getRecords({
paths: [path], older: ts,
attributes: ['version', 'date', 'author', 'content'],
limit: 1
}, next);
},
function fileSystemFallback(records, next) {
if (records.length || (!records.length && !allowFileSystemFallback)) {
next(null, records); return;
}
console.log("Filesystem fallback for %s", Path.join(repo.fs.getRootDirectory(), path));
fs.readFile(Path.join(repo.fs.getRootDirectory(), path), function(err, content) {
next(null, err || !content ? [] : [{content: String(content)}]); });
}
], function(err, records) {
if (err) { res.writeHead(500); res.end(String(err)); return; }
if (!records || !records.length) {
res.writeHead(404);
res.end(util.format('Nothing stored for %s at %s', path, ts));
return;
}
res.setHeader('content-type', '*/*;charset=utf8')
var content = records[records.length-1].content;
res.end(content);
Expand Down
32 changes: 32 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ var tests = {
}
], test.done);
},

testTimeMachineHTTPAccess: function(test) {
test.expect(3);
async.series([
Expand Down Expand Up @@ -274,7 +275,38 @@ var tests = {
});
},
], test.done);
},

testTimeMachineHTTPAccessFallsbackToFilesystemIfResourceNotVersioned: function(test) {
test.expect(3);
testRepo.fs.excludedFiles.push(/.*\.ignored/);
async.series([
function(next) {
put('aFile.ignored', 'test');
testRepo.once('synchronized', next);
},
function(next) {
get('aFile.ignored', function(err, content) {
test.equal(content, 'test', 'normal GET of existing resource failed');
next(err);
});
},
function(next) {
testRepo.getVersionsFor('aFile.ignored', function(err, versions) {
test.equal(versions.length, 0, '# version');
next();
});
},

function(next) {
get('timemachine/' + encodeURIComponent('2013-10-12 10:01:01 PDT') + '/aFile.ignored', function(err, content) {
test.equal(content, 'test', 'timemachined GET no filesystem fallback');
next(err);
});
},
], test.done);
}

};

module.exports = tests;

0 comments on commit 8e7f984

Please sign in to comment.