X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server.js;h=ec123e90a5ead763504202991256bc8329008dbb;hb=26fd90fa1025c98327f60335d19ddd867e130f2a;hp=f661e873ed0e718f726496a2633dccdadf90c356;hpb=8a5c7d41224e6c232ea6095168f456d1887e112c;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git diff --git a/server.js b/server.js index f661e87..ec123e9 100755 --- a/server.js +++ b/server.js @@ -2,7 +2,6 @@ 'use strict'; - var express = require('express'), morgan = require('morgan'), passport = require('passport'), @@ -19,14 +18,15 @@ var express = require('express'), mkdirp = require('mkdirp'), auth = require('./src/auth.js'), serveIndex = require('serve-index'), + webdav = require('webdav-server').v2, 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'); +const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files'); +const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json'); // Ensure the root folder exists -mkdirp.sync(rootFolder); +mkdirp.sync(ROOT_FOLDER); var config = { folderListingEnabled: false @@ -41,7 +41,7 @@ function setSettings(req, res, next) { config.folderListingEnabled = !!req.body.folderListingEnabled; - fs.writeFile(configFile, JSON.stringify(config), function (error) { + fs.writeFile(CONFIG_FILE, JSON.stringify(config), function (error) { if (error) return next(new HttpError(500, 'unable to save settings')); next(new HttpSuccess(201, {})); @@ -50,10 +50,11 @@ function setSettings(req, res, next) { // Load the config file try { - config = require(configFile); + console.log(`Using config file: ${CONFIG_FILE}`); + config = require(CONFIG_FILE); } 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 (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${CONFIG_FILE} not found`); + else console.log(`Cannot load config file ${CONFIG_FILE}`, e); } if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; @@ -62,6 +63,15 @@ if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnab var app = express(); var router = new express.Router(); +var webdavServer = new webdav.WebDAVServer({ + requireAuthentification: true, + httpAuthentication: new webdav.HTTPBasicAuthentication(new auth.WebdavUserManager(), 'Cloudron Surfer') +}); + +webdavServer.setFileSystem('/', new webdav.PhysicalFileSystem(ROOT_FOLDER), function (success) { + console.log(`Mounting ${ROOT_FOLDER} as webdav resource`, success); +}); + var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 }); router.post ('/api/login', auth.login); @@ -77,6 +87,7 @@ router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); app.use(morgan('dev')); app.use(compression()); +app.use(webdav.extensions.express('/webdav', webdavServer)); app.use('/api', bodyParser.json()); app.use('/api', bodyParser.urlencoded({ extended: false, limit: '100mb' })); app.use('/api', cookieParser()); @@ -85,16 +96,16 @@ app.use('/api', passport.initialize()); app.use('/api', passport.session()); app.use(router); app.use('/_admin', express.static(__dirname + '/frontend')); -app.use('/', express.static(rootFolder)); +app.use('/', express.static(ROOT_FOLDER)); app.use('/', function welcomePage(req, res, next) { if (config.folderListingEnabled || 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'); + res.status(404).sendFile(__dirname + '/frontend/404.html'); }); -app.use('/', serveIndex(rootFolder, { icons: true })); +app.use('/', serveIndex(ROOT_FOLDER, { icons: true })); app.use(lastMile()); var server = app.listen(3000, function () { @@ -102,5 +113,5 @@ var server = app.listen(3000, function () { var port = server.address().port; console.log('Surfer listening on http://%s:%s', host, port); - console.log('Using base path', rootFolder); + console.log('Using base path', ROOT_FOLDER); });