Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 52 additions & 26 deletions src/webdav.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// A raw WebDAV interface
// callbacks are all : function(body,error,xhrobj).
var WebDAV = {
GET: function(url, callback) {
return this.request('GET', url, {}, null, 'text', callback);
Expand All @@ -20,6 +21,15 @@ var WebDAV = {
return this.request('PUT', url, {}, data, 'text', callback);
},

COPY: function(url, desturl, callback) {
return this.request('COPY',url, {"Destination":desturl, "Depth":'infinity'}, null, 'text', callback);
},

MOVE: function(url, desturl, callback) {
return this.request('MOVE',url, {"Destination":desturl, "Depth":'infinity'}, null, 'text', callback);
},


request: function(verb, url, headers, data, type, callback) {
var xhr = new XMLHttpRequest();
var body = function() {
Expand All @@ -36,15 +46,12 @@ var WebDAV = {
if(callback) {
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) { // complete.
var b = body();
if(b) {
callback(b);
}
callback((body() || ""), xhr.status >= 400, xhr);
}
};
}
xhr.open(verb, url, !!callback);
xhr.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
if(data != null) xhr.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
Expand All @@ -58,13 +65,36 @@ var WebDAV = {

// An Object-oriented API around WebDAV.
WebDAV.Fs = function(rootUrl) {
this.rootUrl = rootUrl;
//Make url absolute
if(!/^http/.test(rootUrl)) rootUrl = location.protocol + '//' + location.host + rootUrl;

this.rootUrl = rootUrl.replace(/\/$/, ''); // Strip trailing slash;

var fs = this;

this.file = function(href) {
function addcommon(obj) {
//Copy: callback will receive the new file/dir object or undefined if error occurs with xhr object to fetch details
obj.copy = function(desturl, callback) {
return WebDAV.COPY(this.url, fs.urlFor(desturl), function(body,error,xhr) {
callback(error ? undefined : fs[obj.type](desturl),xhr);
});
}

obj.move = function(desturl, callback) {
return WebDAV.MOVE(this.url, fs.urlFor(desturl), function(body,error,xhr) {
callback(error ? undefined : fs[obj.type](desturl),xhr);
});
}

this.rm = function(callback) {
return WebDAV.DELETE(this.url, callback);
}
}

this.file = function(href, urlisabsolute) {
this.type = 'file';

this.url = fs.urlFor(href);
this.url = urlisabsolute ? href : fs.urlFor(href);;

this.name = fs.nameFor(this.url);

Expand All @@ -76,17 +106,15 @@ WebDAV.Fs = function(rootUrl) {
return WebDAV.PUT(this.url, data, callback);
};

this.rm = function(callback) {
return WebDAV.DELETE(this.url, callback);
};

addcommon(this);

return this;
};

this.dir = function(href) {
this.dir = function(href,urlisabsolute) {
this.type = 'dir';

this.url = fs.urlFor(href);
this.url = urlisabsolute ? href : fs.urlFor(href);

this.name = fs.nameFor(this.url);

Expand All @@ -97,19 +125,19 @@ WebDAV.Fs = function(rootUrl) {
}
var result = [];
// Start at 1, because the 0th is the same as self.
for(var i=1; i< doc.childNodes.length; i++) {
var response = doc.childNodes[i];
var href = response.getElementsByTagName('D:href')[0].firstChild.nodeValue;
for(var i=1; i< doc.children.length; i++) {
var response = doc.children[i];
var href = response.getElementsByTagNameNS('DAV:','href')[0].firstChild.nodeValue;
href = href.replace(/\/$/, ''); // Strip trailing slash
var propstat = response.getElementsByTagName('D:propstat')[0];
var prop = propstat.getElementsByTagName('D:prop')[0];
var resourcetype = prop.getElementsByTagName('D:resourcetype')[0];
var collection = resourcetype.getElementsByTagName('D:collection')[0];
var propstat = response.getElementsByTagNameNS('DAV:','propstat')[0];
var prop = propstat.getElementsByTagNameNS('DAV:','prop')[0];
var resourcetype = prop.getElementsByTagNameNS('DAV:','resourcetype')[0];
var collection = resourcetype.getElementsByTagNameNS('DAV:','collection')[0];

if(collection) {
result[i-1] = new fs.dir(href);
result[i-1] = new fs.dir(href,true);
} else {
result[i-1] = new fs.file(href);
result[i-1] = new fs.file(href,true);
}
}
return result;
Expand All @@ -124,9 +152,7 @@ WebDAV.Fs = function(rootUrl) {
}
};

this.rm = function(callback) {
return WebDAV.DELETE(this.url, callback);
};
addcommon(this);

this.mkdir = function(callback) {
return WebDAV.MKCOL(this.url, callback);
Expand Down