diff options
Diffstat (limited to 'server.js')
-rwxr-xr-x | server.js | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -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 | ||
58 | if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; | 61 | if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; |
59 | 62 | ||
63 | function isRoot(p) { | ||
64 | return path.join(ROOT_FOLDER, p) === path.join(ROOT_FOLDER, '/'); | ||
65 | } | ||
66 | |||
67 | function 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 | |||
90 | function 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 |
61 | var app = express(); | 107 | var app = express(); |
62 | var router = new express.Router(); | 108 | var router = new express.Router(); |
@@ -95,6 +141,13 @@ app.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitiali | |||
95 | app.use(router); | 141 | app.use(router); |
96 | app.use(webdav.extensions.express('/_webdav', webdavServer)); | 142 | app.use(webdav.extensions.express('/_webdav', webdavServer)); |
97 | app.use('/_admin', express.static(__dirname + '/frontend')); | 143 | app.use('/_admin', express.static(__dirname + '/frontend')); |
144 | app.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 | }); | ||
149 | app.use('/zip', sendArchive("zip")); | ||
150 | app.use('/tar', sendArchive("tar")); | ||
98 | app.use('/', express.static(ROOT_FOLDER)); | 151 | app.use('/', express.static(ROOT_FOLDER)); |
99 | app.use('/', function welcomePage(req, res, next) { | 152 | app.use('/', function welcomePage(req, res, next) { |
100 | if (config.folderListingEnabled || req.path !== '/') return next(); | 153 | if (config.folderListingEnabled || req.path !== '/') return next(); |