diff options
author | Benjamin Kiessling <mittagessen@l.unchti.me> | 2011-10-23 14:48:51 +0200 |
---|---|---|
committer | Benjamin Kiessling <mittagessen@l.unchti.me> | 2011-10-23 14:48:51 +0200 |
commit | a8bb0fe92506728f27f5c3ee5eede4838c5425d0 (patch) | |
tree | 3ca84f5fd0776ae74c3b95d62c40b04c5712f731 /main.js |
Initial commit
Diffstat (limited to 'main.js')
-rw-r--r-- | main.js | 88 |
1 files changed, 88 insertions, 0 deletions
@@ -0,0 +1,88 @@ +var Connect = require('connect'); +var http = require('http'); +var walk = require('walk'); +var fs = require('fs'); +var url = require('url'); +var path = require('path'); +var files = []; +var walker = walk.walk('./slideshows',{ followLinks: false }); + +walker.on("directories", function (root, dirAr, next) { + var lastDir = path.join(__dirname, root).split('/'); + if((lastDir.length - __dirname.split('/').length) < 2) { + dirAr.forEach(function(dir) { + files[dir.name] = []; + }); + } + next(); +}).on("file", function (root, fileStats, next) { + + if(fileStats.name == "desc") { + next(); + } + else if(fileStats.name == "template.html") { + var lastDir = root.split('/'); + files[lastDir[lastDir.length - 1]].push({ template: fileStats.name}); + next(); + } else { + var lastDir = path.join(root).split('/'); + if(files.hasOwnProperty(lastDir[lastDir.length - 1])) { + files[lastDir[lastDir.length - 1]].push({ file: fileStats.name, head: '', text: ''}); + } + next(); + } +}).on("errors", function (root, nodeStatsArray, next) { + next(); +}).on("end", function () { + + for(var slides in files) { + if(!files.hasOwnProperty(slides)) { continue; } + var data = fs.readFileSync(__dirname+'/slideshows/'+slides+'/desc'); + var descs = data.toString().split('\n'); + for(var file in slides) { + if(!files[slides].hasOwnProperty(file)) { continue; } + if(!files[slides][file].hasOwnProperty('file')) { continue; } + for(var desc in descs) { + if(!descs.hasOwnProperty(desc)) { continue; } + var info = descs[desc].split('|'); + if(info[0] === files[slides][file]['file']) { + files[slides][file]['head'] = info[1]; + files[slides][file]['text'] = info[2]; + } + } + } + } + Connect.createServer( + Connect.logger(), // Log responses to the terminal using Common Log Format. + Connect.responseTime(), // Add a special header with timing information. + Connect.staticCache(), + Connect.static( __dirname + '/slideshows'), // Serve all static files in the current dir. + function(req, res) { + var purl = url.parse(req.url, true); + var slideset = 'default'; + if(purl['query'].hasOwnProperty('slides')) { + slideset = purl['query']['slides']; + } + if(!files.hasOwnProperty(slideset)) { res.end(); return; } + + fs.readFile('./slideshows/'+slideset+'/template.html', function(err,data){ + if(err) { + res.end(body); + } + var body = data.toString(); + var slides = ''; + for(var file in files[slideset]) { + if (!files[slideset].hasOwnProperty(file)) { continue; } + if (!files[slideset][file].hasOwnProperty('file')) { continue; } + slides = slides+'<li><img src="/'+slideset+'/'+files[slideset][file]['file']+'">' + if(files[slideset][file]['text']) { + slides = slides+'<div class="transover"><h2>'+files[slideset][file]['head']+' - '+files[slideset][file]['text']+'</div></img></li>'; + } + } + body = body.replace("$IMAGES", slides); + res.setHeader('Content-Length', body.length); + res.end(body); + }); + } + ).listen(8080); +}); |