mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-09-12 07:46:06 +00:00
Move support for attachments from a plugin into the core
This commit is contained in:
50
core/modules/serverroute/get-file.js
Normal file
50
core/modules/serverroute/get-file.js
Normal file
@@ -0,0 +1,50 @@
|
||||
/*\
|
||||
title: $:/core/modules/serverroute/get-file.js
|
||||
type: application/javascript
|
||||
module-type: serverroute
|
||||
|
||||
GET /files/:filepath
|
||||
|
||||
\*/
|
||||
(function() {
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports.method = "GET";
|
||||
|
||||
exports.path = /^\/files\/(.+)$/;
|
||||
|
||||
exports.handler = function(request,response,state) {
|
||||
var path = require("path"),
|
||||
fs = require("fs"),
|
||||
util = require("util");
|
||||
var filename = path.join($tw.boot.wikiPath,"files",decodeURIComponent(state.params[0])),
|
||||
extension = path.extname(filename);
|
||||
fs.readFile(filename,function(err,content) {
|
||||
var status,content,type = "text/plain";
|
||||
if(err) {
|
||||
if(err.code === "ENOENT") {
|
||||
status = 404;
|
||||
content = "File '" + filename + "' not found";
|
||||
} if(err.code === "EACCES") {
|
||||
status = 403;
|
||||
content = "You do not have permission to access the file '" + filename + "'";
|
||||
} else {
|
||||
status = 500;
|
||||
content = err.toString();
|
||||
}
|
||||
} else {
|
||||
status = 200;
|
||||
content = content;
|
||||
type = $tw.config.fileExtensionInfo[extension] || "application/octet-stream";
|
||||
}
|
||||
response.writeHead(status,{
|
||||
"Content-Type": type
|
||||
});
|
||||
response.end(content);
|
||||
});
|
||||
};
|
||||
|
||||
}());
|
@@ -1,60 +0,0 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/server-static-assets/get-server-asset.js
|
||||
type: application/javascript
|
||||
module-type: serverroute
|
||||
|
||||
GET /assets/:server_asset
|
||||
|
||||
\*/
|
||||
/*jshint maxlen:false */
|
||||
(function() {
|
||||
if(!$tw.node) { return; }
|
||||
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var util = require("util");
|
||||
|
||||
var RESPONSES = {
|
||||
_ok: function(filename,content) {
|
||||
var extension = path.extname(filename);
|
||||
return {
|
||||
status: 200,
|
||||
content: content,
|
||||
type: $tw.config.fileExtensionInfo[extension] || "application/octet-stream"
|
||||
};
|
||||
},
|
||||
_error: function(filename,err) {
|
||||
return {status: 500, content: err.toString(), type: "text/plain"};
|
||||
},
|
||||
ENOENT: function(filename) {
|
||||
return {
|
||||
status: 404,
|
||||
content: "File '" + filename + "' not found.",
|
||||
type: "text/plain"
|
||||
};
|
||||
},
|
||||
EACCES: function(filename) {
|
||||
return {
|
||||
status: 403,
|
||||
content: "File '" + filename + "' is forbidden (permissions).",
|
||||
type: "text/plain"
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
method: "GET",
|
||||
path: /^\/assets\/(.+)$/,
|
||||
|
||||
handler: function(request,response,state) {
|
||||
var filename = path.join($tw.boot.wikiPath, "assets", state.params[0]);
|
||||
var extension = path.extname(filename);
|
||||
fs.readFile(filename,function(err,content) {
|
||||
var contentInfo = err ? RESPONSES[err.code] || RESPONSES._error : RESPONSES._ok;
|
||||
var responseData = contentInfo(filename, err || content);
|
||||
response.writeHead(responseData.status, {"Content-Type": responseData.type});
|
||||
response.end(responseData.content);
|
||||
});
|
||||
}
|
||||
};
|
||||
}());
|
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"title": "$:/plugins/tiddlywiki/server-static-assets",
|
||||
"description": "Static Server Assets",
|
||||
"author": "Sukima",
|
||||
"core-version": ">=5.0.0",
|
||||
"list": "readme"
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
title: $:/plugins/tiddlywiki/server-static-assets/readme
|
||||
|
||||
This plugin allows the Node.JS server to serve static files from the filesystem.
|
||||
|
||||
Any files that are placed in `assets` can be served to the browser directly.
|
||||
|
||||
For example, if you had a wiki editions with the following directory structure:
|
||||
|
||||
```
|
||||
editions/tw5.com-server
|
||||
├── assets
|
||||
│ └── pony.png
|
||||
├── tiddlers
|
||||
│ └── (All the tiddler files)
|
||||
└── tiddlywiki.info
|
||||
```
|
||||
|
||||
And the command `tiddlywiki editions/tw5.com-server --server` then pointing your browser to `http://localhost:8080/assets/pony.png` would display the image.
|
Reference in New Issue
Block a user