diff options
-rw-r--r-- | config.js | 8 | ||||
-rw-r--r-- | frontend.js | 48 | ||||
-rw-r--r-- | main.js | 97 | ||||
-rw-r--r-- | model_fs.js | 83 |
4 files changed, 151 insertions, 85 deletions
diff --git a/config.js b/config.js new file mode 100644 index 0000000..7d691a2 --- /dev/null +++ b/config.js @@ -0,0 +1,8 @@ +exports.modelBackend = 'fs'; +exports.Config = { + dir: 'slideshows' , + template: 'template.html', + description: 'desc', + events: 'events', + defaultSlides: 'default' +}; diff --git a/frontend.js b/frontend.js new file mode 100644 index 0000000..5b2d486 --- /dev/null +++ b/frontend.js @@ -0,0 +1,48 @@ +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) { + model = 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>'; + } + } + body = body.replace("$IMAGES", slideStr); + res.setHeader('Content-Length', body.length); + res.end(body); + }); + }); + } + ).listen(8080); +}; @@ -1,88 +1,15 @@ -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 }); +#! /usr/bin/env node -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) { +process.on('uncaughtException', function(err) { + console.error('uncaughtException: ' + (err.stack || err.message || err.toString())); +}); - 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 () { +process.chdir(__dirname); +var config = require('./config'); - 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); -}); +var model = require('./model_' + config.modelBackend); +model.start(config.Config); + +var frontend = require('./frontend'); +frontend.setModel(model); +frontend.start(config.Config); diff --git a/model_fs.js b/model_fs.js new file mode 100644 index 0000000..6f68e02 --- /dev/null +++ b/model_fs.js @@ -0,0 +1,83 @@ +var walk = require('walk'); +var fs = require('fs'); +var path = require('path'); + +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) { + 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: [] }; + }); + } + next(); +}).on("file", function (root, fileStats, next) { + + if(fileStats.name == configuration.description) { + 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; + } + 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("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]; + } + } + } + } +}); +}; + +exports.getSlides = function(slideset, cb) { + + for (var searchset in files) { + if(!files.hasOwnProperty(searchset)) { continue; } + if(files[searchset].name == slideset) { + cb(files[searchset]); + } + } +}; + + |