From 18f0e3a58b694c9d87432cf02c5132356b65e369 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sat, 19 Aug 2017 12:30:05 +0200 Subject: [PATCH] Added a method in the server to give the listing of resources --- lib/server/v2/webDAVServer/WebDAVServer.d.ts | 2 ++ lib/server/v2/webDAVServer/WebDAVServer.js | 22 +++++++++++++++ src/server/v2/webDAVServer/WebDAVServer.ts | 29 ++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/lib/server/v2/webDAVServer/WebDAVServer.d.ts b/lib/server/v2/webDAVServer/WebDAVServer.d.ts index d3a9437e..03f97520 100644 --- a/lib/server/v2/webDAVServer/WebDAVServer.d.ts +++ b/lib/server/v2/webDAVServer/WebDAVServer.d.ts @@ -68,6 +68,8 @@ export declare class WebDAVServer { subPath: Path; }; onUnknownMethod(unknownMethod: HTTPMethod): void; + listResources(callback: (paths: string[]) => void): void; + listResources(root: string | Path, callback: (paths: string[]) => void): void; start(port: number): any; start(callback: WebDAVServerStartCallback): any; start(port: number, callback: WebDAVServerStartCallback): any; diff --git a/lib/server/v2/webDAVServer/WebDAVServer.js b/lib/server/v2/webDAVServer/WebDAVServer.js index 29c7e013..4a71d971 100644 --- a/lib/server/v2/webDAVServer/WebDAVServer.js +++ b/lib/server/v2/webDAVServer/WebDAVServer.js @@ -162,6 +162,28 @@ var WebDAVServer = (function () { WebDAVServer.prototype.onUnknownMethod = function (unknownMethod) { this.unknownMethod = unknownMethod; }; + WebDAVServer.prototype.listResources = function (_root, _callback) { + var _this = this; + var root = new Path_1.Path(_callback ? _root : '/'); + var callback = _callback ? _callback : _root; + var output = []; + output.push(root.toString()); + this.getResource(this.createExternalContext(), root, function (e, resource) { + resource.readDir(true, function (e, files) { + if (e || files.length === 0) + return callback(output); + var nb = files.length; + files.forEach(function (fileName) { + var childPath = root.getChildPath(fileName); + _this.listResources(childPath, function (outputs) { + outputs.forEach(function (o) { return output.push(o); }); + if (--nb === 0) + callback(output); + }); + }); + }); + }); + }; WebDAVServer.prototype.start = function (port, callback) { startStop.start.bind(this)(port, callback); }; diff --git a/src/server/v2/webDAVServer/WebDAVServer.ts b/src/server/v2/webDAVServer/WebDAVServer.ts index ce38dfa1..1b970414 100644 --- a/src/server/v2/webDAVServer/WebDAVServer.ts +++ b/src/server/v2/webDAVServer/WebDAVServer.ts @@ -253,6 +253,35 @@ export class WebDAVServer this.unknownMethod = unknownMethod; } + listResources(callback : (paths : string[]) => void) : void + listResources(root : string | Path, callback : (paths : string[]) => void) : void + listResources(_root : string | Path | ((paths : string[]) => void), _callback ?: (paths : string[]) => void) : void + { + const root = new Path(_callback ? _root as (string | Path) : '/'); + const callback = _callback ? _callback : _root as ((paths : string[]) => void); + + const output = []; + output.push(root.toString()); + + this.getResource(this.createExternalContext(), root, (e, resource) => { + resource.readDir(true, (e, files) => { + if(e || files.length === 0) + return callback(output); + + let nb = files.length; + + files.forEach((fileName) => { + const childPath = root.getChildPath(fileName); + this.listResources(childPath, (outputs) => { + outputs.forEach((o) => output.push(o)); + if(--nb === 0) + callback(output); + }) + }) + }); + }); + } + // Start / Stop start(port : number) start(callback : WebDAVServerStartCallback)