This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
webdav.js
121 lines (102 loc) · 3.12 KB
/
webdav.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// A raw WebDAV interface
var WebDav = {
GET: function(url, callback) {
return this.request('GET', url, null, 'text', callback);
},
PROPFIND: function(url, callback) {
return this.request('PROPFIND', url, null, 'xml', callback);
},
MKCOL: function(url, callback) {
return this.request('MKCOL', url, null, 'text', callback);
},
DELETE: function(url, callback) {
return this.request('DELETE', url, null, 'text', callback);
},
PUT: function(url, data, callback) {
return this.request('PUT', url, data, 'text', callback);
},
request: function(verb, url, data, type, callback) {
var xhr = new XMLHttpRequest();
var body = function() {
var b = xhr.responseText;
if (type == 'xml') {
var xml = xhr.responseXML;
if(xml) {
b = xml.firstChild.nextSibling ? xml.firstChild.nextSibling : xml.firstChild;
}
}
return b;
};
var async = !(callback === null);
if(async) {
xhr.onreadystatechange = function(readyState) {
if(readyState == 4 || readyState === null) { // complete. (null check is workaround for env.rhino.js).
if(body()) { // Workaround for env.rhino.js
callback(body());
}
}
};
}
xhr.open(verb, url, async);
xhr.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
xhr.send(data);
if(!async) {
return body();
}
}
};
// An Object-oriented API around WebDav.
WebDav.Fs = {
file: function(url) {
this.url = url;
this.read = function(callback) {
return WebDav.GET(url, callback);
};
this.write = function(data, callback) {
return WebDav.PUT(url, data, callback);
};
this.rm = function(callback) {
return WebDav.DELETE(url, callback);
};
return this;
},
dir: function(url) {
this.url = url;
this.children = function(callback) {
var childrenFunc = function(doc) {
if(doc.childNodes === null) {
throw('No such directory: ' + url);
}
var result = [];
for(var i=0; i< doc.childNodes.length; i++) {
var response = doc.childNodes[i];
var href = response.getElementsByTagName('D:href')[0].firstChild.nodeValue;
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];
if(collection) {
result[i] = new WebDav.Fs.dir(href);
} else {
result[i] = new WebDav.Fs.file(href);
}
}
return result;
};
if(callback) {
WebDav.PROPFIND(url, function(doc) {
callback(childrenFunc(doc));
});
} else {
return childrenFunc(WebDav.PROPFIND(url));
}
};
this.rm = function(callback) {
return WebDav.DELETE(url, callback);
};
this.mkdir = function(callback) {
return WebDav.MKCOL(url, callback);
};
return this;
}
};