1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-24 22:33:16 +00:00

AWS Plugin: Add s3-savetiddlers command

This commit is contained in:
Jermolene 2017-08-16 18:10:52 +01:00
parent 7e88dd788c
commit 2ebf4285e8
3 changed files with 52 additions and 10 deletions

View File

@ -37,6 +37,19 @@ Save a raw tiddler to a file in an S3 bucket.
* ''filename'': filename of the saved file
* ''zipfilename'': optional; the file will be packed into a ZIP file with the specified name
! "s3-savetiddlers" subcommand
Save raw tiddlers matching a filter to an S3 bucket.
```
--aws s3-savetiddlers <filter> <region> <bucket> <prefix>
```
* ''filter'': filter identifying tiddlers to render
* ''region'': AWS region
* ''bucket'': name of the bucket to save the files
* ''prefix'': prefix for rendered file names
! "s3-rendertiddler" subcommand
Save the results of rendering a tiddler to a file in an S3 bucket.

View File

@ -55,9 +55,9 @@ Command.prototype.subCommands["s3-load"] = function() {
}
async.eachLimit(
filepaths,
4,
20,
function(filepath,callback) {
awsUtils.getTextFile(region,bucket,filepath,function(err,data) {
awsUtils.getFile(region,bucket,filepath,function(err,data) {
if(err) {
return callback(err);
}
@ -112,7 +112,7 @@ Command.prototype.subCommands["s3-rendertiddler"] = function() {
}
// Save the file
async.series([
awsUtils.putTextFile.bind(null,region,bucket,filename,text,saveType)
awsUtils.putFile.bind(null,region,bucket,filename,text,saveType)
],
function(err,results){
self.callback(err,results);
@ -138,14 +138,14 @@ Command.prototype.subCommands["s3-rendertiddlers"] = function() {
}
async.eachLimit(
tiddlers,
2,
20,
function(title,callback) {
var parser = wiki.parseTiddler(template),
widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title}}),
container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
var text = type === "text/html" ? container.innerHTML : container.textContent;
awsUtils.putTextFile(region,bucket,prefix + encodeURIComponent(title) + extension,text,saveType,callback);
awsUtils.putFile(region,bucket,prefix + encodeURIComponent(title) + extension,text,saveType,callback);
},
function(err,results) {
self.callback(err,results);
@ -182,7 +182,7 @@ Command.prototype.subCommands["s3-savetiddler"] = function() {
}
// Save the file
async.series([
awsUtils.putTextFile.bind(null,region,bucket,filename,text,type)
awsUtils.putFile.bind(null,region,bucket,filename,text,type)
],
function(err,results){
self.callback(err,results);
@ -190,6 +190,35 @@ Command.prototype.subCommands["s3-savetiddler"] = function() {
return null;
};
// 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],
prefix = this.params[4],
tiddlers = wiki.filterTiddlers(filter);
// Check parameters
if(!filter || !region || !bucket || !prefix) {
throw "Missing parameters";
}
async.eachLimit(
tiddlers,
20,
function(title,callback) {
var tiddler = wiki.getTiddler(title),
text = tiddler.fields.text || "",
type = tiddler.fields.type || "text/vnd.tiddlywiki";
awsUtils.putFile(region,bucket,prefix + encodeURIComponent(title),text,type,callback);
},
function(err,results) {
self.callback(err,results);
}
);
return null;
};
exports.Command = Command;
})();

View File

@ -19,7 +19,7 @@ bucketName:
title:
callback: invoked with (err,{body:,type:}
*/
function getTextFile(region,bucketName,title,callback) {
function getFile(region,bucketName,title,callback) {
console.log("Reading file from S3",bucketName,title)
var AWS = require("aws-sdk"),
s3bucket = new AWS.S3({
@ -45,7 +45,7 @@ console.log("Reading file from S3",bucketName,title)
/*
Put a file to an S3 bucket
*/
function putTextFile(region,bucketName,title,text,type,callback) {
function putFile(region,bucketName,title,text,type,callback) {
console.log("Writing file to S3",bucketName,title,type)
var AWS = require("aws-sdk"),
s3bucket = new AWS.S3({
@ -61,7 +61,7 @@ console.log("Writing file to S3",bucketName,title,type)
s3bucket.upload(params,callback);
}
exports.putTextFile = putTextFile;
exports.getTextFile = getTextFile;
exports.putFile = putFile;
exports.getFile = getFile;
})();