2024-01-19 19:33:58 +00:00
|
|
|
/*\
|
2024-03-11 09:10:01 +00:00
|
|
|
title: $:/plugins/tiddlywiki/multiwikiserver/routes/handlers/get-bag-tiddler.js
|
2024-01-19 19:33:58 +00:00
|
|
|
type: application/javascript
|
2024-03-18 08:44:45 +00:00
|
|
|
module-type: mws-route
|
2024-01-19 19:33:58 +00:00
|
|
|
|
2024-03-20 15:13:50 +00:00
|
|
|
GET /bags/:bag_name/tiddler/:title
|
2024-01-19 19:33:58 +00:00
|
|
|
|
2024-03-20 15:40:38 +00:00
|
|
|
Parameters:
|
|
|
|
|
|
|
|
fallback=<url> // Optional redirect if the tiddler is not found
|
2024-01-19 19:33:58 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.method = "GET";
|
|
|
|
|
2024-03-20 15:13:50 +00:00
|
|
|
exports.path = /^\/bags\/([^\/]+)\/tiddlers\/(.+)$/;
|
2024-01-19 19:33:58 +00:00
|
|
|
|
|
|
|
exports.handler = function(request,response,state) {
|
|
|
|
// Get the parameters
|
2024-03-20 15:40:38 +00:00
|
|
|
const bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
|
2024-03-20 15:13:50 +00:00
|
|
|
title = $tw.utils.decodeURIComponentSafe(state.params[1]),
|
2024-03-20 15:40:38 +00:00
|
|
|
tiddlerInfo = $tw.mws.store.getBagTiddler(title,bag_name);
|
|
|
|
if(tiddlerInfo && tiddlerInfo.tiddler) {
|
2024-01-19 19:33:58 +00:00
|
|
|
// 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"
|
|
|
|
];
|
2024-03-20 15:40:38 +00:00
|
|
|
$tw.utils.each(tiddlerInfo.tiddler,function(value,name) {
|
2024-01-19 19:33:58 +00:00
|
|
|
if(knownFields.indexOf(name) !== -1) {
|
|
|
|
tiddlerFields[name] = value;
|
|
|
|
} else {
|
|
|
|
tiddlerFields.fields = tiddlerFields.fields || {};
|
|
|
|
tiddlerFields.fields[name] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
|
|
|
|
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8");
|
2024-03-10 17:45:33 +00:00
|
|
|
return;
|
2024-01-19 19:33:58 +00:00
|
|
|
} else {
|
|
|
|
// This is not a JSON API request, we should return the raw tiddler content
|
2024-03-10 17:45:33 +00:00
|
|
|
const result = $tw.mws.store.getBagTiddlerStream(title,bag_name);
|
|
|
|
if(result) {
|
|
|
|
response.writeHead(200, "OK",{
|
|
|
|
"Content-Type": result.type
|
|
|
|
});
|
|
|
|
result.stream.pipe(response);
|
|
|
|
return;
|
2024-03-20 15:40:38 +00:00
|
|
|
} else {
|
|
|
|
response.writeHead(404);
|
|
|
|
response.end();
|
|
|
|
return;
|
2024-03-10 17:45:33 +00:00
|
|
|
}
|
2024-01-19 19:33:58 +00:00
|
|
|
}
|
2024-03-20 15:40:38 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2024-01-19 19:33:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}());
|