diff options
author | Benjamin Kiessling <mittagessen@l.unchti.me> | 2011-11-17 23:52:43 +0100 |
---|---|---|
committer | Benjamin Kiessling <mittagessen@l.unchti.me> | 2011-11-17 23:52:43 +0100 |
commit | be0c922fccdaba96cb7dab391f03208745dc01d9 (patch) | |
tree | 38b3c41899f2572dc4efbe078c76e5e8077d1225 | |
parent | 3fd29331bf3342166f8d9fcb9173c7f40bc91b97 (diff) |
Refactor code, update files dynamically, reading event file
-rw-r--r-- | config.js | 4 | ||||
-rw-r--r-- | frontend.js | 80 | ||||
-rw-r--r-- | main.js | 11 | ||||
-rw-r--r-- | model_fs.js | 121 |
4 files changed, 133 insertions, 83 deletions
@@ -2,7 +2,7 @@ exports.modelBackend = 'fs'; exports.Config = { dir: 'slideshows' , template: 'template.html', - description: 'desc', - events: 'events', + description: 'desc.js', + events: 'events.js', defaultSlides: 'default' }; diff --git a/frontend.js b/frontend.js index 5b2d486..0dba5b4 100644 --- a/frontend.js +++ b/frontend.js @@ -1,10 +1,6 @@ -var Connect = require('connect'); -var http = require('http'); -var url = require('url'); var path = require('path'); var fs = require('fs'); -var configuration; var model; exports.setModel = function(m) { @@ -12,37 +8,51 @@ exports.setModel = function(m) { }; exports.start = function(config) { - configuration = config; - 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(path.join(__dirname, configuration.dir)), - function(req, res) { - var purl = url.parse(req.url, true); - var slideset = 'default'; - if(purl['query'].hasOwnProperty('slides')) { - slideset = purl['query']['slides']; - } - model.getSlides(slideset, function(slides) { - fs.readFile(path.join(__dirname, configuration.dir, slides['template']) , function(err,data) { - if(err) { res.end(body); } - var body = data.toString(); - var slideStr = ''; - for(var slide in slides['slides']) { - if(!slides['slides'].hasOwnProperty(slide)) { continue; } - if(slides['slides'][slide]['type'] === 'image') { - slideStr = slideStr +'<li><img src="/'+slides['slides'][slide]['file']+'">'; - } - if(slides['slides'][slide]['text'].length > 0) { - slideStr = slideStr+'<div class="transover"><h2>'+slides['slides'][slide]['head']+' - '+slides['slides'][slide]['text']+'</h2></div></img></li>'; - } + config.app.get('/slides/:id', function(req, res) { + var slideset; + slideset = req.params.id; + model.getSlides(slideset, function(err, slides) { + if(err) { throw err; } + fs.readFile(path.join(__dirname, config.dir, slides['template']) , function(err,data) { + if(err) { res.end(body); } + var body = data.toString(); + var slideStr = ''; + for(var slide in slides['slides']) { + if(!slides['slides'].hasOwnProperty(slide)) { continue; } + if(slides['slides'][slide]['type'] === 'image') { + slideStr = slideStr +'<li><img src="/'+slides['slides'][slide]['file']+'">'; + } + if(slides['slides'][slide]['text'].length > 0) { + slideStr = slideStr+'<div class="transover"><h2>'+slides['slides'][slide]['head']+' - '+slides['slides'][slide]['text']+'</h2></div></img></li>'; } - body = body.replace("$IMAGES", slideStr); - res.setHeader('Content-Length', body.length); - res.end(body); - }); + } + var eventStr = ''; + for(var date in slides['events']) { + if(!slides['events'].hasOwnProperty(date)) { continue; } + eventStr = eventStr + '<h4>'+slides['events'][date]['date']+'</h4><hr><ul>'; + for(var ev in slides['events'][date]['events']) { + if(!slides['events'][date]['events'].hasOwnProperty(ev)) { continue; } + eventStr = eventStr + '<li>'+slides['events'][date]['events'][ev]['time']+' - '+slides['events'][date]['events'][ev]['text']+'</li>'; + } + eventStr = eventStr + '</ul>'; + } + body = body.replace("$IMAGES", slideStr); + body = body.replace("$EVENTS", eventStr); + res.setHeader('Content-Length', body.length); + res.end(body); }); - } - ).listen(8080); + }); + }); + config.app.get('*', function(req, res){ + model.getAllSlides(function(slidesets) { + var body = '<h3>Slideshows:</h3>'; + for(var slideset in slidesets) { + if(!slidesets.hasOwnProperty(slideset)) { continue; } + body = body + '<a href="/slides/' + slidesets[slideset] + '">'+slidesets[slideset]+'</a><br/>'; + } + res.setHeader('Content-Length', body.length); + res.end(body); + }); + }); + config.app.listen(8080); }; @@ -1,5 +1,7 @@ #! /usr/bin/env node +var path = require('path'); + process.on('uncaughtException', function(err) { console.error('uncaughtException: ' + (err.stack || err.message || err.toString())); }); @@ -7,6 +9,15 @@ process.on('uncaughtException', function(err) { process.chdir(__dirname); var config = require('./config'); +var express = require('express'); +var app = express.createServer(); + +app.use(express.logger()); +app.use(express.staticCache()); +app.use(express.static(path.join(__dirname, config.Config.dir))); + +config.Config.app = app; + var model = require('./model_' + config.modelBackend); model.start(config.Config); diff --git a/model_fs.js b/model_fs.js index 6f68e02..c8d04e2 100644 --- a/model_fs.js +++ b/model_fs.js @@ -1,83 +1,112 @@ var walk = require('walk'); var fs = require('fs'); var path = require('path'); +var walker; +var watcher; var configuration; var files = []; exports.start = function(config) { configuration = config; - var walker = walk.walk(configuration.dir, { followLinks: false }); -walker.on("directories", function (root, dirAr, next) { + watcher = require('watch-tree').watchTree(configuration.dir, {'sample-rate': 5}); + watcher.on('fileDeleted', exports.rebuildFiles); + watcher.on('fileCreated', exports.rebuildFiles); + watcher.on('fileModified', exports.rebuildFiles); + exports.rebuildFiles(); + +}; + +exports.rebuildFiles = function() { + + walker = walk.walk(configuration.dir, { 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] = { name: dir.name, template: path.join(configuration.defaultSlides, - configuration.template) , slides: [] }; + configuration.template) , slides: []}; }); } next(); -}).on("file", function (root, fileStats, next) { - - if(fileStats.name == configuration.description) { + }).on("file", function(root, fileStats, next) { + if(fileStats.name == configuration.description || fileStats.name == configuration.events) { next(); - } - else if(fileStats.name == configuration.template) { - var lastDir = root.split('/'); - files[lastDir[lastDir.length - 1]]['template'] = path.join(lastDir[lastDir.length - 1], fileStats.name); - next(); - } else { - var lastDir = path.join(root).split('/'); - if(files.hasOwnProperty(lastDir[lastDir.length - 1])) { - var ext = path.extname(fileStats.name); - var type = 'image'; - switch (ext) { - case '.mp4': - case '.avi': - case '.ogv': - case '.mkv': - type = 'video'; - break; + } + else if(fileStats.name == configuration.template) { + var lastDir = root.split('/'); + files[lastDir[lastDir.length - 1]]['template'] = path.join(lastDir[lastDir.length - 1], fileStats.name); + next(); + } else { + var lastDir = path.join(root).split('/'); + if(files.hasOwnProperty(lastDir[lastDir.length - 1])) { + var ext = path.extname(fileStats); + var type = 'image'; + switch (ext) { + case '.mp4': + case '.avi': + case '.ogv': + case '.mkv': + type = 'video'; + break; + } + files[lastDir[lastDir.length - 1]]['slides'].push({ type: type, file: path.join(lastDir[lastDir.length - 1], fileStats.name), head: '', text: ''}); } - files[lastDir[lastDir.length - 1]]['slides'].push({ type: type, file: path.join(lastDir[lastDir.length - 1], fileStats.name), head: '', text: ''}); + next(); } + }).on("errors", function(root, nodeStatsArray, next) { next(); - } -}).on("errors", function (root, nodeStatsArray, next) { - next(); -}).on("end", function () { - - for(var slideset in files) { - if(!files.hasOwnProperty(slideset)) { continue; } - var data = fs.readFileSync(__dirname+'/'+configuration.dir+'/'+slideset+'/desc'); - var descs = data.toString().split('\n'); - for(var file in files[slideset]['slides']) { - if(!files[slideset]['slides'].hasOwnProperty(file)) { continue; } - if(!files[slideset]['slides'][file].hasOwnProperty('file')) { continue; } - for(var desc in descs) { - if(!descs.hasOwnProperty(desc)) { continue; } - var info = descs[desc].split('|'); - var comp = files[slideset]['slides'][file]['file'].split('/'); - if(info[0] === comp[comp.length - 1]) { - files[slideset]['slides'][file]['head'] = info[1]; - files[slideset]['slides'][file]['text'] = info[2]; + }).on("end", function() { + for(var slideset in files) { + if(!files.hasOwnProperty(slideset)) { continue; } + try { + var data = fs.readFileSync(path.join(__dirname, configuration.dir, slideset, configuration.description)); + } catch(e) { + console.log(e.toString()); + } + var descs = JSON.parse(data.toString()); + for(var file in files[slideset]['slides']) { + if(!files[slideset]['slides'].hasOwnProperty(file)) { continue; } + if(!files[slideset]['slides'][file].hasOwnProperty('file')) { continue; } + for(var desc in descs) { + if(!descs.hasOwnProperty(desc)) { continue; } + var comp = files[slideset]['slides'][file]['file'].split('/'); + if(descs[desc]['file'] == comp[comp.length - 1]) { + files[slideset]['slides'][file]['head'] = descs[desc]['head']; + files[slideset]['slides'][file]['text'] = descs[desc]['text']; + } } } + try { + data = fs.readFileSync(path.join(__dirname, configuration.dir, 'default', configuration.events)); + } catch(e) { + console.log(e.toString()); + } + files[slideset]['events'] = JSON.parse(data.toString()); } - } -}); + }); }; + exports.getSlides = function(slideset, cb) { for (var searchset in files) { if(!files.hasOwnProperty(searchset)) { continue; } if(files[searchset].name == slideset) { - cb(files[searchset]); + cb(null, files[searchset]); } } }; +exports.getAllSlides = function(cb) { + var slidesets = []; + for(var slideset in files) { + if(!files.hasOwnProperty(slideset)) { continue; } + slidesets.push(files[slideset].name); + } + cb(slidesets); +}; |