-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy paththost_upload_plugin.js.erb
229 lines (187 loc) · 6.8 KB
/
thost_upload_plugin.js.erb
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/***
|Name |ThostUploadPlugin |
|Description |Support saving to Tiddlyhost.com |
|Version |1.0.1 |
|Date |March 06, 2021 |
|Source |https://github.com/tiddlyhost/tiddlyhost-com/tree/main/rails/tw_content/plugins |
|Author |BidiX, Simon Baird, Yakov Litvin |
|License |BSD open source license |
|~CoreVersion |2.9.2 |
***/
//{{{
version.extensions.ThostUploadPlugin = { major: 1, minor: 0, revision: 1 };
//
// Environment
//
if (!window.bidix) window.bidix = {};
// To change these defaults, create a tiddler named "ThostOptions" with tag
// "systemConfig" and the following content:
// window.bidix = { "editModeAlways": false, "uploadButtonAlways": false };
// Set false if you want the chkHttpReadOnly cookie to decide whether to
// render in read-only mode or edit mode when you're not logged in or when
// the site is being viewed by others. Default true.
if (!("editModeAlways" in bidix)) { bidix.editModeAlways = true; }
// Set false to hide the "upload to tiddlyhost" button when you're not logged
// in or when the site is being viewed by others. Default true.
if (!("uploadButtonAlways" in bidix)) { bidix.uploadButtonAlways = true; }
// For debugging. Default false.
if (!("debugMode" in bidix)) { bidix.debugMode = false; }
//
// Upload Macro
//
config.macros.thostUpload = {
handler: function(place,macroName,params) {
createTiddlyButton(place, "save to tiddlyhost",
"save this TiddlyWiki to a site on Tiddlyhost.com",
this.action, null, null, this.accessKey);
},
action: function(params) {
var siteName = config.options.txtThostSiteName.trim();
if (!siteName) {
alert("Tiddlyhost site name is missing!");
clearMessage();
}
else {
bidix.thostUpload.uploadChanges('<%= site_url %>');
}
return false;
}
};
//
// Upload functions
//
if (!bidix.thostUpload) bidix.thostUpload = {};
if (!bidix.thostUpload.messages) bidix.thostUpload.messages = {
invalidFileError: "The original file '%0' does not appear to be a valid TiddlyWiki",
mainSaved: "Main TiddlyWiki file uploaded",
mainFailed: "Failed to upload main TiddlyWiki file. Your changes have not been saved",
loadOriginalHttpPostError: "Can't get original file",
aboutToSaveOnHttpPost: 'About to upload on %0 ...',
storePhpNotFound: "The store script '%0' was not found."
};
bidix.thostUpload.uploadChanges = function(storeUrl) {
var callback = function(status, uploadParams, original, url, xhr) {
if (!status) {
displayMessage(bidix.thostUpload.messages.loadOriginalHttpPostError);
return;
}
if (bidix.debugMode) {
alert(original.substr(0,500)+"\n...");
}
var posDiv = locateStoreArea(original);
if ((posDiv[0] == -1) || (posDiv[1] == -1)) {
alert(config.messages.invalidFileError.format([localPath]));
return;
}
bidix.thostUpload.uploadMain(uploadParams, original, posDiv);
};
clearMessage();
// get original
var uploadParams = [storeUrl];
var originalPath = document.location.toString();
var dest = 'index.html';
displayMessage(bidix.thostUpload.messages.aboutToSaveOnHttpPost.format([dest]));
if (bidix.debugMode) {
alert("about to execute Http - GET on "+originalPath);
}
var r = doHttp("GET", originalPath, null, null, null, null, callback, uploadParams, null);
if (typeof r == "string") {
displayMessage(r);
}
return r;
};
bidix.thostUpload.uploadMain = function(uploadParams, original, posDiv) {
var callback = function(status, params, responseText, url, xhr) {
if (status) {
displayMessage(bidix.thostUpload.messages.mainSaved);
store.setDirty(false);
}
else {
alert(bidix.thostUpload.messages.mainFailed);
displayMessage(bidix.thostUpload.messages.mainFailed);
}
};
var revised = updateOriginal(original, posDiv);
bidix.thostUpload.httpUpload(uploadParams, revised, callback, uploadParams);
};
bidix.thostUpload.httpUpload = function(uploadParams, data, callback, params) {
var localCallback = function(status, params, responseText, url, xhr) {
if (xhr.status == 404) {
alert(bidix.thostUpload.messages.storePhpNotFound.format([url]));
}
var saveNotOk = responseText.charAt(0) != '0';
if (bidix.debugMode || saveNotOk) {
alert(responseText);
}
if (saveNotOk) {
status = null;
}
callback(status, params, responseText, url, xhr);
};
// do httpUpload
var boundary = "---------------------------"+"AaB03x";
var uploadFormName = "UploadPlugin";
// compose headers data
var sheader = "";
sheader += "--" + boundary + "\r\nContent-disposition: form-data; name=\"";
sheader += uploadFormName +"\"\r\n\r\n";
sheader += "backupDir=x" +
";user=x" +
";password=x" +
";uploaddir=x";
if (bidix.debugMode) {
sheader += ";debug=1";
}
sheader += ";;\r\n";
sheader += "\r\n" + "--" + boundary + "\r\n";
sheader += "Content-disposition: form-data; name=\"userfile\"; filename=\"index.html\"\r\n";
sheader += "Content-Type: text/html;charset=UTF-8" + "\r\n";
sheader += "Content-Length: " + data.length + "\r\n\r\n";
// compose trailer data
var strailer = "";
strailer = "\r\n--" + boundary + "--\r\n";
data = sheader + data + strailer;
if (bidix.debugMode) {
alert("about to execute Http - POST on " + uploadParams[0]+ "\n with \n" + data.substr(0,500) + " ... ");
}
var r = doHttp("POST", uploadParams[0], data,
"multipart/form-data; ;charset=UTF-8; boundary=" + boundary, 'x','x', localCallback, params, null);
if (typeof r == "string") {
displayMessage(r);
}
return r;
};
// a fix for versions before 2.9.2 (updateOriginal used conversions irrelevant for Tiddlyhost)
convertUnicodeToFileFormat = function(s) { return s };
//
// Site config
//
bidix.initOption = function(name,value) {
if (!config.options[name]) {
config.options[name] = value;
}
};
merge(config.optionsDesc, {
txtThostSiteName: "Site name for uploads to Tiddlyhost.com",
});
bidix.initOption('txtThostSiteName','<%= site_name %>');
//
// Tiddlyhost stuff
//
bidix.ownerLoggedIn = (config.shadowTiddlers.TiddlyHostIsLoggedIn &&
config.shadowTiddlers.TiddlyHostIsLoggedIn == "yes")
if (bidix.editModeAlways || bidix.ownerLoggedIn) {
// If user is logged in to Tiddlyhost and viewing their own site then
// we disregard the original value of the chkHttpReadOnly cookie
config.options.chkHttpReadOnly = false
// window.readOnly gets set before plugins are loaded, so we need to
// set it here to make sure TW is editable, unlike window.showBackstage
// which is set after
window.readOnly = false
}
if (bidix.uploadButtonAlways || bidix.ownerLoggedIn) {
// Add the 'save to tiddlyhost' button after the regular save button
config.shadowTiddlers.SideBarOptions = config.shadowTiddlers.SideBarOptions
.replace(/(<<saveChanges>>)/,"$1<<thostUpload>>");
}
//}}}