1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-14 05:44:50 +00:00
TiddlyWiki5/core/modules/savers/upload.js
Simon Baird 39e4e69ae7
Show server response as error message in put saver (#6589)
* Show server response as error message in put saver

I'd like to use this on Tiddlyhost so users can get more informative
error messages if the put save fails for whatever reason.

This would make the put saver a viable replacement for the legacy
upload saver, which is what Tiddlyhost uses currently.

I'm not sure what standard WebDAV servers do, but I would guess they
don't provide any response body for put requests, and hence this
patch would have no impact for a standard WebDAV server. (That said,
it would be a good idea to test it to make sure there aren't any
unexpected regressions for WebDAV or other put saver compatible
services.)

* Access http response status directly in put saver

There's no need to extract it from the error string created inside
tw.utils.httpRequest if we can get it directly from the xhr object.

* Add 'Save starting' notification for put saver

There are two related changes here:

1. Add a 'Save starting' notification for the put saver, similar to
   the upload saver. Not sure if it was intentionally omitted for
   the put saver, but it seems reasonable to have the two be
   consistent.

2. Send the 'Save starting' notifications in both upload and put
   save right before the actual request is sent. While testing I
   noticed that the save might have failed before the "Save
   starting" notification appeared which doesn't seem useful.
2022-04-05 17:06:56 +01:00

112 lines
3.3 KiB
JavaScript

/*\
title: $:/core/modules/savers/upload.js
type: application/javascript
module-type: saver
Handles saving changes via upload to a server.
Designed to be compatible with BidiX's UploadPlugin at http://tiddlywiki.bidix.info/#UploadPlugin
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Select the appropriate saver module and set it up
*/
var UploadSaver = function(wiki) {
this.wiki = wiki;
};
UploadSaver.prototype.save = function(text,method,callback) {
// Get the various parameters we need
var backupDir = this.wiki.getTextReference("$:/UploadBackupDir") || ".",
username = this.wiki.getTextReference("$:/UploadName"),
password = $tw.utils.getPassword("upload"),
uploadDir = this.wiki.getTextReference("$:/UploadDir") || ".",
uploadFilename = this.wiki.getTextReference("$:/UploadFilename") || "index.html",
uploadWithUrlOnly = this.wiki.getTextReference("$:/UploadWithUrlOnly") || "no",
url = this.wiki.getTextReference("$:/UploadURL");
// Bail out if we don't have the bits we need
if (uploadWithUrlOnly === "yes") {
// The url is good enough. No need for a username and password.
// Assume the server uses some other kind of auth mechanism.
if(!url || url.toString().trim() === "") {
return false;
}
}
else {
// Require username and password to be present.
// Assume the server uses the standard UploadPlugin username/password.
if(!username || username.toString().trim() === "" || !password || password.toString().trim() === "") {
return false;
}
}
// Construct the url if not provided
if(!url) {
url = "http://" + username + ".tiddlyspot.com/store.cgi";
}
// Assemble the header
var boundary = "---------------------------" + "AaB03x";
var uploadFormName = "UploadPlugin";
var head = [];
head.push("--" + boundary + "\r\nContent-disposition: form-data; name=\"UploadPlugin\"\r\n");
head.push("backupDir=" + backupDir + ";user=" + username + ";password=" + password + ";uploaddir=" + uploadDir + ";;");
head.push("\r\n" + "--" + boundary);
head.push("Content-disposition: form-data; name=\"userfile\"; filename=\"" + uploadFilename + "\"");
head.push("Content-Type: text/html;charset=UTF-8");
head.push("Content-Length: " + text.length + "\r\n");
head.push("");
// Assemble the tail and the data itself
var tail = "\r\n--" + boundary + "--\r\n",
data = head.join("\r\n") + text + tail;
// Do the HTTP post
$tw.notifier.display("$:/language/Notifications/Save/Starting");
var http = new XMLHttpRequest();
http.open("POST",url,true,username,password);
http.setRequestHeader("Content-Type","multipart/form-data; charset=UTF-8; boundary=" + boundary);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
if(http.responseText.substr(0,4) === "0 - ") {
callback(null);
} else {
callback(http.responseText);
}
}
};
try {
http.send(data);
} catch(ex) {
return callback($tw.language.getString("Error/Caption") + ":" + ex);
}
return true;
};
/*
Information about this saver
*/
UploadSaver.prototype.info = {
name: "upload",
priority: 2000,
capabilities: ["save", "autosave"]
};
/*
Static method that returns true if this saver is capable of working
*/
exports.canSave = function(wiki) {
return true;
};
/*
Create an instance of this saver
*/
exports.create = function(wiki) {
return new UploadSaver(wiki);
};
})();