Get rid of those annoying missing image icons

By adding a `?fallback=url` parameter to tiddler requests
This commit is contained in:
Jeremy Ruston 2024-03-20 15:40:38 +00:00
parent 9b3ca525ee
commit ae8ef305fa
4 changed files with 49 additions and 21 deletions

View File

@ -5,6 +5,9 @@ module-type: mws-route
GET /bags/:bag_name/tiddler/:title
Parameters:
fallback=<url> // 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();
};
}());

View File

@ -5,6 +5,10 @@ module-type: mws-route
GET /recipes/:recipe_name/tiddler/:title
Parameters:
fallback=<url> // 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;
}
}
};

View File

@ -1,16 +1,10 @@
title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-bag
! <$image
source=`/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico`
! <img
src=`/bags/${ [<bag-name>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"
/>
</$image> Bag <$text text={{{ [<bag-name>]}}}/>
/> Bag <$text text={{{ [<bag-name>]}}}/>
<form
method="post"

View File

@ -5,7 +5,7 @@ title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-index
<$genesis $type=<<element-tag>> class={{{ mws-bag-pill [<is-topmost>match[yes]then[mws-bag-pill-topmost]] +[join[ ]] }}}>
<a class="mws-bag-pill-link" href=`/bags/${ [<bag-name>encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank">
<img
src=`/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico`
src=`/bags/${ [<bag-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png`
class="mws-favicon-small"
/>
<span class="mws-bag-pill-label">
@ -29,7 +29,7 @@ title: $:/plugins/tiddlywiki/multiwikiserver/templates/get-index
>
<div class="mws-wiki-card-image">
<img
src=`/recipes/${ [<recipe-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico`
src=`/recipes/${ [<recipe-name>encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico?fallback=/.system/missing-favicon.png`
class="mws-favicon"
/>
</div>