2012-07-13 12:03:38 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/savers/upload.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: saver
|
|
|
|
|
2012-07-13 15:51:35 +00:00
|
|
|
Handles saving changes via upload to a server.
|
|
|
|
|
|
|
|
Designed to be compatible with BidiX's UploadPlugin at http://tiddlywiki.bidix.info/#UploadPlugin
|
2012-07-13 12:03:38 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(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;
|
|
|
|
};
|
|
|
|
|
2013-11-27 20:51:08 +00:00
|
|
|
UploadSaver.prototype.save = function(text,method,callback) {
|
2012-07-13 12:03:38 +00:00
|
|
|
// Get the various parameters we need
|
2014-01-12 12:05:15 +00:00
|
|
|
var backupDir = this.wiki.getTextReference("$:/UploadBackupDir") || ".",
|
2013-03-17 19:37:31 +00:00
|
|
|
username = this.wiki.getTextReference("$:/UploadName"),
|
2012-07-13 12:03:38 +00:00
|
|
|
password = $tw.utils.getPassword("upload"),
|
2014-01-12 12:05:15 +00:00
|
|
|
uploadDir = this.wiki.getTextReference("$:/UploadDir") || ".",
|
|
|
|
uploadFilename = this.wiki.getTextReference("$:/UploadFilename") || "index.html",
|
2021-01-31 15:32:18 +00:00
|
|
|
uploadWithUrlOnly = this.wiki.getTextReference("$:/UploadWithUrlOnly") || "no",
|
2012-07-13 12:03:38 +00:00
|
|
|
url = this.wiki.getTextReference("$:/UploadURL");
|
2012-07-13 12:53:51 +00:00
|
|
|
// Bail out if we don't have the bits we need
|
2021-01-31 15:32:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-07-13 12:53:51 +00:00
|
|
|
}
|
|
|
|
// Construct the url if not provided
|
2012-07-13 12:03:38 +00:00
|
|
|
if(!url) {
|
2013-03-17 19:37:31 +00:00
|
|
|
url = "http://" + username + ".tiddlyspot.com/store.cgi";
|
2012-07-13 12:03:38 +00:00
|
|
|
}
|
|
|
|
// Assemble the header
|
2021-05-30 18:20:17 +00:00
|
|
|
var boundary = "---------------------------" + "AaB03x";
|
2012-07-13 12:03:38 +00:00
|
|
|
var uploadFormName = "UploadPlugin";
|
|
|
|
var head = [];
|
|
|
|
head.push("--" + boundary + "\r\nContent-disposition: form-data; name=\"UploadPlugin\"\r\n");
|
2013-03-17 19:37:31 +00:00
|
|
|
head.push("backupDir=" + backupDir + ";user=" + username + ";password=" + password + ";uploaddir=" + uploadDir + ";;");
|
2012-07-13 12:03:38 +00:00
|
|
|
head.push("\r\n" + "--" + boundary);
|
2014-01-12 12:05:15 +00:00
|
|
|
head.push("Content-disposition: form-data; name=\"userfile\"; filename=\"" + uploadFilename + "\"");
|
2012-07-13 12:03:38 +00:00
|
|
|
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
|
|
|
|
var http = new XMLHttpRequest();
|
2013-03-17 19:37:31 +00:00
|
|
|
http.open("POST",url,true,username,password);
|
2016-03-03 17:08:24 +00:00
|
|
|
http.setRequestHeader("Content-Type","multipart/form-data; charset=UTF-8; boundary=" + boundary);
|
2012-07-13 12:03:38 +00:00
|
|
|
http.onreadystatechange = function() {
|
|
|
|
if(http.readyState == 4 && http.status == 200) {
|
2014-01-12 12:05:15 +00:00
|
|
|
if(http.responseText.substr(0,4) === "0 - ") {
|
2013-11-26 12:50:40 +00:00
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
callback(http.responseText);
|
|
|
|
}
|
2012-07-13 12:03:38 +00:00
|
|
|
}
|
2012-07-16 11:56:59 +00:00
|
|
|
};
|
2014-09-27 09:47:31 +00:00
|
|
|
try {
|
|
|
|
http.send(data);
|
|
|
|
} catch(ex) {
|
2016-05-17 20:58:47 +00:00
|
|
|
return callback($tw.language.getString("Error/Caption") + ":" + ex);
|
2014-09-27 09:47:31 +00:00
|
|
|
}
|
2014-02-16 09:46:43 +00:00
|
|
|
$tw.notifier.display("$:/language/Notifications/Save/Starting");
|
2012-07-13 12:03:38 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Information about this saver
|
|
|
|
*/
|
|
|
|
UploadSaver.prototype.info = {
|
|
|
|
name: "upload",
|
2014-02-04 21:21:01 +00:00
|
|
|
priority: 2000,
|
|
|
|
capabilities: ["save", "autosave"]
|
2012-07-13 12:03:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Static method that returns true if this saver is capable of working
|
|
|
|
*/
|
|
|
|
exports.canSave = function(wiki) {
|
2012-07-13 12:53:51 +00:00
|
|
|
return true;
|
2012-07-13 12:03:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create an instance of this saver
|
|
|
|
*/
|
|
|
|
exports.create = function(wiki) {
|
|
|
|
return new UploadSaver(wiki);
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|