1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-07 22:33:50 +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 * ''filename'': filename of the saved file
* ''zipfilename'': optional; the file will be packed into a ZIP file with the specified name * ''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 ! "s3-rendertiddler" subcommand
Save the results of rendering a tiddler to a file in an S3 bucket. 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( async.eachLimit(
filepaths, filepaths,
4, 20,
function(filepath,callback) { function(filepath,callback) {
awsUtils.getTextFile(region,bucket,filepath,function(err,data) { awsUtils.getFile(region,bucket,filepath,function(err,data) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
@ -112,7 +112,7 @@ Command.prototype.subCommands["s3-rendertiddler"] = function() {
} }
// Save the file // Save the file
async.series([ async.series([
awsUtils.putTextFile.bind(null,region,bucket,filename,text,saveType) awsUtils.putFile.bind(null,region,bucket,filename,text,saveType)
], ],
function(err,results){ function(err,results){
self.callback(err,results); self.callback(err,results);
@ -138,14 +138,14 @@ Command.prototype.subCommands["s3-rendertiddlers"] = function() {
} }
async.eachLimit( async.eachLimit(
tiddlers, tiddlers,
2, 20,
function(title,callback) { function(title,callback) {
var parser = wiki.parseTiddler(template), var parser = wiki.parseTiddler(template),
widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title}}), widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title}}),
container = $tw.fakeDocument.createElement("div"); container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null); widgetNode.render(container,null);
var text = type === "text/html" ? container.innerHTML : container.textContent; 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) { function(err,results) {
self.callback(err,results); self.callback(err,results);
@ -182,7 +182,7 @@ Command.prototype.subCommands["s3-savetiddler"] = function() {
} }
// Save the file // Save the file
async.series([ async.series([
awsUtils.putTextFile.bind(null,region,bucket,filename,text,type) awsUtils.putFile.bind(null,region,bucket,filename,text,type)
], ],
function(err,results){ function(err,results){
self.callback(err,results); self.callback(err,results);
@ -190,6 +190,35 @@ Command.prototype.subCommands["s3-savetiddler"] = function() {
return null; 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; exports.Command = Command;
})(); })();

View File

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