Skip to content

Commit

Permalink
Implemented PROPFIND with 'xml-js'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 14, 2017
1 parent c4f206e commit 87878cd
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 41 deletions.
74 changes: 54 additions & 20 deletions lib/server/commands/Propfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var Resource_1 = require("../../resource/Resource");
var xml = require("xmlbuilder");
var XML_1 = require("../../helper/XML");
function default_1(arg, callback) {
arg.getResource(function (e, resource) {
if (e || !resource) {
arg.setCode(WebDAVRequest_1.HTTPCodes.NotFound);
callback();
return;
}
var multistatus = xml.create('D:multistatus', ['xmlns:D="DAV:"']);
var multistatus = XML_1.XML.createElement('D:multistatus', {
'xmlns:D': 'DAV:'
});
resource.type(function (e, type) {
if (!type.isDirectory || arg.depth === 0) {
addXMLInfo(resource, multistatus, function () { return done(multistatus); });
return;
}
resource.getChildren(function (e, children) {
var nb = children.length + 1;
function nbOut() {
function nbOut(error) {
if (nb > 0 && error) {
nb = -1;
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
--nb;
if (nb === 0)
done(multistatus);
Expand All @@ -32,28 +40,40 @@ function default_1(arg, callback) {
function addXMLInfo(resource, multistatus, callback) {
var response = multistatus.ele('D:response');
var propstat = response.ele('D:propstat');
propstat.ele('D:status', null, 'HTTP/1.1 200 OK');
propstat.ele('D:status').add('HTTP/1.1 200 OK');
var prop = propstat.ele('D:prop');
var nb = 7;
function nbOut() {
function nbOut(error) {
if (nb > 0 && error) {
nb = -1;
callback(error);
return;
}
--nb;
if (nb === 0)
callback();
}
resource.creationDate(function (e, ticks) {
prop.ele('D:creationdate', null, arg.dateISO8601(ticks));
nbOut();
if (!e)
prop.ele('D:creationdate').add(arg.dateISO8601(ticks));
nbOut(e);
});
arg.getResourcePath(resource, function (e, path) {
response.ele('D:href', null, arg.fullUri(path).replace(' ', '%20'));
nbOut();
if (!e)
response.ele('D:href').add(arg.fullUri(path).replace(' ', '%20'));
nbOut(e);
});
resource.webName(function (e, name) {
prop.ele('D:displayname', name);
nbOut();
if (!e)
prop.ele('D:displayname').add(name);
nbOut(e);
});
var supportedlock = prop.ele('D:supportedlock');
resource.getAvailableLocks(function (e, lockKinds) {
if (e) {
nbOut(e);
return;
}
lockKinds.forEach(function (lockKind) {
var lockentry = supportedlock.ele('D:lockentry');
var lockscope = lockentry.ele('D:lockscope');
Expand All @@ -64,37 +84,51 @@ function default_1(arg, callback) {
nbOut();
});
resource.getProperties(function (e, properties) {
if (e) {
nbOut(e);
return;
}
for (var name_1 in properties) {
var value = properties[name_1];
prop.ele(name_1, null, value);
prop.ele(name_1).add(value);
}
nbOut();
});
resource.type(function (e, type) {
if (e) {
nbOut(e);
return;
}
var resourcetype = prop.ele('D:resourcetype');
if (type.isDirectory)
resourcetype.ele('D:collection');
if (type.isFile) {
nb += 2;
resource.mimeType(function (e, mimeType) {
prop.ele('D:getcontenttype', null, mimeType);
nbOut();
if (!e)
prop.ele('D:getcontenttype').add(mimeType);
nbOut(e);
});
resource.size(function (e, size) {
prop.ele('D:getcontentlength', null, size);
nbOut();
if (!e)
prop.ele('D:getcontentlength').add(size);
else
console.log(e);
nbOut(e);
});
}
nbOut();
});
resource.lastModifiedDate(function (e, lastModifiedDate) {
prop.ele('D:getetag', null, Resource_1.ETag.createETag(lastModifiedDate));
prop.ele('D:getlastmodified', new Date(lastModifiedDate).toUTCString());
nbOut();
if (!e) {
prop.ele('D:getetag').add(Resource_1.ETag.createETag(lastModifiedDate));
prop.ele('D:getlastmodified').add(new Date(lastModifiedDate).toUTCString());
}
nbOut(e);
});
}
function done(multistatus) {
var content = '<?xml version="1.0" encoding="utf-8" ?>\r\n' + multistatus.toString({ pretty: false });
var content = XML_1.XML.toXML(multistatus);
arg.setCode(WebDAVRequest_1.HTTPCodes.MultiStatus);
arg.response.setHeader('Content-Type', 'text/xml; charset="utf-8"');
arg.response.setHeader('Content-Length', content.length.toString());
Expand Down
85 changes: 64 additions & 21 deletions src/server/commands/Propfind.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'
import { IResource, ETag } from '../../resource/Resource'
import * as xml from 'xmlbuilder'
import { XML } from '../../helper/XML'

export default function(arg : MethodCallArgs, callback)
{
Expand All @@ -12,7 +12,9 @@ export default function(arg : MethodCallArgs, callback)
return;
}

const multistatus = xml.create('D:multistatus', ['xmlns:D="DAV:"'])
const multistatus = XML.createElement('D:multistatus', {
'xmlns:D': 'DAV:'
})

resource.type((e, type) => {
if(!type.isDirectory || arg.depth === 0)
Expand All @@ -24,8 +26,16 @@ export default function(arg : MethodCallArgs, callback)
resource.getChildren((e, children) => {
let nb = children.length + 1;

function nbOut()
function nbOut(error)
{
if(nb > 0 && error)
{
nb = -1;
arg.setCode(HTTPCodes.InternalServerError);
callback();
return;
}

--nb;
if(nb === 0)
done(multistatus);
Expand All @@ -38,42 +48,57 @@ export default function(arg : MethodCallArgs, callback)
})
})
})

function addXMLInfo(resource, multistatus, callback)
{
const response = multistatus.ele('D:response')

const propstat = response.ele('D:propstat')

propstat.ele('D:status', null, 'HTTP/1.1 200 OK')
propstat.ele('D:status').add('HTTP/1.1 200 OK')

const prop = propstat.ele('D:prop')

let nb = 7;
function nbOut()
function nbOut(error?)
{
if(nb > 0 && error)
{
nb = -1;
callback(error);
return;
}
--nb;
if(nb === 0)
callback();
}

resource.creationDate((e, ticks) => {
prop.ele('D:creationdate', null, arg.dateISO8601(ticks));
nbOut();
if(!e)
prop.ele('D:creationdate').add(arg.dateISO8601(ticks));
nbOut(e);
})

arg.getResourcePath(resource, (e, path) => {
response.ele('D:href', null, arg.fullUri(path).replace(' ', '%20'));
nbOut();
if(!e)
response.ele('D:href').add(arg.fullUri(path).replace(' ', '%20'));
nbOut(e);
})

resource.webName((e, name) => {
prop.ele('D:displayname', name);
nbOut();
if(!e)
prop.ele('D:displayname').add(name);
nbOut(e);
})

const supportedlock = prop.ele('D:supportedlock')
resource.getAvailableLocks((e, lockKinds) => {
if(e)
{
nbOut(e);
return;
}

lockKinds.forEach((lockKind) => {
const lockentry = supportedlock.ele('D:lockentry')

Expand All @@ -87,15 +112,26 @@ export default function(arg : MethodCallArgs, callback)
})

resource.getProperties((e, properties) => {
if(e)
{
nbOut(e);
return;
}

for(const name in properties)
{
const value = properties[name];
prop.ele(name, null, value)
prop.ele(name).add(value)
}
nbOut();
})

resource.type((e, type) => {
if(e)
{
nbOut(e);
return;
}

const resourcetype = prop.ele('D:resourcetype')
if(type.isDirectory)
Expand All @@ -105,28 +141,35 @@ export default function(arg : MethodCallArgs, callback)
{
nb += 2;
resource.mimeType((e, mimeType) => {
prop.ele('D:getcontenttype', null, mimeType)
nbOut();
if(!e)
prop.ele('D:getcontenttype').add(mimeType)
nbOut(e);
})
resource.size((e, size) => {
prop.ele('D:getcontentlength', null, size)
nbOut();
if(!e)
prop.ele('D:getcontentlength').add(size)
else
console.log(e);
nbOut(e);
})
}

nbOut();
})

resource.lastModifiedDate((e, lastModifiedDate) => {
prop.ele('D:getetag', null, ETag.createETag(lastModifiedDate))
prop.ele('D:getlastmodified', new Date(lastModifiedDate).toUTCString())
nbOut();
if(!e)
{
prop.ele('D:getetag').add(ETag.createETag(lastModifiedDate))
prop.ele('D:getlastmodified').add(new Date(lastModifiedDate).toUTCString())
}
nbOut(e);
})
}

function done(multistatus)
{
const content = '<?xml version="1.0" encoding="utf-8" ?>\r\n' + multistatus.toString({pretty: false})
const content = XML.toXML(multistatus);
arg.setCode(HTTPCodes.MultiStatus);
arg.response.setHeader('Content-Type', 'text/xml; charset="utf-8"')
arg.response.setHeader('Content-Length', content.length.toString())
Expand Down

0 comments on commit 87878cd

Please sign in to comment.