From 552d44bbd28eb66c05b450f112ce20bff463fe6f Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Thu, 17 May 2018 15:49:40 +0200 Subject: Make folder listing a global setting --- .gitignore | 1 + frontend/404.html | 19 +++++++++++++++ frontend/js/app.js | 10 +++++++- frontend/welcome.html | 5 ++-- server.js | 67 ++++++++++++++++++++++++++++++++++++++++----------- 5 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 frontend/404.html diff --git a/.gitignore b/.gitignore index 7efeed8..a34adba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ files/ .users.json +.config.json diff --git a/frontend/404.html b/frontend/404.html new file mode 100644 index 0000000..21fd6b4 --- /dev/null +++ b/frontend/404.html @@ -0,0 +1,19 @@ + + + Surfer - File not found + + + + + + + +
+
+

Surfer

+

File not found

+
+
+ + + diff --git a/frontend/js/app.js b/frontend/js/app.js index 57b831e..e9d469b 100644 --- a/frontend/js/app.js +++ b/frontend/js/app.js @@ -211,7 +211,9 @@ var app = new Vue({ }, onOptionsMenu: function (command) { if (command === 'folderListing') { - console.log('Not implemented'); + superagent.put('/api/settings').send({ folderListingEnabled: this.folderListingEnabled }).query({ access_token: localStorage.accessToken }).end(function (error) { + if (error) console.error(error); + }); } else if (command === 'about') { this.$msgbox({ title: 'About Surfer', @@ -337,6 +339,12 @@ getProfile(localStorage.accessToken, function (error) { if (error) return console.error(error); loadDirectory(window.location.hash.slice(1)); + + superagent.get('/api/settings').query({ access_token: localStorage.accessToken }).end(function (error, result) { + if (error) console.error(error); + + app.folderListingEnabled = !!result.body.folderListingEnabled; + }); }); $(window).on('hashchange', function () { diff --git a/frontend/welcome.html b/frontend/welcome.html index d16dcef..ddc9c36 100644 --- a/frontend/welcome.html +++ b/frontend/welcome.html @@ -1,9 +1,10 @@ - Surfer + Surfer - Welcome - + + diff --git a/server.js b/server.js index f9dd4f5..4a8bfe5 100755 --- a/server.js +++ b/server.js @@ -7,17 +7,58 @@ var express = require('express'), morgan = require('morgan'), passport = require('passport'), path = require('path'), + fs = require('fs'), compression = require('compression'), session = require('express-session'), bodyParser = require('body-parser'), cookieParser = require('cookie-parser'), lastMile = require('connect-lastmile'), + HttpError = require('connect-lastmile').HttpError, + HttpSuccess = require('connect-lastmile').HttpSuccess, multipart = require('./src/multipart'), mkdirp = require('mkdirp'), auth = require('./src/auth.js'), serveIndex = require('serve-index'), files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); + +var rootFolder = path.resolve(__dirname, process.argv[2] || 'files'); +var configFile = path.resolve(__dirname, process.argv[3] || './config.json'); + +// Ensure the root folder exists +mkdirp.sync(rootFolder); + +var config = { + folderListingEnabled: true +}; + +function getSettings(req, res, next) { + res.send({ folderListingEnabled: !!config.folderListingEnabled }); +} + +function setSettings(req, res, next) { + if (typeof req.body.folderListingEnabled === 'undefined') return next(new HttpError(400, 'missing folderListingEnabled boolean')); + + config.folderListingEnabled = !!req.body.folderListingEnabled; + + fs.writeFile(configFile, JSON.stringify(config), function (error) { + if (error) return next(new HttpError(500, 'unable to save settings')); + + next(new HttpSuccess(201, {})); + }); +} + +// Load the config file +try { + config = require(configFile); +} catch (e) { + if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${configFile} not found`); + else console.log(`Cannot load config file ${configFile}`, e); +} + +if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; + +// Setup the express server and routes var app = express(); var router = new express.Router(); @@ -25,6 +66,8 @@ var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 router.post ('/api/login', auth.login); router.post ('/api/logout', auth.verify, auth.logout); +router.get ('/api/settings', auth.verify, getSettings); +router.put ('/api/settings', auth.verify, setSettings); router.get ('/api/profile', auth.verify, auth.getProfile); router.get ('/api/files/*', auth.verify, files.get); router.post ('/api/files/*', auth.verify, multipart, files.post); @@ -32,14 +75,6 @@ router.put ('/api/files/*', auth.verify, files.put); router.delete('/api/files/*', auth.verify, files.del); router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); }); -// welcome screen in case / does not serve up any file yet -function welcomePage(req, res, next) { - if (req.path !== '/') return next(); - res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html')); -} - -var rootFolder = path.resolve(__dirname, process.argv[2] || 'files'); - app.use(morgan('dev')); app.use(compression()); app.use('/api', bodyParser.json()); @@ -51,7 +86,14 @@ app.use('/api', passport.session()); app.use(router); app.use('/_admin', express.static(__dirname + '/frontend')); app.use('/', express.static(rootFolder)); -app.use('/', welcomePage); +app.use('/', function welcomePage(req, res, next) { + if (req.path !== '/') return next(); + res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html')); +}); +app.use('/', function (req, res, next) { + if (config.folderListingEnabled) return next(); + res.sendFile(__dirname + '/frontend/404.html'); +}); app.use('/', serveIndex(rootFolder, { icons: true })); app.use(lastMile()); @@ -59,9 +101,6 @@ var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; - var basePath = path.resolve(__dirname, process.argv[2] || 'files'); - mkdirp.sync(basePath); - - console.log('Surfer listening at http://%s:%s', host, port); - console.log('Using base path', basePath); + console.log('Surfer listening on http://%s:%s', host, port); + console.log('Using base path', rootFolder); }); -- cgit v1.2.3