]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - server.js
Make listen port and ldap filter more flexible
[perso/Immae/Projets/Nodejs/Surfer.git] / server.js
index dd7d7cba8c2c898b8e87dc68259ccb2a2acc820e..e79dad29e444f652477bba8fe20af6e94afac793 100755 (executable)
--- 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'),
@@ -35,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;
@@ -55,7 +59,50 @@ try {
     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 = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>wget/curl friendly directory listing of ';
+  html += locals.directory;
+  html += '</title></head><body><ul>\n';
+  html += locals.fileList.map(function (file) {
+    if (file.name === "..") { return ""; }
+    var isDir = file.stat && file.stat.isDirectory();
+    var fpath = locals.directory.split('/').map(function (c) { return encodeURIComponent(c); });
+    if (!isDir) { fpath.shift(); fpath[0] = ""; }
+    fpath.push(encodeURIComponent(file.name));
+    return '<li><a href="' + escapeHtml(path.normalize(fpath.join('/'))) + '">' + escapeHtml(file.name) + '</a></li>';
+  }).join("\n");
+  html += '\n</ul></body></html>';
+  callback(null, html);
+};
 
 // Setup the express server and routes
 var app = express();
@@ -95,21 +142,26 @@ 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) {
-    if (!config.folderListingEnabled) return res.status(404).sendFile(__dirname + '/frontend/404.html');
-
     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(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;