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; 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: []}; }); } next(); }).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); 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; } 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(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); };