1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-12 03:00:28 +00:00
TiddlyWiki5/plugins/tiddlywiki/multiwikiserver/modules/routes/handlers/get-system.js
Jeremy Ruston 6063256439 Create new static index route with ability to create/update bags and recipes
Also introduces a new /.system/filename route for stylesheets, scripts etc.
2024-03-20 09:44:52 +00:00

53 lines
1.7 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/multiwikiserver/routes/handlers/get-system.js
type: application/javascript
module-type: mws-route
Retrieves a system file. System files are stored in configuration tiddlers with the following fields:
* title: "$:/plugins/tiddlywiki/multiwikiserver/system-files/" suffixed with the name of the file
* tags: tagged $:/tags/MWS/SystemFile or $:/tags/MWS/SystemFileWikified
* system-file-type: optionally specify the MIME type that should be returned for the file
GET /.system/:filename
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.method = "GET";
exports.path = /^\/\.system\/(.+)$/;
const SYSTEM_FILE_TITLE_PREFIX = "$:/plugins/tiddlywiki/multiwikiserver/system-files/";
exports.handler = function(request,response,state) {
// Get the parameters
const filename = $tw.utils.decodeURIComponentSafe(state.params[0]),
title = SYSTEM_FILE_TITLE_PREFIX + filename,
tiddler = $tw.wiki.getTiddler(title),
isSystemFile = tiddler && tiddler.hasTag("$:/tags/MWS/SystemFile"),
isSystemFileWikified = tiddler && tiddler.hasTag("$:/tags/MWS/SystemFileWikified");
if(tiddler && (isSystemFile || isSystemFileWikified)) {
let text = tiddler.fields.text || "";
const type = tiddler.fields["system-file-type"] || tiddler.fields.type || "text/plain",
encoding = ($tw.config.contentTypeInfo[type] ||{encoding: "utf8"}).encoding;
if(isSystemFileWikified) {
text = $tw.wiki.renderTiddler("text/plain",title);
}
response.writeHead(200, "OK",{
"Content-Type": type
});
response.write(text,encoding);
response.end();
} else {
response.writeHead(404);
response.end();
}
};
}());