X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git;a=blobdiff_plain;f=server.js;h=e79dad29e444f652477bba8fe20af6e94afac793;hp=4c9df2d44b2792666bb46c1d96feea407201d780;hb=HEAD;hpb=c2c00fca7dccb6e512a0f01bc87db129538765ef diff --git a/server.js b/server.js index 4c9df2d..e79dad2 100755 --- a/server.js +++ b/server.js @@ -6,8 +6,11 @@ var express = require('express'), morgan = require('morgan'), path = require('path'), fs = require('fs'), + archiver = require('archiver'), compression = require('compression'), session = require('express-session'), + serveIndex = require('serve-index'), + escapeHtml = require('escape-html'), bodyParser = require('body-parser'), cookieParser = require('cookie-parser'), lastMile = require('connect-lastmile'), @@ -16,7 +19,6 @@ var express = require('express'), multipart = require('./src/multipart'), 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')); @@ -36,6 +38,7 @@ function getSettings(req, res, next) { } function setSettings(req, res, next) { + return next(new HttpError(400, 'not editable')); if (typeof req.body.folderListingEnabled === 'undefined') return next(new HttpError(400, 'missing folderListingEnabled boolean')); config.folderListingEnabled = !!req.body.folderListingEnabled; @@ -49,14 +52,57 @@ function setSettings(req, res, next) { // Load the config file try { - console.log(`Using config file: ${CONFIG_FILE}`); + console.log(`Using config file at: ${CONFIG_FILE}`); config = require(CONFIG_FILE); } catch (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; +if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = false; + +function isRoot(p) { + return path.join(ROOT_FOLDER, p) === path.join(ROOT_FOLDER, '/'); +} + +function sendArchive(format) { + var mime, extension; + if (format === "zip") { + mime = "application/zip"; + extension = "zip"; + } else { + mime = "application/tar+gzip"; + extension = "tar.gz"; + } + return function(req, res, next) { + if (isRoot(req.path) || !fs.existsSync(path.join(ROOT_FOLDER, req.path))) + return res.status(404).sendFile(__dirname + '/frontend/404.html'); + res.writeHead(200, { + 'Content-Type': mime, + 'Content-disposition': 'attachment; filename=' + path.basename(req.path) + '.' + extension + }); + var archive = archiver(format); + archive.pipe(res); + archive.directory(path.join(ROOT_FOLDER, req.path), path.basename(req.path)) + archive.finalize(); + } +} + +function rawTemplate(locals, callback) { + var html = 'wget/curl friendly directory listing of '; + html += locals.directory; + html += ''; + callback(null, html); +}; // Setup the express server and routes var app = express(); @@ -68,7 +114,7 @@ var webdavServer = new webdav.WebDAVServer({ }); webdavServer.setFileSystem('/', new webdav.PhysicalFileSystem(ROOT_FOLDER), function (success) { - console.log(`Mounting ${ROOT_FOLDER} as webdav resource`, success); + if (success) console.log(`Mounting webdav resource from: ${ROOT_FOLDER}`); }); var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 }); @@ -81,12 +127,12 @@ router.get ('/api/tokens', auth.verify, auth.getTokens); router.post ('/api/tokens', auth.verify, auth.createToken); router.delete('/api/tokens/:token', auth.verify, auth.delToken); router.get ('/api/profile', auth.verify, auth.getProfile); -router.get ('/api/files/*', auth.verify, files.get); +router.get ('/api/files/*', auth.verifyIfNeeded, files.get); router.post ('/api/files/*', auth.verify, multipart, files.post); 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(); }); +app.use('/api/healthcheck', function (req, res) { res.status(200).send(); }); app.use(morgan('dev')); app.use(compression()); app.use('/api', bodyParser.json()); @@ -96,22 +142,32 @@ app.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitiali app.use(router); app.use(webdav.extensions.express('/_webdav', webdavServer)); app.use('/_admin', express.static(__dirname + '/frontend')); +app.use('/raw', function serveRaw(req, res, next) { + if (isRoot(req.path) || !fs.existsSync(path.join(ROOT_FOLDER, req.path))) + return res.status(404).sendFile(__dirname + '/frontend/404.html'); + serveIndex(ROOT_FOLDER, { template: rawTemplate })(req, res, next); +}); +app.use('/zip', sendArchive("zip")); +app.use('/tar', sendArchive("tar")); app.use('/', express.static(ROOT_FOLDER)); app.use('/', function welcomePage(req, res, next) { - if (config.folderListingEnabled || req.path !== '/') return next(); + if (config.folderListingEnabled || !isRoot(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.status(404).sendFile(__dirname + '/frontend/404.html'); +app.use('/', function (req, res) { + if (!fs.existsSync(path.join(ROOT_FOLDER, req.path))) return res.status(404).sendFile(__dirname + '/frontend/404.html'); + + res.status(200).sendFile(__dirname + '/frontend/public.html'); }); -app.use('/', serveIndex(ROOT_FOLDER, { icons: true })); app.use(lastMile()); -var server = app.listen(3000, function () { +var server = app.listen(process.env.LISTEN, function () { var host = server.address().address; var port = server.address().port; - console.log('Surfer listening on http://%s:%s', host, port); - console.log('Using base path', ROOT_FOLDER); + console.log(`Base path: ${ROOT_FOLDER}`); + console.log(); + console.log(`Listening on http://${host}:${port}`); + + auth.init(config); });