diff --git a/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag-tiddler.js b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag-tiddler.js index 56a536a45..ab72476b7 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag-tiddler.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-bag-tiddler.js @@ -5,6 +5,9 @@ module-type: mws-route GET /bags/:bag_name/tiddler/:title +Parameters: + +fallback= // Optional redirect if the tiddler is not found \*/ (function() { @@ -19,17 +22,17 @@ exports.path = /^\/bags\/([^\/]+)\/tiddlers\/(.+)$/; exports.handler = function(request,response,state) { // Get the parameters - var bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]), + const bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]), title = $tw.utils.decodeURIComponentSafe(state.params[1]), - result = bag_name && $tw.mws.store.getBagTiddler(title,bag_name); - if(bag_name && result) { + tiddlerInfo = $tw.mws.store.getBagTiddler(title,bag_name); + if(tiddlerInfo && tiddlerInfo.tiddler) { // If application/json is requested then this is an API request, and gets the response in JSON if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) { var tiddlerFields = {}, knownFields = [ "bag", "created", "creator", "modified", "modifier", "permissions", "recipe", "revision", "tags", "text", "title", "type", "uri" ]; - $tw.utils.each(result.tiddler,function(value,name) { + $tw.utils.each(tiddlerInfo.tiddler,function(value,name) { if(knownFields.indexOf(name) !== -1) { tiddlerFields[name] = value; } else { @@ -49,11 +52,26 @@ exports.handler = function(request,response,state) { }); result.stream.pipe(response); return; + } else { + response.writeHead(404); + response.end(); + return; } } + } else { + // Redirect to fallback URL if tiddler not found + if(state.queryParameters.fallback) { + response.writeHead(302, "OK",{ + "Location": state.queryParameters.fallback + }); + response.end(); + return; + } else { + response.writeHead(404); + response.end(); + return; + } } - response.writeHead(404); - response.end(); }; }()); diff --git a/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-recipe-tiddler.js b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-recipe-tiddler.js index b7e1b1333..8ba00e912 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-recipe-tiddler.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-recipe-tiddler.js @@ -5,6 +5,10 @@ module-type: mws-route GET /recipes/:recipe_name/tiddler/:title +Parameters: + +fallback= // Optional redirect if the tiddler is not found + \*/ (function() { @@ -20,8 +24,8 @@ exports.handler = function(request,response,state) { // Get the parameters var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]), title = $tw.utils.decodeURIComponentSafe(state.params[1]), - tiddlerInfo = recipe_name && $tw.mws.store.getRecipeTiddler(title,recipe_name); - if(recipe_name && tiddlerInfo && tiddlerInfo.tiddler) { + tiddlerInfo = $tw.mws.store.getRecipeTiddler(title,recipe_name); + if(tiddlerInfo && tiddlerInfo.tiddler) { // If application/json is requested then this is an API request, and gets the response in JSON if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) { var tiddlerFields = {}, @@ -38,6 +42,7 @@ exports.handler = function(request,response,state) { }); tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki"; state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8"); + return; } else { // This is not a JSON API request, we should return the raw tiddler content var type = tiddlerInfo.tiddler.type || "text/plain"; @@ -46,10 +51,21 @@ exports.handler = function(request,response,state) { }); response.write(tiddlerInfo.tiddler.text || "",($tw.config.contentTypeInfo[type] ||{encoding: "utf8"}).encoding); response.end();; + return; } } else { - response.writeHead(404); - response.end(); + // Redirect to fallback URL if tiddler not found + if(state.queryParameters.fallback) { + response.writeHead(302, "OK",{ + "Location": state.queryParameters.fallback + }); + response.end(); + return; + } else { + response.writeHead(404); + response.end(); + return; + } } }; diff --git a/plugins/tiddlywiki/multiwikiserver/templates/get-bag.tid b/plugins/tiddlywiki/multiwikiserver/templates/get-bag.tid index 94ffe504d..a48650c52 100644 --- a/plugins/tiddlywiki/multiwikiserver/templates/get-bag.tid +++ b/plugins/tiddlywiki/multiwikiserver/templates/get-bag.tid @@ -1,16 +1,10 @@ title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-bag -! <$image - source=`/bags/${ [encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` +! encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png` class="mws-favicon-small" width="32px" -> - <$image - source="$:/plugins/multiwikiserver/images/missing-favicon.png" - class="mws-favicon-small" - width="32px" - /> - Bag <$text text={{{ []}}}/> +/> Bag <$text text={{{ []}}}/>
> class={{{ mws-bag-pill [match[yes]then[mws-bag-pill-topmost]] +[join[ ]] }}}> encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank"> encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` + src=`/bags/${ [encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png` class="mws-favicon-small" /> @@ -29,7 +29,7 @@ title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-index >
encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` + src=`/recipes/${ [encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png` class="mws-favicon" />