2016-02-08 21:40:10 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/savers/put.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: saver
|
|
|
|
|
|
|
|
Saves wiki by performing a PUT request to the server
|
|
|
|
|
|
|
|
Works with any server which accepts a PUT request
|
|
|
|
to the current URL, such as a WebDAV server.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2018-05-04 10:48:38 +00:00
|
|
|
/*
|
|
|
|
Retrieve ETag if available
|
|
|
|
*/
|
2018-06-13 14:50:47 +00:00
|
|
|
var retrieveETag = function(self) {
|
|
|
|
var headers = {
|
2024-09-06 18:55:51 +00:00
|
|
|
Accept: "*/*"
|
2018-06-13 14:50:47 +00:00
|
|
|
};
|
2018-05-04 10:48:38 +00:00
|
|
|
$tw.utils.httpRequest({
|
|
|
|
url: self.uri(),
|
|
|
|
type: "HEAD",
|
|
|
|
headers: headers,
|
2018-06-13 14:50:47 +00:00
|
|
|
callback: function(err,data,xhr) {
|
|
|
|
if(err) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-05 10:40:41 +00:00
|
|
|
var etag = xhr.getResponseHeader("ETag");
|
2018-06-13 14:50:47 +00:00
|
|
|
if(!etag) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-09 17:04:52 +00:00
|
|
|
self.etag = etag.replace(/^W\//,"");
|
2018-05-04 10:48:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-08 21:40:10 +00:00
|
|
|
/*
|
|
|
|
Select the appropriate saver module and set it up
|
|
|
|
*/
|
|
|
|
var PutSaver = function(wiki) {
|
|
|
|
this.wiki = wiki;
|
2016-02-16 18:42:36 +00:00
|
|
|
var self = this;
|
2017-02-17 12:26:15 +00:00
|
|
|
var uri = this.uri();
|
2016-02-16 18:42:36 +00:00
|
|
|
// Async server probe. Until probe finishes, save will fail fast
|
2024-07-25 16:44:52 +00:00
|
|
|
// See also https://github.com/TiddlyWiki/TiddlyWiki5/issues/2276
|
2017-02-17 12:26:15 +00:00
|
|
|
$tw.utils.httpRequest({
|
|
|
|
url: uri,
|
|
|
|
type: "OPTIONS",
|
2018-06-13 14:50:47 +00:00
|
|
|
callback: function(err,data,xhr) {
|
2017-02-17 12:26:15 +00:00
|
|
|
// Check DAV header http://www.webdav.org/specs/rfc2518.html#rfc.section.9.1
|
2017-02-18 12:12:29 +00:00
|
|
|
if(!err) {
|
2024-08-26 20:50:44 +00:00
|
|
|
self.serverAcceptsPuts = xhr.status >= 200 && xhr.status < 300 && !!xhr.getResponseHeader("dav");
|
2017-02-18 12:12:29 +00:00
|
|
|
}
|
2017-02-17 12:26:15 +00:00
|
|
|
}
|
|
|
|
});
|
2018-06-13 14:50:47 +00:00
|
|
|
retrieveETag(this);
|
2017-02-17 12:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PutSaver.prototype.uri = function() {
|
2017-09-16 14:30:13 +00:00
|
|
|
return document.location.toString().split("#")[0];
|
2016-02-08 21:40:10 +00:00
|
|
|
};
|
|
|
|
|
2017-02-17 12:26:15 +00:00
|
|
|
// TODO: in case of edit conflict
|
|
|
|
// Prompt: Do you want to save over this? Y/N
|
|
|
|
// Merging would be ideal, and may be possible using future generic merge flow
|
2018-06-13 14:50:47 +00:00
|
|
|
PutSaver.prototype.save = function(text,method,callback) {
|
2017-02-18 12:12:29 +00:00
|
|
|
if(!this.serverAcceptsPuts) {
|
2016-02-16 18:42:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-17 12:26:15 +00:00
|
|
|
var self = this;
|
2018-06-13 14:50:47 +00:00
|
|
|
var headers = {
|
|
|
|
"Content-Type": "text/html;charset=UTF-8"
|
|
|
|
};
|
2017-02-18 12:12:29 +00:00
|
|
|
if(this.etag) {
|
2017-02-17 12:26:15 +00:00
|
|
|
headers["If-Match"] = this.etag;
|
|
|
|
}
|
2022-04-05 16:06:56 +00:00
|
|
|
$tw.notifier.display("$:/language/Notifications/Save/Starting");
|
2017-02-17 12:26:15 +00:00
|
|
|
$tw.utils.httpRequest({
|
|
|
|
url: this.uri(),
|
|
|
|
type: "PUT",
|
|
|
|
headers: headers,
|
|
|
|
data: text,
|
2018-06-13 14:50:47 +00:00
|
|
|
callback: function(err,data,xhr) {
|
2017-02-18 12:12:29 +00:00
|
|
|
if(err) {
|
2022-04-05 16:06:56 +00:00
|
|
|
var status = xhr.status,
|
|
|
|
errorMsg = err;
|
2021-04-27 09:14:04 +00:00
|
|
|
if(status === 412) { // file changed on server
|
2022-04-05 16:06:56 +00:00
|
|
|
errorMsg = $tw.language.getString("Error/PutEditConflict");
|
2021-04-27 09:14:04 +00:00
|
|
|
} else if(status === 401) { // authentication required
|
2022-04-05 16:06:56 +00:00
|
|
|
errorMsg = $tw.language.getString("Error/PutUnauthorized");
|
2021-04-27 09:14:04 +00:00
|
|
|
} else if(status === 403) { // permission denied
|
2022-04-05 16:06:56 +00:00
|
|
|
errorMsg = $tw.language.getString("Error/PutForbidden");
|
2018-05-04 10:48:38 +00:00
|
|
|
}
|
2022-04-05 16:06:56 +00:00
|
|
|
if (xhr.responseText) {
|
|
|
|
// treat any server response like a plain text error explanation
|
|
|
|
errorMsg = errorMsg + "\n\n" + xhr.responseText;
|
|
|
|
}
|
|
|
|
callback(errorMsg); // fail
|
2018-05-04 10:48:38 +00:00
|
|
|
} else {
|
2017-02-17 12:26:15 +00:00
|
|
|
self.etag = xhr.getResponseHeader("ETag");
|
2018-06-13 14:50:47 +00:00
|
|
|
if(self.etag == null) {
|
|
|
|
retrieveETag(self);
|
2018-05-04 10:48:38 +00:00
|
|
|
}
|
2017-02-17 12:26:15 +00:00
|
|
|
callback(null); // success
|
|
|
|
}
|
2016-02-08 21:40:10 +00:00
|
|
|
}
|
2017-02-17 12:26:15 +00:00
|
|
|
});
|
2016-02-08 21:40:10 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Information about this saver
|
|
|
|
*/
|
|
|
|
PutSaver.prototype.info = {
|
|
|
|
name: "put",
|
|
|
|
priority: 2000,
|
2018-06-13 14:50:47 +00:00
|
|
|
capabilities: ["save","autosave"]
|
2016-02-08 21:40:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Static method that returns true if this saver is capable of working
|
|
|
|
*/
|
|
|
|
exports.canSave = function(wiki) {
|
|
|
|
return /^https?:/.test(location.protocol);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create an instance of this saver
|
|
|
|
*/
|
|
|
|
exports.create = function(wiki) {
|
|
|
|
return new PutSaver(wiki);
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|