1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 02:19:55 +00:00

putSaver: probe for server support on startup

save fails until probe succeeds,
allowing other saver methods to take over

Follow up to #2288, #2277
This commit is contained in:
Eric Drechsel 2016-02-16 10:42:36 -08:00
parent 1f4df88960
commit 60d585f7d5

View File

@ -20,23 +20,34 @@ Select the appropriate saver module and set it up
*/
var PutSaver = function(wiki) {
this.wiki = wiki;
var self = this;
// Async server probe. Until probe finishes, save will fail fast
// See also https://github.com/Jermolene/TiddlyWiki5/issues/2276
var req = new XMLHttpRequest();
req.open("OPTIONS", encodeURI(window.location.href));
req.onload = function() {
// Check DAV header http://www.webdav.org/specs/rfc2518.html#rfc.section.9.1
self.serverAcceptsPuts = (this.status === 200 && !!this.getResponseHeader('dav'));
};
req.send();
};
PutSaver.prototype.save = function(text,method,callback) {
if (!this.serverAcceptsPuts) {
return false;
}
var req = new XMLHttpRequest();
// TODO: store/check ETags if supported by server, to protect against overwrites
// Prompt: Do you want to save over this? Y/N
// Merging would be ideal, and may be possible using future generic merge flow
req.addEventListener("load", function() {
req.onload = function() {
if (this.status === 200 || this.status === 201) {
// success
callback(null);
callback(null); // success
}
else {
// fail
callback(this.responseText);
callback(this.responseText); // fail
}
});
};
req.open("PUT", encodeURI(window.location.href));
req.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
req.send(text);
@ -56,10 +67,6 @@ PutSaver.prototype.info = {
Static method that returns true if this saver is capable of working
*/
exports.canSave = function(wiki) {
// TODO This should probe for the DAV header
// http://www.webdav.org/specs/rfc2518.html#rfc.section.9.1
// but doing so would require canSave to resolve async. See
// https://github.com/Jermolene/TiddlyWiki5/issues/2276
return /^https?:/.test(location.protocol);
};