-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebdav.js
85 lines (75 loc) · 2.59 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
'use strict';
/*\
title: $:/plugins/cjxgm/tearly/webdav.js
type: application/javascript
module-type: library
\*/
(function (tiddlersList) {
function WebDAV() {}
WebDAV.prototype.put = function(path, text) {
console.log("DAV-PUT", path, { text: text });
return new Promise((resolve, reject) => {
var req = this.xhr(reject,
req => resolve(),
req => {
reject(new Error(req.statusText + "\n" + req.responseText));
});
req.open("PUT", path);
req.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
req.send(text);
});
};
WebDAV.prototype.delete = function(path) {
console.log("DAV-DELETE", path);
return new Promise((resolve, reject) => {
var req = this.xhr(reject,
req => resolve(true),
req => {
if (req.status === 404) { // file not found
resolve(false);
} else {
reject(new Error(req.statusText + "\n" + req.responseText));
}
});
req.open("DELETE", path);
req.send();
});
};
WebDAV.prototype.mkcol = function(path) {
console.log("DAV-MKCOL", path);
if (!path.endsWith("/")) throw new Error("Path for MKCOL must end with a slash: " + path);
return new Promise((resolve, reject) => {
var req = this.xhr(reject,
req => resolve(true),
req => {
if (req.status === 405) { // maybe collection exists already
resolve(false);
} else {
reject(new Error(req.statusText + "\n" + req.responseText));
}
});
req.open("MKCOL", path);
req.send();
});
};
WebDAV.prototype.xhr = function(reject, ok, fail) {
var req = new XMLHttpRequest();
req.onload = function() {
if (this.status >= 200 && this.status < 300) {
ok(this);
} else {
fail(this);
}
};
req.onerror = function (ev) {
console.error(ev);
reject(new Error("interrupted"));
};
req.onabort = function (ev) {
console.error(ev);
reject(new Error("abort"));
};
return req;
}
this.WebDAV = WebDAV;
}).call(exports);