diff --git a/core/modules/serverroute/get-file.js b/core/modules/serverroute/get-file.js new file mode 100644 index 000000000..09f17712d --- /dev/null +++ b/core/modules/serverroute/get-file.js @@ -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); + }); +}; + +}()); diff --git a/plugins/tiddlywiki/server-static-assets/get-server-asset.js b/plugins/tiddlywiki/server-static-assets/get-server-asset.js deleted file mode 100644 index 7ab0747d3..000000000 --- a/plugins/tiddlywiki/server-static-assets/get-server-asset.js +++ /dev/null @@ -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); - }); - } - }; -}()); diff --git a/plugins/tiddlywiki/server-static-assets/plugin.info b/plugins/tiddlywiki/server-static-assets/plugin.info deleted file mode 100644 index 7a78f3a86..000000000 --- a/plugins/tiddlywiki/server-static-assets/plugin.info +++ /dev/null @@ -1,7 +0,0 @@ -{ - "title": "$:/plugins/tiddlywiki/server-static-assets", - "description": "Static Server Assets", - "author": "Sukima", - "core-version": ">=5.0.0", - "list": "readme" -} diff --git a/plugins/tiddlywiki/server-static-assets/readme.tid b/plugins/tiddlywiki/server-static-assets/readme.tid deleted file mode 100644 index 26ed90949..000000000 --- a/plugins/tiddlywiki/server-static-assets/readme.tid +++ /dev/null @@ -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.