-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouchtwalaPlugin.txt
241 lines (221 loc) · 7.71 KB
/
couchtwalaPlugin.txt
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
230
231
232
233
234
235
236
237
238
239
240
241
/***
|''Name:''|CouchTwaLaPlugin|
|''Description:''|Save to CouchDb server|
|''Why this name:''|Just a joke, but only French speaking people can understand it|
|''Version:''|0.0.1|
|''Date:''|2010-06-01|
|''Based on:''|http://tiddlywiki.bidix.info/#UploadPlugin|
|''Documentation:''|http://blog.unclephil.net|
|''Author:''|Unclephil|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.5.0|
***/
//{{{
//slurped from http://www.tiddlytools.com/#AttachFilePlugin
function encodeBase64(d) {
if (!d) return null;
// encode as base64
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var out="";
var chr1,chr2,chr3="";
var enc1,enc2,enc3,enc4="";
for (var count=0,i=0; i<d.length; ) {
chr1=d.charCodeAt(i++);
chr2=d.charCodeAt(i++);
chr3=d.charCodeAt(i++);
enc1=chr1 >> 2;
enc2=((chr1 & 3) << 4) | (chr2 >> 4);
enc3=((chr2 & 15) << 2) | (chr3 >> 6);
enc4=chr3 & 63;
if (isNaN(chr2)) enc3=enc4=64;
else if (isNaN(chr3)) enc4=64;
out+=keyStr.charAt(enc1)+keyStr.charAt(enc2)+keyStr.charAt(enc3)+keyStr.charAt(enc4);
chr1=chr2=chr3=enc1=enc2=enc3=enc4="";
}
return out;
};
// read local BINARY file data
//slurped from http://www.tiddlytools.com/#AttachFilePlugin
function readfile(filePath) {
if(!window.Components) { return null; }
try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); }
catch(e) { alert("access denied: "+filePath); return null; }
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
try { file.initWithPath(filePath); } catch(e) { alert("cannot read file - invalid path: "+filePath); return null; }
if (!file.exists()) { alert("cannot read file - not found: "+filePath); return null; }
var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
inputStream.init(file, 0x01, 00004, null);
var bInputStream = Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream);
bInputStream.setInputStream(inputStream);
return(bInputStream.readBytes(inputStream.available()));
};
//couch db function
function couchExist(xurl){
var rt=null;
ajaxReq({
type: "Get",
url: xurl,
async: false,
dataType: "json",
success: function(data, status, xhr) { rt=data;},
error: function() {rt=null;}
})
return rt
};
function couchSave(xurl, xdata){
var rt=false;
ajaxReq({
type: "PUT",
async: false,
url: xurl,
dataType: "json",
data: xdata,
success: function() {
rt=true;
},
error: function() {
rt=false;
}
});
return rt
};
// this is a non standard request type, but allowed in couchdb
function couchCopy(xurl, xdest){
var rt=false;
ajaxReq({
type: "COPY",
beforeSend: function(xhr) {xhr.setRequestHeader('Destination', xdest);},
async: false,
url: xurl,
dataType: "json",
success: function() {
rt=true;
},
error: function() {
rt=false;
}
});
return rt
};
function couchDelete(xurl){
var rt=false;
ajaxReq({
type: "DELETE",
async: false,
url: xurl,
dataType: "json",
success: function() {
rt=true;
},
error: function() {
rt=false;
}
});
return rt
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
version.extensions.CouchTwaLaPlugin = {
major: 0, minor: 0, revision: 1,
date: '2010/06/01',
source: 'http://blog.unclephil.net/',
author: 'UnclePhil (unclephil (at) unclephil (dot) net',
coreVersion: '2.5.0'
};
// Configuration
if (config.options.txtCouchTwaLaServer==undefined)
config.options.txtCouchTwaLaServer='http://127.0.0.1:5984';
if (config.options.txtCouchTwaLaDbName==undefined)
config.options.txtCouchTwaLaDbName='couchtwala';
if (config.options.txtCouchTwaLaPageName==undefined)
config.options.txtCouchTwaLaPageName='MyTw';
if (config.options.txtCouchTwaLaAttachName==undefined)
config.options.txtCouchTwaLaAttachName='index.html';
if (config.options.chkCouchTwaLaCreateBackup==undefined)
config.options.chkCouchTwaLaCreateBackup=false;
if (config.options.txtCouchTwaLaBackupPattern==undefined)
config.options.txtCouchTwaLaBackupPattern='YYYY0MM0DD-0hh0mm0ss';
//
// Upload Macro
//
config.macros.CouchTwaLa = {};
config.macros.CouchTwaLa.label = {
promptOption: "Save to CouchDb",
promptParamMacro: "Save and Upload this TiddlyWiki in %0",
saveLabel: "save to couchdb",
saveToDisk: "save to disk",
uploadLabel: "upload to couchDb"
};
config.macros.CouchTwaLa.messages = {
noStoreUrl: "No store URL in parmeters or options",
usernameOrPasswordMissing: "Username or password missing"
};
//display button
config.macros.CouchTwaLa.handler = function(place,macroName,params) {
if (readOnly)
return;
var label;
if (document.location.toString().substr(0,4) == "http")
label = this.label.saveLabel;
else
label = this.label.uploadLabel;
var prompt;
if (params[0]) {
prompt = this.label.promptParamMacro.toString().format([this.destFile(params[0],
(params[1] ? params[1]:bidix.basename(window.location.toString())), params[3])]);
} else {
prompt = this.label.promptOption;
}
createTiddlyButton(place, label, prompt, function() {config.macros.CouchTwaLa.action(params);}, null, null, this.accessKey);
};
//
// main working process
config.macros.CouchTwaLa.action = function(params)
{
var url = config.options.txtCouchTwaLaServer+'/'+config.options.txtCouchTwaLaDbName+'/'+config.options.txtCouchTwaLaPageName;
var href = url + config.options.txtCouchTwaLaAttachName
var doctitle = document.title;
clearMessage();
// save change before upload of the file
saveChanges();
// get file content
originalPath = document.location.toString();
fname = getLocalPath(originalPath);
str= readfile(fname);
//encode file content to base64
var page = config.options.txtCouchTwaLaPageName;
var attach = config.options.txtCouchTwaLaAttachName ;
var dt64 = encodeBase64(str);
var xtime = new Date();
var dt ={};
//check if record exist
var rtdata = couchExist(url);
if (rtdata==null) {
dt = '{"Title":"'+doctitle+'","Type":"TWCouch","Create_at":"'+xtime.convertToYYYYMMDDHHMM()+'","Modified_at":"'+xtime.convertToYYYYMMDDHHMM()+'","_attachments":{"'+attach+'":{"content_type":"text\/html","data":"'+dt64+'"}}}';
}
else {
//save backup
if (config.options.chkCouchTwaLaCreateBackup==true){
bkp = config.options.txtCouchTwaLaPageName+"-"+xtime.formatString(config.options.txtCouchTwaLaBackupPattern);
//bkp = this.config.options.txtCouchTwaLaPageName+"-"+xtime.formatString(config.options.txtCouchTwaLaBackupPattern);
if (couchCopy(url, bkp)) {
displayMessage("Backup saved to couchDb");
}
else {
displayMessage("ERROR:Backup NOT saved to couchDb");
}
}
dt = '{"_rev":"'+rtdata._rev+'","Title":"'+doctitle+'","Type":"TWCouch","Create_at":"'+rtdata.Create_at+'","Modified_at":"'+xtime.convertToYYYYMMDDHHMM()+'","_attachments":{"'+attach+'":{"content_type":"text\/html","data":"'+dt64+'"}}}';
}
if (couchSave(url, dt)) {
displayMessage("Record saved to couchDb");
}
else {
displayMessage("ERROR:Record NOT saved to couchDb");
}
};
// Backstage
merge(config.tasks,{
uploadOptions: {text: "couchIt", tooltip: "Save this file To couchDb", content: '<<CouchTwaLa>>'}
});
config.backstageTasks.push("uploadOptions");
//}}}