/*** |''Name:''|CouchToiLaPlugin| |''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> 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())); }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// version.extensions.CouchToiLaPlugin = { 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' }; // // Upload Macro // config.macros.couchtoila = { // default values defaultCouchServer: 'http://127.0.0.1:5984', defaultCouchDb: "twcouch", defaultPageName: "MyTw", defaultAttachName: "index.html", defaultUserCode: "", defaultUserPass :"" // UploadService Authenticate User }; config.macros.couchtoila.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.couchtoila.messages = { noStoreUrl: "No store URL in parmeters or options", usernameOrPasswordMissing: "Username or password missing" }; //display button config.macros.couchtoila.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.couchtoila.action(params);}, null, null, this.accessKey); }; // // main working process config.macros.couchtoila.action = function(params) { var url = this.defaultCouchServer+'/'+this.defaultCouchDb+'/'+this.defaultPageName; var href = url + this.defaultAttachName 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 = this.defaultPageName; var attach = this.defaultAttachName ; var dt64 = encodeBase64(str); var xtime = new Date(); //check if record exist ajaxReq({ type: "Get", url: url, dataType: "json", success: function(data, status, xhr) { // if record exist generate json with _rev of existing record dt = '{"_rev":"'+data._rev+'","Title":"'+doctitle+'","Type":"TWCouch","Create_at":"'+data.Create_at+'","Modified_at":"'+xtime+'","_attachments":{"'+attach+'":{"content_type":"text\/html","data":"'+dt64+'"}}}'; ajaxReq({ type: "PUT", url: url, dataType: "json", data: dt, success: function(data, status, xhr) { displayMessage("Record Refreshed on CouchDb"); }, error: function(xhr, error, exc) { if (xhr.status==409) { displayMessage("Error: Cannot create record"); } } }); }, error: function(xhr, error, exc) { if (xhr.status==404) { //if record not existing generate json without _rev dt = '{"Title":"'+doctitle+'","Type":"TWCouch","Create_at":"'+xtime+'","Modified_at":"'+xtime+'","_attachments":{"'+attach+'":{"content_type":"text\/html","data":"'+dt64+'"}}}'; ajaxReq({ type: "PUT", url: url, dataType: "json", data: dt, success: function(data, status, xhr) { displayMessage("New Record created on CouchDb"); }, error: function(xhr, error, exc) { if (xhr.status==409) { displayMessage("Error: Cannot create record"); } } }); } } }); }; //}}}