aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server.js')
-rwxr-xr-xserver.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/server.js b/server.js
index dd7d7cb..bee74c7 100755
--- a/server.js
+++ b/server.js
@@ -6,8 +6,11 @@ var express = require('express'),
6 morgan = require('morgan'), 6 morgan = require('morgan'),
7 path = require('path'), 7 path = require('path'),
8 fs = require('fs'), 8 fs = require('fs'),
9 archiver = require('archiver'),
9 compression = require('compression'), 10 compression = require('compression'),
10 session = require('express-session'), 11 session = require('express-session'),
12 serveIndex = require('serve-index'),
13 escapeHtml = require('escape-html'),
11 bodyParser = require('body-parser'), 14 bodyParser = require('body-parser'),
12 cookieParser = require('cookie-parser'), 15 cookieParser = require('cookie-parser'),
13 lastMile = require('connect-lastmile'), 16 lastMile = require('connect-lastmile'),
@@ -57,6 +60,49 @@ try {
57 60
58if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; 61if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true;
59 62
63function isRoot(p) {
64 return path.join(ROOT_FOLDER, p) === path.join(ROOT_FOLDER, '/');
65}
66
67function sendArchive(format) {
68 var mime, extension;
69 if (format === "zip") {
70 mime = "application/zip";
71 extension = "zip";
72 } else {
73 mime = "application/tar+gzip";
74 extension = "tar.gz";
75 }
76 return function(req, res, next) {
77 if (isRoot(req.path) || !fs.existsSync(path.join(ROOT_FOLDER, req.path)))
78 return res.status(404).sendFile(__dirname + '/frontend/404.html');
79 res.writeHead(200, {
80 'Content-Type': mime,
81 'Content-disposition': 'attachment; filename=' + path.basename(req.path) + '.' + extension
82 });
83 var archive = archiver(format);
84 archive.pipe(res);
85 archive.directory(path.join(ROOT_FOLDER, req.path), path.basename(req.path))
86 archive.finalize();
87 }
88}
89
90function rawTemplate(locals, callback) {
91 var html = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>wget/curl friendly directory listing of ';
92 html += locals.directory;
93 html += '</title></head><body><ul>\n';
94 html += locals.fileList.map(function (file) {
95 if (file.name === "..") { return ""; }
96 var isDir = file.stat && file.stat.isDirectory();
97 var fpath = locals.directory.split('/').map(function (c) { return encodeURIComponent(c); });
98 if (!isDir) { fpath.shift(); fpath[0] = ""; }
99 fpath.push(encodeURIComponent(file.name));
100 return '<li><a href="' + escapeHtml(path.normalize(fpath.join('/'))) + '">' + escapeHtml(file.name) + '</a></li>';
101 }).join("\n");
102 html += '\n</ul></body></html>';
103 callback(null, html);
104};
105
60// Setup the express server and routes 106// Setup the express server and routes
61var app = express(); 107var app = express();
62var router = new express.Router(); 108var router = new express.Router();
@@ -95,6 +141,13 @@ app.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitiali
95app.use(router); 141app.use(router);
96app.use(webdav.extensions.express('/_webdav', webdavServer)); 142app.use(webdav.extensions.express('/_webdav', webdavServer));
97app.use('/_admin', express.static(__dirname + '/frontend')); 143app.use('/_admin', express.static(__dirname + '/frontend'));
144app.use('/raw', function serveRaw(req, res, next) {
145 if (isRoot(req.path) || !fs.existsSync(path.join(ROOT_FOLDER, req.path)))
146 return res.status(404).sendFile(__dirname + '/frontend/404.html');
147 serveIndex(ROOT_FOLDER, { template: rawTemplate })(req, res, next);
148});
149app.use('/zip', sendArchive("zip"));
150app.use('/tar', sendArchive("tar"));
98app.use('/', express.static(ROOT_FOLDER)); 151app.use('/', express.static(ROOT_FOLDER));
99app.use('/', function welcomePage(req, res, next) { 152app.use('/', function welcomePage(req, res, next) {
100 if (config.folderListingEnabled || req.path !== '/') return next(); 153 if (config.folderListingEnabled || req.path !== '/') return next();