+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);
+};
+