2017-07-03 19:34:58 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/aws/command.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: command
|
|
|
|
|
|
|
|
--aws command
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var async,
|
|
|
|
awsUtils;
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: "aws",
|
|
|
|
synchronous: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var Command = function(params,commander,callback) {
|
|
|
|
async = require("$:/plugins/tiddlywiki/async/async.js");
|
|
|
|
awsUtils = require("$:/plugins/tiddlywiki/aws/utils.js");
|
|
|
|
this.params = params;
|
|
|
|
this.commander = commander;
|
|
|
|
this.callback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
Command.prototype.execute = function() {
|
|
|
|
var self = this,
|
|
|
|
wiki = this.commander.wiki,
|
|
|
|
subCommand = this.params[0],
|
|
|
|
fn = this.subCommands[subCommand];
|
|
|
|
if(!fn) {
|
|
|
|
return this.callback("AWS: Unknown subcommand")
|
|
|
|
}
|
|
|
|
fn.bind(this)();
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
Command.prototype.subCommands = {};
|
|
|
|
|
2019-03-22 09:20:25 +00:00
|
|
|
// Set credentials profile
|
|
|
|
Command.prototype.subCommands["profile"] = function() {
|
|
|
|
var AWS = require("aws-sdk"),
|
|
|
|
profile = this.params[1],
|
|
|
|
credentials = new AWS.SharedIniFileCredentials({profile: profile});
|
|
|
|
AWS.config.update({credentials: credentials});
|
|
|
|
this.callback(null);
|
|
|
|
};
|
|
|
|
|
2017-08-15 14:48:12 +00:00
|
|
|
// Load tiddlers from files in an S3 bucket
|
|
|
|
Command.prototype.subCommands["s3-load"] = function() {
|
|
|
|
var self = this,
|
|
|
|
wiki = this.commander.wiki,
|
|
|
|
region = this.params[1],
|
|
|
|
bucket = this.params[2],
|
|
|
|
filepaths = this.params.slice(3);
|
|
|
|
// Check parameters
|
2019-03-22 17:27:20 +00:00
|
|
|
if(!region || !bucket) {
|
|
|
|
self.callback("Missing parameters");
|
2017-08-15 14:48:12 +00:00
|
|
|
}
|
|
|
|
async.eachLimit(
|
|
|
|
filepaths,
|
2017-08-16 17:10:52 +00:00
|
|
|
20,
|
2017-08-15 14:48:12 +00:00
|
|
|
function(filepath,callback) {
|
2017-08-16 17:10:52 +00:00
|
|
|
awsUtils.getFile(region,bucket,filepath,function(err,data) {
|
2017-08-15 14:48:12 +00:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
var tiddlers = self.commander.wiki.deserializeTiddlers(data.type,data.body,{});
|
|
|
|
$tw.utils.each(tiddlers,function(tiddler) {
|
|
|
|
self.commander.wiki.importTiddler(new $tw.Tiddler(tiddler));
|
|
|
|
});
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(err,results) {
|
|
|
|
self.callback(err,results);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2017-07-03 19:34:58 +00:00
|
|
|
// Render a tiddler to an S3 bucket
|
|
|
|
Command.prototype.subCommands["s3-rendertiddler"] = function() {
|
|
|
|
var self = this,
|
|
|
|
wiki = this.commander.wiki,
|
|
|
|
title = this.params[1],
|
|
|
|
region = this.params[2],
|
|
|
|
bucket = this.params[3],
|
|
|
|
filename = this.params[4],
|
|
|
|
type = this.params[5] || "text/html",
|
|
|
|
template = this.params[6],
|
|
|
|
zipfilename = this.params[7],
|
|
|
|
saveType = this.params[8] || type,
|
|
|
|
variables = {};
|
2017-08-15 14:48:12 +00:00
|
|
|
// Check parameters
|
|
|
|
if(!title || !region || !bucket || !filename) {
|
|
|
|
throw "Missing parameters";
|
|
|
|
}
|
2017-07-03 19:34:58 +00:00
|
|
|
// Process the template if present
|
|
|
|
if(template) {
|
|
|
|
variables.currentTiddler = title;
|
|
|
|
title = template;
|
|
|
|
}
|
|
|
|
// Render the tiddler
|
|
|
|
var text = this.commander.wiki.renderTiddler(type,title,{variables: variables}),
|
|
|
|
type = "text/plain",
|
|
|
|
encoding = ($tw.config.contentTypeInfo[type] || {encoding: "utf8"}).encoding;
|
|
|
|
// Zip it if needed
|
|
|
|
if(zipfilename) {
|
|
|
|
var JSZip = require("$:/plugins/tiddlywiki/jszip/jszip.js"),
|
|
|
|
zip = new JSZip();
|
|
|
|
zip.file(filename,new Buffer(text,encoding));
|
|
|
|
text = zip.generate({type: "base64"});
|
|
|
|
type = "application/zip";
|
|
|
|
filename = zipfilename;
|
|
|
|
}
|
|
|
|
// Save the file
|
|
|
|
async.series([
|
2017-08-16 17:10:52 +00:00
|
|
|
awsUtils.putFile.bind(null,region,bucket,filename,text,saveType)
|
2017-07-03 19:34:58 +00:00
|
|
|
],
|
|
|
|
function(err,results){
|
|
|
|
self.callback(err,results);
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2017-08-15 14:48:12 +00:00
|
|
|
Command.prototype.subCommands["s3-rendertiddlers"] = function() {
|
|
|
|
var self = this,
|
|
|
|
wiki = this.commander.wiki,
|
|
|
|
filter = this.params[1],
|
|
|
|
template = this.params[2],
|
|
|
|
region = this.params[3],
|
|
|
|
bucket = this.params[4],
|
2017-08-28 09:31:27 +00:00
|
|
|
filenameFilter = this.params[5],
|
2017-08-15 14:48:12 +00:00
|
|
|
type = this.params[6] || "text/html",
|
2017-09-12 15:04:35 +00:00
|
|
|
saveTypeFilter = this.params[7] || "[[" + type + "]]",
|
2017-08-15 14:48:12 +00:00
|
|
|
tiddlers = wiki.filterTiddlers(filter);
|
|
|
|
// Check parameters
|
2017-09-15 13:15:56 +00:00
|
|
|
if(!filter || !region || !bucket || !filenameFilter) {
|
2017-08-15 14:48:12 +00:00
|
|
|
throw "Missing parameters";
|
|
|
|
}
|
|
|
|
async.eachLimit(
|
|
|
|
tiddlers,
|
2017-08-16 17:10:52 +00:00
|
|
|
20,
|
2017-08-15 14:48:12 +00:00
|
|
|
function(title,callback) {
|
2017-09-12 15:04:35 +00:00
|
|
|
var parser = wiki.parseTiddler(template || title),
|
2017-08-15 14:48:12 +00:00
|
|
|
widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title}}),
|
|
|
|
container = $tw.fakeDocument.createElement("div");
|
|
|
|
widgetNode.render(container,null);
|
2017-08-28 09:31:27 +00:00
|
|
|
var text = type === "text/html" ? container.innerHTML : container.textContent,
|
2017-09-12 15:04:35 +00:00
|
|
|
filename = wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0],
|
|
|
|
saveType = wiki.filterTiddlers(saveTypeFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0];
|
2017-08-28 09:31:27 +00:00
|
|
|
awsUtils.putFile(region,bucket,filename,text,saveType,callback);
|
2017-08-15 14:48:12 +00:00
|
|
|
},
|
|
|
|
function(err,results) {
|
|
|
|
self.callback(err,results);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2017-07-03 19:34:58 +00:00
|
|
|
// Save a tiddler to an S3 bucket
|
|
|
|
Command.prototype.subCommands["s3-savetiddler"] = function() {
|
|
|
|
var self = this,
|
|
|
|
wiki = this.commander.wiki,
|
|
|
|
title = this.params[1],
|
|
|
|
region = this.params[2],
|
|
|
|
bucket = this.params[3],
|
|
|
|
filename = this.params[4],
|
|
|
|
zipfilename = this.params[5],
|
2017-08-23 09:31:28 +00:00
|
|
|
saveType = this.params[6],
|
2017-07-03 19:34:58 +00:00
|
|
|
tiddler = wiki.getTiddler(title),
|
|
|
|
text = tiddler.fields.text,
|
|
|
|
type = tiddler.fields.type,
|
|
|
|
encoding = ($tw.config.contentTypeInfo[type] || {encoding: "utf8"}).encoding;
|
2017-08-15 14:48:12 +00:00
|
|
|
// Check parameters
|
|
|
|
if(!title || !region || !bucket || !filename) {
|
|
|
|
throw "Missing parameters";
|
|
|
|
}
|
2017-07-03 19:34:58 +00:00
|
|
|
// Zip it if needed
|
|
|
|
if(zipfilename) {
|
|
|
|
var JSZip = require("$:/plugins/tiddlywiki/jszip/jszip.js"),
|
|
|
|
zip = new JSZip();
|
|
|
|
zip.file(filename,new Buffer(text,encoding));
|
|
|
|
text = zip.generate({type: "base64"});
|
|
|
|
type = "application/zip";
|
|
|
|
filename = zipfilename;
|
|
|
|
}
|
|
|
|
// Save the file
|
|
|
|
async.series([
|
2017-08-23 09:31:28 +00:00
|
|
|
awsUtils.putFile.bind(null,region,bucket,filename,text,saveType || type)
|
2017-07-03 19:34:58 +00:00
|
|
|
],
|
|
|
|
function(err,results){
|
|
|
|
self.callback(err,results);
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2017-08-16 17:10:52 +00:00
|
|
|
// Save a tiddler to an S3 bucket
|
|
|
|
Command.prototype.subCommands["s3-savetiddlers"] = function() {
|
|
|
|
var self = this,
|
|
|
|
wiki = this.commander.wiki,
|
|
|
|
filter = this.params[1],
|
|
|
|
region = this.params[2],
|
|
|
|
bucket = this.params[3],
|
2017-08-28 09:31:27 +00:00
|
|
|
filenameFilter = this.params[4],
|
2017-09-12 15:04:35 +00:00
|
|
|
saveTypeFilter = this.params[5] || "[is[tiddler]get[type]]",
|
2017-08-16 17:10:52 +00:00
|
|
|
tiddlers = wiki.filterTiddlers(filter);
|
|
|
|
// Check parameters
|
2017-08-28 09:31:27 +00:00
|
|
|
if(!filter || !region || !bucket || !filenameFilter) {
|
2017-08-16 17:10:52 +00:00
|
|
|
throw "Missing parameters";
|
|
|
|
}
|
|
|
|
async.eachLimit(
|
|
|
|
tiddlers,
|
|
|
|
20,
|
|
|
|
function(title,callback) {
|
2017-09-15 13:15:56 +00:00
|
|
|
var tiddler = wiki.getTiddler(title);
|
|
|
|
if(tiddler) {
|
|
|
|
var text = tiddler.fields.text || "",
|
|
|
|
type = tiddler.fields.type || "text/vnd.tiddlywiki",
|
|
|
|
filename = wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0],
|
|
|
|
saveType = wiki.filterTiddlers(saveTypeFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0];
|
|
|
|
awsUtils.putFile(region,bucket,filename,text,saveType || type,callback);
|
|
|
|
} else {
|
|
|
|
process.nextTick(callback,null);
|
|
|
|
}
|
2017-08-16 17:10:52 +00:00
|
|
|
},
|
|
|
|
function(err,results) {
|
|
|
|
self.callback(err,results);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2017-07-03 19:34:58 +00:00
|
|
|
exports.Command = Command;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|