]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env node | |
2 | ||
3 | 'use strict'; | |
4 | ||
5 | var express = require('express'), | |
6 | morgan = require('morgan'), | |
7 | path = require('path'), | |
8 | fs = require('fs'), | |
9 | compression = require('compression'), | |
10 | session = require('express-session'), | |
11 | bodyParser = require('body-parser'), | |
12 | cookieParser = require('cookie-parser'), | |
13 | lastMile = require('connect-lastmile'), | |
14 | HttpError = require('connect-lastmile').HttpError, | |
15 | HttpSuccess = require('connect-lastmile').HttpSuccess, | |
16 | multipart = require('./src/multipart'), | |
17 | mkdirp = require('mkdirp'), | |
18 | auth = require('./src/auth.js'), | |
19 | serveIndex = require('serve-index'), | |
20 | webdav = require('webdav-server').v2, | |
21 | files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); | |
22 | ||
23 | ||
24 | const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files'); | |
25 | const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json'); | |
26 | ||
27 | // Ensure the root folder exists | |
28 | mkdirp.sync(ROOT_FOLDER); | |
29 | ||
30 | var config = { | |
31 | folderListingEnabled: false | |
32 | }; | |
33 | ||
34 | function getSettings(req, res, next) { | |
35 | res.send({ folderListingEnabled: !!config.folderListingEnabled }); | |
36 | } | |
37 | ||
38 | function setSettings(req, res, next) { | |
39 | if (typeof req.body.folderListingEnabled === 'undefined') return next(new HttpError(400, 'missing folderListingEnabled boolean')); | |
40 | ||
41 | config.folderListingEnabled = !!req.body.folderListingEnabled; | |
42 | ||
43 | fs.writeFile(CONFIG_FILE, JSON.stringify(config), function (error) { | |
44 | if (error) return next(new HttpError(500, 'unable to save settings')); | |
45 | ||
46 | next(new HttpSuccess(201, {})); | |
47 | }); | |
48 | } | |
49 | ||
50 | // Load the config file | |
51 | try { | |
52 | console.log(`Using config file: ${CONFIG_FILE}`); | |
53 | config = require(CONFIG_FILE); | |
54 | } catch (e) { | |
55 | if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${CONFIG_FILE} not found`); | |
56 | else console.log(`Cannot load config file ${CONFIG_FILE}`, e); | |
57 | } | |
58 | ||
59 | if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; | |
60 | ||
61 | // Setup the express server and routes | |
62 | var app = express(); | |
63 | var router = new express.Router(); | |
64 | ||
65 | var webdavServer = new webdav.WebDAVServer({ | |
66 | requireAuthentification: true, | |
67 | httpAuthentication: new webdav.HTTPBasicAuthentication(new auth.WebdavUserManager(), 'Cloudron Surfer') | |
68 | }); | |
69 | ||
70 | webdavServer.setFileSystem('/', new webdav.PhysicalFileSystem(ROOT_FOLDER), function (success) { | |
71 | console.log(`Mounting ${ROOT_FOLDER} as webdav resource`, success); | |
72 | }); | |
73 | ||
74 | var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 }); | |
75 | ||
76 | router.post ('/api/login', auth.login); | |
77 | router.post ('/api/logout', auth.verify, auth.logout); | |
78 | router.get ('/api/settings', auth.verify, getSettings); | |
79 | router.put ('/api/settings', auth.verify, setSettings); | |
80 | router.get ('/api/profile', auth.verify, auth.getProfile); | |
81 | router.get ('/api/files/*', auth.verify, files.get); | |
82 | router.post ('/api/files/*', auth.verify, multipart, files.post); | |
83 | router.put ('/api/files/*', auth.verify, files.put); | |
84 | router.delete('/api/files/*', auth.verify, files.del); | |
85 | router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); }); | |
86 | ||
87 | app.use(morgan('dev')); | |
88 | app.use(compression()); | |
89 | app.use('/api', bodyParser.json()); | |
90 | app.use('/api', bodyParser.urlencoded({ extended: false, limit: '100mb' })); | |
91 | app.use('/api', cookieParser()); | |
92 | app.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitialized: false })); | |
93 | app.use(router); | |
94 | app.use(webdav.extensions.express('/_webdav', webdavServer)); | |
95 | app.use('/_admin', express.static(__dirname + '/frontend')); | |
96 | app.use('/', express.static(ROOT_FOLDER)); | |
97 | app.use('/', function welcomePage(req, res, next) { | |
98 | if (config.folderListingEnabled || req.path !== '/') return next(); | |
99 | res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html')); | |
100 | }); | |
101 | app.use('/', function (req, res, next) { | |
102 | if (config.folderListingEnabled) return next(); | |
103 | res.status(404).sendFile(__dirname + '/frontend/404.html'); | |
104 | }); | |
105 | app.use('/', serveIndex(ROOT_FOLDER, { icons: true })); | |
106 | app.use(lastMile()); | |
107 | ||
108 | var server = app.listen(3000, function () { | |
109 | var host = server.address().address; | |
110 | var port = server.address().port; | |
111 | ||
112 | console.log('Surfer listening on http://%s:%s', host, port); | |
113 | console.log('Using base path', ROOT_FOLDER); | |
114 | }); |