Skip to content

Commit

Permalink
Merge pull request #14 from ajaxorg/feature/filelist
Browse files Browse the repository at this point in the history
Feature/filelist
  • Loading branch information
Zef Hemel committed Apr 12, 2012
2 parents 8bc0836 + 08c93d8 commit 5e3d927
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 76 deletions.
84 changes: 44 additions & 40 deletions lib/DAV/plugins/filelist.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/*
* @package jsDAV
* @subpackage DAV
* @copyright Copyright (C) 2010 Mike de Boer. All rights reserved.
* @author Mike de Boer <mike AT ajax DOT org>
* @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
* @author Mike de Boer <info AT mikedeboer DOT nl>
* @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
*/
"use strict";

var jsDAV = require("./../../jsdav");
var jsDAV_ServerPlugin = require("./../plugin").jsDAV_ServerPlugin;
var jsDAV = require("./../../jsdav");
var jsDAV_ServerPlugin = require("./../plugin").jsDAV_ServerPlugin;
var jsDAV_Codesearch_Plugin = require("./codesearch");

var Spawn = require("child_process").spawn;
var Util = require("./../util");
var async = require("asyncjs");

function jsDAV_Filelist_Plugin(handler) {
this.handler = handler;
Expand All @@ -28,7 +29,6 @@ jsDAV_Filelist_Plugin.FIND_CMD = "find";
this.httpReportHandler = function(e, reportName, dom) {
if (reportName != "{DAV:}filelist")
return e.next();

e.stop();

var uri = this.handler.getRequestUri();
Expand All @@ -37,17 +37,17 @@ jsDAV_Filelist_Plugin.FIND_CMD = "find";
options.uri = uri;
this.handler.server.tree.getNodeForPath(uri, function(err, node) {
if (err)
return self.handler.handleError(err);
return e.next(err);

if (jsDAV.debugMode)
Util.log("report" + reportName + ", " + node.path + ", ", options);

self.doFilelist(node, options, function(err, sResults) {
if (!Util.empty(err))
return self.handler.handleError(err);

return e.stop(err);
self.handler.httpResponse.writeHead(207, {"Content-Type":"text/xml; charset=utf-8"});
self.handler.httpResponse.end(sResults);
e.stop();
});
});
};
Expand All @@ -62,41 +62,45 @@ jsDAV_Filelist_Plugin.FIND_CMD = "find";
}
return options;
};

this.doFilelist = function(node, options, cbsearch) {
var self = this;
var output = [];
var re = new RegExp("\\.bzr|\\.cdv|\\.dep|\\.dot|\\.nib|\\.plst|\\.git|\\.hg|\\.pc|\\.svn|blib|CVS|RCS|SCCS|_darcs|_sgbak|autom4te\\.cache|cover_db|_build|\\.tmp");
var excludePattern =
(options.showHiddenFiles == "1" ? "" : "\\/\\.[^\\/]*$|") //Hidden Files
+ ".*(\\.gz|\\.bzr|\\.cdv|\\.dep|\\.dot|\\.nib|\\.plst|_darcs|_sgbak|autom4te\\.cache|cover_db|_build|\\.tmp)$" //File Extensions
+ "|.*\\/(\\.c9revisions|\\.git|\\.hg|\\.pc|\\.svn|blib|CVS|RCS|SCCS|\.DS_Store)(\\/.*|$)"; //File Names

async.walkfiles(node.path, function(item) { return !re.test(item.path); }, async.PREORDER)
.each(function(item){
output.push(item.path);
})
.end(function(err, res) {
cbsearch(err, self.parseSearchResult(output, node.path, options));
});
};

this.parseSearchResult = function(res, basePath, options) {
var namespace, prefix, line;
var aXml = ['<?xml version="1.0" encoding="utf-8"?><d:multistatus'];
var aLines = res;
var i = 0;
var l = aLines.length;
// Adding in default namespaces
for (namespace in this.handler.xmlNamespaces) {
prefix = this.handler.xmlNamespaces[namespace];
aXml.push(' xmlns:' + prefix + '="' + namespace + '"');
var args;
if (process.platform == "darwin") {
// BSD find
args = ["-L", "-E", ".", "-type", "f", "(",
"-a", "!", "-regex",
excludePattern, ")",
"-print"];
}
aXml.push('><d:response query="', Util.escapeXml(options.query, '"'), '">');
for (; i < l; ++i) {
line = Util.trim(aLines[i]);
if (!line) continue;
line = encodeURI(options.uri + Util.rtrim(line.replace(basePath, "")), "/");
if (line && line !== "")
aXml.push('<d:href>' + line + '</d:href>');
else {
// GNU find
args = ["-L", ".", "-type", "f",
"-a", "!", "-regex", excludePattern,
"-regextype", "posix-extended", "-print"];
}
return aXml.join("") + '</d:response></d:multistatus>';
if (jsDAV.debugMode)
Util.log("search command: find " + args.join(" "));
var err = "",
out = "",
find = Spawn(jsDAV_Filelist_Plugin.FIND_CMD, args, {
cwd: node.path
});
find.stdout.on("data", function(data) {
if (!Util.empty(data))
out += data;
});
find.stderr.on("data", function(data) {
if (!Util.empty(data))
err += data;
});
find.on("exit", function(code) {
cbsearch(err, out);
});
};
}).call(jsDAV_Filelist_Plugin.prototype = new jsDAV_ServerPlugin());

Expand Down
83 changes: 48 additions & 35 deletions test/test_filelist.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
/*
* @package jsDAV
* @subpackage DAV
* @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax.org>
* @author Ruben Daniels <ruben AT c9 DOT io>
* @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
*/
"use strict";

var Http = require("http");
var Async = require("./../node_modules/asyncjs");
var assert = require("assert");
var jsDAV = require("./../lib/jsdav");
var jsDAV_Filelist_Plugin = require("./../lib/DAV/plugins/filelist");

var options = {
host: "localhost",
port: 8000,
method: "REPORT",
path: "/"
};
jsDAV.debugMode = true;

Async.range(1, 1000)
.each(function(num) {
var req = Http.request(options, function(res) {
if (res.statusCode != 207)
return next("Invalid status code! " + res.statusCode);

console.log("[" + num + "] status: " + res.statusCode);
//console.log("[" + num + "] headers: " + JSON.stringify(res.headers));
res.setEncoding("utf8");
res.on("data", function(chunk) {
console.log("[" + num + "] body: " + chunk);
});

res.on("end", function() {
//next();
});
module.exports = {
timeout: 30000,

setUpSuite: function(next) {
this.plugin = new jsDAV_Filelist_Plugin({
addEventListener : function(){}
});
next();
},

tearDownSuite: function(next) {

req.on("error", function(e) {
console.log("problem with request: " + e.message);
next();
},

"test retrieving a file list": function(next) {
this.plugin.doFilelist({path: "./"}, {}, function(err, out){
assert.ok(out.indexOf("test_filelist.js") > -1);

next();
});

// write data to request body
req.write('<?xml version="1.0" encoding="utf-8" ?>\n');
req.write('<D:filelist xmlns:D="DAV:"></D:filelist>');
req.end();
})
.end(function(err) {
console.log("DONE");
});
},

"test retrieving a file list including hidden files": function(next) {
this.plugin.doFilelist({path: "../"}, {
showHiddenFiles: "1"
}, function(err, out){
assert.ok(out.indexOf(".gitignore") > -1);

next();
});
}
};

process.on("exit", function() {
if (module.exports.conn)
module.exports.conn.end();
});

!module.parent && require("./../node_modules/asyncjs/lib/test").testcase(module.exports).exec();
2 changes: 1 addition & 1 deletion test/test_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jsDAV.debugMode = true;
jsDAV.createServer({
node: __dirname + "/assets",
plugins: ["auth", /*"browser", */"codesearch", "filelist", "filesearch", "locks", "mount", "temporaryfilefilter"]
}, 8000);
}, 8000);

0 comments on commit 5e3d927

Please sign in to comment.