X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git;a=blobdiff_plain;f=server.js;fp=server.js;h=bee74c7ee5e71b500e6db60b36d20b898c94d40b;hp=dd7d7cba8c2c898b8e87dc68259ccb2a2acc820e;hb=d34561d155e99a263bf8874318a91e0161518e4a;hpb=0ac3ab8ea976f580ff68b6c003436fb536020c5c 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'), 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'), @@ -57,6 +60,49 @@ try { if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; +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(); var router = new express.Router(); @@ -95,6 +141,13 @@ 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();