* Added ability to serve static files from web server

* Web server can have custom error pages, e.g. 404.html
* "file_area" stuff -> "file_base"
* Fix some rare bugs in theme/art loading
* Adjust tab order dynamically for file upload details
This commit is contained in:
Bryan Ashby 2017-02-04 09:20:36 -07:00
parent ff64a7aed5
commit 92772eb1a9
14 changed files with 165 additions and 48 deletions

View file

@ -11,6 +11,8 @@ const http = require('http');
const https = require('https');
const _ = require('lodash');
const fs = require('fs');
const paths = require('path');
const mimeTypes = require('mime-types');
const ModuleInfo = exports.moduleInfo = {
name : 'Web',
@ -55,6 +57,14 @@ exports.getModule = class WebServerModule extends ServerModule {
this.enableHttps = Config.contentServers.web.https.enabled || false;
this.routes = {};
if(Config.contentServers.web.staticRoot) {
this.addRoute({
method : 'GET',
path : '/static/.*$',
handler : this.routeStaticFile,
});
}
}
createServer() {
@ -116,8 +126,54 @@ exports.getModule = class WebServerModule extends ServerModule {
return route ? route.handler(req, resp) : this.accessDenied(resp);
}
accessDenied(resp) {
resp.writeHead(401, { 'Content-Type' : 'text/html' } );
return resp.end('<html><body>Access denied</body></html>');
respondWithError(resp, code, bodyText, title) {
const customErrorPage = paths.join(Config.contentServers.web.staticRoot, `${code}.html`);
fs.readFile(customErrorPage, 'utf8', (err, data) => {
resp.writeHead(code, { 'Content-Type' : 'text/html' } );
if(err) {
return resp.end(`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<article>
<h2>${bodyText}</h2>
</article>
</body>
</html>`
);
}
return resp.end(data);
});
}
}
accessDenied(resp) {
return this.respondWithError(resp, 401, 'Access denied.', 'Access Denied');
}
routeStaticFile(req, resp) {
const fileName = req.url.substr(req.url.indexOf('/', 1));
const filePath = paths.join(Config.contentServers.web.staticRoot, fileName);
fs.stat(filePath, (err, stats) => {
if(err) {
return this.respondWithError(resp, 404, 'File not found.', 'File Not Found');
}
const headers = {
'Content-Type' : mimeTypes.contentType(filePath) || mimeTypes.contentType('.bin'),
'Content-Length' : stats.size,
};
const readStream = fs.createReadStream(filePath);
resp.writeHead(200, headers);
return readStream.pipe(resp);
});
}
};