aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJohannes Zellner <johannes@cloudron.io>2018-05-17 15:49:40 +0200
committerJohannes Zellner <johannes@cloudron.io>2018-05-17 15:49:40 +0200
commit552d44bbd28eb66c05b450f112ce20bff463fe6f (patch)
treea0662e0c4f0328f9a5884ad7794275a02bd00159
parentf2f2b69322749c558f2dac8bf21b1620aacc284b (diff)
downloadSurfer-552d44bbd28eb66c05b450f112ce20bff463fe6f.tar.gz
Surfer-552d44bbd28eb66c05b450f112ce20bff463fe6f.tar.zst
Surfer-552d44bbd28eb66c05b450f112ce20bff463fe6f.zip
Make folder listing a global setting
-rw-r--r--.gitignore1
-rw-r--r--frontend/404.html19
-rw-r--r--frontend/js/app.js10
-rw-r--r--frontend/welcome.html5
-rwxr-xr-xserver.js67
5 files changed, 85 insertions, 17 deletions
diff --git a/.gitignore b/.gitignore
index 7efeed8..a34adba 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
1node_modules/ 1node_modules/
2files/ 2files/
3.users.json 3.users.json
4.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 @@
1<html>
2<head>
3 <title> Surfer - File not found</title>
4
5 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
6
7 <link rel="stylesheet" href="/_admin/css/style.css">
8</head>
9<body>
10
11<div class="wrapper">
12 <div class="content">
13 <h2> Surfer </h2>
14 <p>File not found</p>
15 </div>
16</div>
17
18</body>
19</html>
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({
211 }, 211 },
212 onOptionsMenu: function (command) { 212 onOptionsMenu: function (command) {
213 if (command === 'folderListing') { 213 if (command === 'folderListing') {
214 console.log('Not implemented'); 214 superagent.put('/api/settings').send({ folderListingEnabled: this.folderListingEnabled }).query({ access_token: localStorage.accessToken }).end(function (error) {
215 if (error) console.error(error);
216 });
215 } else if (command === 'about') { 217 } else if (command === 'about') {
216 this.$msgbox({ 218 this.$msgbox({
217 title: 'About Surfer', 219 title: 'About Surfer',
@@ -337,6 +339,12 @@ getProfile(localStorage.accessToken, function (error) {
337 if (error) return console.error(error); 339 if (error) return console.error(error);
338 340
339 loadDirectory(window.location.hash.slice(1)); 341 loadDirectory(window.location.hash.slice(1));
342
343 superagent.get('/api/settings').query({ access_token: localStorage.accessToken }).end(function (error, result) {
344 if (error) console.error(error);
345
346 app.folderListingEnabled = !!result.body.folderListingEnabled;
347 });
340}); 348});
341 349
342$(window).on('hashchange', function () { 350$(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 @@
1<html> 1<html>
2<head> 2<head>
3 <title> Surfer </title> 3 <title> Surfer - Welcome</title>
4 4
5 <link rel="stylesheet" href="/_admin/css/style.css"> 5 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
6 6
7 <link rel="stylesheet" href="/_admin/css/style.css">
7</head> 8</head>
8<body> 9<body>
9 10
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'),
7 morgan = require('morgan'), 7 morgan = require('morgan'),
8 passport = require('passport'), 8 passport = require('passport'),
9 path = require('path'), 9 path = require('path'),
10 fs = require('fs'),
10 compression = require('compression'), 11 compression = require('compression'),
11 session = require('express-session'), 12 session = require('express-session'),
12 bodyParser = require('body-parser'), 13 bodyParser = require('body-parser'),
13 cookieParser = require('cookie-parser'), 14 cookieParser = require('cookie-parser'),
14 lastMile = require('connect-lastmile'), 15 lastMile = require('connect-lastmile'),
16 HttpError = require('connect-lastmile').HttpError,
17 HttpSuccess = require('connect-lastmile').HttpSuccess,
15 multipart = require('./src/multipart'), 18 multipart = require('./src/multipart'),
16 mkdirp = require('mkdirp'), 19 mkdirp = require('mkdirp'),
17 auth = require('./src/auth.js'), 20 auth = require('./src/auth.js'),
18 serveIndex = require('serve-index'), 21 serveIndex = require('serve-index'),
19 files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); 22 files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files'));
20 23
24
25var rootFolder = path.resolve(__dirname, process.argv[2] || 'files');
26var configFile = path.resolve(__dirname, process.argv[3] || './config.json');
27
28// Ensure the root folder exists
29mkdirp.sync(rootFolder);
30
31var config = {
32 folderListingEnabled: true
33};
34
35function getSettings(req, res, next) {
36 res.send({ folderListingEnabled: !!config.folderListingEnabled });
37}
38
39function setSettings(req, res, next) {
40 if (typeof req.body.folderListingEnabled === 'undefined') return next(new HttpError(400, 'missing folderListingEnabled boolean'));
41
42 config.folderListingEnabled = !!req.body.folderListingEnabled;
43
44 fs.writeFile(configFile, JSON.stringify(config), function (error) {
45 if (error) return next(new HttpError(500, 'unable to save settings'));
46
47 next(new HttpSuccess(201, {}));
48 });
49}
50
51// Load the config file
52try {
53 config = require(configFile);
54} catch (e) {
55 if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${configFile} not found`);
56 else console.log(`Cannot load config file ${configFile}`, e);
57}
58
59if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true;
60
61// Setup the express server and routes
21var app = express(); 62var app = express();
22var router = new express.Router(); 63var router = new express.Router();
23 64
@@ -25,6 +66,8 @@ var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3
25 66
26router.post ('/api/login', auth.login); 67router.post ('/api/login', auth.login);
27router.post ('/api/logout', auth.verify, auth.logout); 68router.post ('/api/logout', auth.verify, auth.logout);
69router.get ('/api/settings', auth.verify, getSettings);
70router.put ('/api/settings', auth.verify, setSettings);
28router.get ('/api/profile', auth.verify, auth.getProfile); 71router.get ('/api/profile', auth.verify, auth.getProfile);
29router.get ('/api/files/*', auth.verify, files.get); 72router.get ('/api/files/*', auth.verify, files.get);
30router.post ('/api/files/*', auth.verify, multipart, files.post); 73router.post ('/api/files/*', auth.verify, multipart, files.post);
@@ -32,14 +75,6 @@ router.put ('/api/files/*', auth.verify, files.put);
32router.delete('/api/files/*', auth.verify, files.del); 75router.delete('/api/files/*', auth.verify, files.del);
33router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); }); 76router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); });
34 77
35// welcome screen in case / does not serve up any file yet
36function welcomePage(req, res, next) {
37 if (req.path !== '/') return next();
38 res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html'));
39}
40
41var rootFolder = path.resolve(__dirname, process.argv[2] || 'files');
42
43app.use(morgan('dev')); 78app.use(morgan('dev'));
44app.use(compression()); 79app.use(compression());
45app.use('/api', bodyParser.json()); 80app.use('/api', bodyParser.json());
@@ -51,7 +86,14 @@ app.use('/api', passport.session());
51app.use(router); 86app.use(router);
52app.use('/_admin', express.static(__dirname + '/frontend')); 87app.use('/_admin', express.static(__dirname + '/frontend'));
53app.use('/', express.static(rootFolder)); 88app.use('/', express.static(rootFolder));
54app.use('/', welcomePage); 89app.use('/', function welcomePage(req, res, next) {
90 if (req.path !== '/') return next();
91 res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html'));
92});
93app.use('/', function (req, res, next) {
94 if (config.folderListingEnabled) return next();
95 res.sendFile(__dirname + '/frontend/404.html');
96});
55app.use('/', serveIndex(rootFolder, { icons: true })); 97app.use('/', serveIndex(rootFolder, { icons: true }));
56app.use(lastMile()); 98app.use(lastMile());
57 99
@@ -59,9 +101,6 @@ var server = app.listen(3000, function () {
59 var host = server.address().address; 101 var host = server.address().address;
60 var port = server.address().port; 102 var port = server.address().port;
61 103
62 var basePath = path.resolve(__dirname, process.argv[2] || 'files'); 104 console.log('Surfer listening on http://%s:%s', host, port);
63 mkdirp.sync(basePath); 105 console.log('Using base path', rootFolder);
64
65 console.log('Surfer listening at http://%s:%s', host, port);
66 console.log('Using base path', basePath);
67}); 106});