Skip to content

Commit

Permalink
* Added ability to serve static files from web server
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
NuSkooler committed Feb 4, 2017
1 parent ff64a7a commit 92772eb
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 47 deletions.
49 changes: 49 additions & 0 deletions UPGRADE.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
INTRODUCTION
-------------------------------------------------------------------------------
This document covers basic upgrade notes for major ENiGMA½.


BEFORE UPGRADING
-------------------------------------------------------------------------------
* Always back ALL files in the 'db' directory
* Back up your menu.hjson (or renamed equivalent)


GENERAL NOTES
-------------------------------------------------------------------------------
Upgrades often come with changes to the default menu.hjson. It is wise to
use a *different* file name for your BBS's version of this file and point to
it via config.hjson. For example:

general: {
menuFile: my_bbs.hjson
}

After updating code, use a program such as DiffMerge to merge in updates to
my_bbs.hjson from the shipping menu.hjson.


FROM GITHUB
-------------------------------------------------------------------------------
Upgrading from GitHub is easy:

cd /path/to/enigma-bbs
git pull
npm install


PROBLEMS
-------------------------------------------------------------------------------
Report your issue on Xibalba BBS, hop in #enigma-bbs on Freenet and chat, or
file a issue on GitHub.


0.0.1-alpha to 0.0.4-alpha
-------------------------------------------------------------------------------
* Manual Database Upgrade
sqlite3 db/message.sqlite
INSERT INTO message_fts(message_fts) VALUES('rebuild');

* Archiver Changes
If you have overridden or made additions to archivers in your config.hjson
you will need to update them. See docs/archive.md and core/config.js
10 changes: 7 additions & 3 deletions core/art.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getArtFromPath(path, options, cb) {
return iconv.decode(data, encoding);
} else {
const eofMarker = defaultEofFromExtension(ext);
return iconv.decode(sliceAtEOF(data, eofMarker), encoding);
return iconv.decode(eofMarker ? sliceAtEOF(data, eofMarker) : data, encoding);
}
}

Expand Down Expand Up @@ -213,11 +213,15 @@ function getArt(name, options, cb) {
}

function defaultEncodingFromExtension(ext) {
return SUPPORTED_ART_TYPES[ext.toLowerCase()].defaultEncoding;
const artType = SUPPORTED_ART_TYPES[ext.toLowerCase()];
return artType ? artType.defaultEncoding : 'utf8';
}

function defaultEofFromExtension(ext) {
return SUPPORTED_ART_TYPES[ext.toLowerCase()].eof;
const artType = SUPPORTED_ART_TYPES[ext.toLowerCase()];
if(artType) {
return artType.eof;
}
}

// :TODO: Implement the following
Expand Down
5 changes: 5 additions & 0 deletions core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ function getDefaultConfig() {
contentServers : {
web : {
domain : 'another-fine-enigma-bbs.org',

staticRoot : paths.join(__dirname, './../www'),

http : {
enabled : false,
Expand Down Expand Up @@ -364,6 +366,9 @@ function getDefaultConfig() {
},

fileTransferProtocols : {
//
// See http://www.synchro.net/docs/sexyz.txt for information on SEXYZ
//
zmodem8kSexyz : {
name : 'ZModem 8k (SEXYZ)',
type : 'external',
Expand Down
17 changes: 7 additions & 10 deletions core/file_area_web.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class FileAreaWebAccess {
return self.load(callback);
},
function addWebRoute(callback) {
const webServer = getServer(WEB_SERVER_PACKAGE_NAME);
if(!webServer) {
self.webServer = getServer(WEB_SERVER_PACKAGE_NAME);
if(!self.webServer) {
return callback(Errors.DoesNotExist(`Server with package name "${WEB_SERVER_PACKAGE_NAME}" does not exist`));
}

const routeAdded = webServer.instance.addRoute({
const routeAdded = self.webServer.instance.addRoute({
method : 'GET',
path : '/f/[a-zA-Z0-9]+$', // :TODO: allow this to be configurable
handler : self.routeWebRequest.bind(self),
handler : self.routeWebRequestForFile.bind(self),
});

return callback(routeAdded ? null : Errors.General('Failed adding route'));
Expand Down Expand Up @@ -217,13 +217,10 @@ class FileAreaWebAccess {
}

fileNotFound(resp) {
resp.writeHead(404, { 'Content-Type' : 'text/html' } );

// :TODO: allow custom 404 - mods/<theme>/file_area_web-404.html
return resp.end('<html><body>Not found</html>');
this.webServer.instance.respondWithError(resp, 404, 'File not found.', 'File Not Found');
}

routeWebRequest(req, resp) {
routeWebRequestForFile(req, resp) {
const hashId = paths.basename(req.url);

this.loadServedHashId(hashId, (err, servedItem) => {
Expand Down Expand Up @@ -259,7 +256,7 @@ class FileAreaWebAccess {
});

const headers = {
'Content-Type' : mimeTypes.contentType(paths.extname(filePath)) || mimeTypes.contentType('.bin'),
'Content-Type' : mimeTypes.contentType(filePath) || mimeTypes.contentType('.bin'),
'Content-Length' : stats.size,
'Content-Disposition' : `attachment; filename="${fileEntry.fileName}"`,
};
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions core/transfer_file.js → core/file_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ const SYSTEM_EOL = require('os').EOL;
const TEMP_SUFFIX = 'enigtf-'; // temp CWD/etc.

/*
Notes
-----------------------------------------------------------------------------
See core/config.js for external protocol configuration
Resources
-----------------------------------------------------------------------------
ZModem
* http://gallium.inria.fr/~doligez/zmodem/zmodem.txt
* https://github.com/protomouse/synchronet/blob/master/src/sbbs3/zmodem.c
*/

exports.moduleInfo = {
Expand Down
1 change: 0 additions & 1 deletion core/listening_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const logger = require('./logger.js');

// deps
const async = require('async');
const _ = require('lodash');

const listeningServers = {}; // packageName -> info

Expand Down
62 changes: 59 additions & 3 deletions core/servers/content/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -116,8 +126,54 @@ exports.getModule = class WebServerModule extends ServerModule {
return route ? route.handler(req, resp) : this.accessDenied(resp);
}

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) {
resp.writeHead(401, { 'Content-Type' : 'text/html' } );
return resp.end('<html><body>Access denied</body></html>');
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);
});
}
};
22 changes: 13 additions & 9 deletions core/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,16 @@ function setClientTheme(client, themeId) {
function getThemeArt(options, cb) {
//
// options - required:
// name
// client
// name
//
// options - optional
// themeId
// asAnsi
// readSauce
// random
// client - needed for user's theme/etc.
// themeId
// asAnsi
// readSauce
// random
//
if(!options.themeId && _.has(options.client, 'user.properties.theme_id')) {
if(!options.themeId && _.has(options, 'client.user.properties.theme_id')) {
options.themeId = options.client.user.properties.theme_id;
} else {
options.themeId = Config.defaults.theme;
Expand Down Expand Up @@ -437,9 +437,13 @@ function getThemeArt(options, cb) {
],
function complete(err, artInfo) {
if(err) {
options.client.log.debug( { error : err }, 'Cannot find art');
if(options.client) {
options.client.log.debug( { error : err.message }, 'Cannot find theme art' );
} else {
Log.debug( { error : err.message }, 'Cannot find theme art' );
}
}
cb(err, artInfo);
return cb(err, artInfo);
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion mods/file_area_filter_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// ENiGMA½
const MenuModule = require('../core/menu_module.js').MenuModule;
const ViewController = require('../core/view_controller.js').ViewController;
const getSortedAvailableFileAreas = require('../core/file_area.js').getSortedAvailableFileAreas;
const getSortedAvailableFileAreas = require('../core/file_base_area.js').getSortedAvailableFileAreas;
const FileBaseFilters = require('../core/file_base_filter.js');
const stringFormat = require('../core/string_format.js');

Expand Down
8 changes: 1 addition & 7 deletions mods/file_area_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const theme = require('../core/theme.js');
const FileEntry = require('../core/file_entry.js');
const stringFormat = require('../core/string_format.js');
const createCleanAnsi = require('../core/string_util.js').createCleanAnsi;
const FileArea = require('../core/file_area.js');
const FileArea = require('../core/file_base_area.js');
const Errors = require('../core/enig_error.js').Errors;
const ArchiveUtil = require('../core/archive_util.js');
const Config = require('../core/config.js').config;
Expand All @@ -23,12 +23,6 @@ const cleanControlCodes = require('../core/string_util.js').cleanControlCodes;
const async = require('async');
const _ = require('lodash');
const moment = require('moment');
const paths = require('path');

/*
Misc TODO
*/

exports.moduleInfo = {
name : 'File Area List',
Expand Down
25 changes: 13 additions & 12 deletions mods/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// enigma-bbs
const MenuModule = require('../core/menu_module.js').MenuModule;
const stringFormat = require('../core/string_format.js');
const getSortedAvailableFileAreas = require('../core/file_area.js').getSortedAvailableFileAreas;
const getAreaDefaultStorageDirectory = require('../core/file_area.js').getAreaDefaultStorageDirectory;
const scanFile = require('../core/file_area.js').scanFile;
const getSortedAvailableFileAreas = require('../core/file_base_area.js').getSortedAvailableFileAreas;
const getAreaDefaultStorageDirectory = require('../core/file_base_area.js').getAreaDefaultStorageDirectory;
const scanFile = require('../core/file_base_area.js').scanFile;
const ansiGoto = require('../core/ansi_term.js').goto;
const moveFileWithCollisionHandling = require('../core/file_util.js').moveFileWithCollisionHandling;
const pathWithTerminatingSeparator = require('../core/file_util.js').pathWithTerminatingSeparator;
Expand Down Expand Up @@ -560,20 +560,21 @@ exports.getModule = class UploadModule extends MenuModule {
},
function populateViews(callback) {
const descView = self.viewControllers.fileDetails.getView(MciViewIds.fileDetails.desc);

const tagsView = self.viewControllers.fileDetails.getView(MciViewIds.fileDetails.tags);
const yearView = self.viewControllers.fileDetails.getView(MciViewIds.fileDetails.estYear);

tagsView.setText( Array.from(fileEntry.hashTags).join(',') ); // :TODO: optional 'hashTagsSep' like file list/browse
yearView.setText(fileEntry.meta.est_release_year || '');

if(self.fileEntryHasDetectedDesc(fileEntry)) {
descView.setText(fileEntry.desc);
descView.setPropertyValue('mode', 'preview');

// :TODO: it would be nice to take this out of the focus order
self.viewControllers.fileDetails.switchFocus(MciViewIds.fileDetails.tags);
descView.acceptsFocus = false;
} else {
self.viewControllers.fileDetails.switchFocus(MciViewIds.fileDetails.desc);
}

const tagsView = self.viewControllers.fileDetails.getView(MciViewIds.fileDetails.tags);
tagsView.setText( Array.from(fileEntry.hashTags).join(',') ); // :TODO: optional 'hashTagsSep' like file list/browse

const yearView = self.viewControllers.fileDetails.getView(MciViewIds.fileDetails.estYear);
yearView.setText(fileEntry.meta.est_release_year || '');

return callback(null);
}
],
Expand Down
2 changes: 1 addition & 1 deletion oputil.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function fileAreaScan() {
return initConfigAndDatabases(callback);
},
function getFileArea(callback) {
const fileAreaMod = require('./core/file_area.js');
const fileAreaMod = require('./core/file_base_area.js');

const areaInfo = fileAreaMod.getFileAreaByTag(argv.scan);
if(!areaInfo) {
Expand Down
Empty file added www/.gitkeep
Empty file.

0 comments on commit 92772eb

Please sign in to comment.