summaryrefslogtreecommitdiff
path: root/model_fs.js
diff options
context:
space:
mode:
Diffstat (limited to 'model_fs.js')
-rw-r--r--model_fs.js98
1 files changed, 67 insertions, 31 deletions
diff --git a/model_fs.js b/model_fs.js
index 353d107..8a4aa13 100644
--- a/model_fs.js
+++ b/model_fs.js
@@ -1,6 +1,8 @@
var walk = require('walk');
var fs = require('fs');
var path = require('path');
+var ical = require('ical');
+var forEachAsync = require('forEachAsync');
var walker;
var watcher;
@@ -22,11 +24,12 @@ exports.rebuildFiles = function() {
walker = walk.walk(configuration.dir, { followLinks: false });
+ var newSlides = {};
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:
+ newSlides[dir.name] = { name: dir.name, template:
path.join(configuration.defaultSlides,
configuration.template) , slides: []};
});
@@ -38,11 +41,11 @@ exports.rebuildFiles = function() {
}
else if(fileStats.name == configuration.template) {
var lastDir = root.split('/');
- files[lastDir[lastDir.length - 1]]['template'] = path.join(lastDir[lastDir.length - 1], fileStats.name);
+ newSlides[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])) {
+ if(newSlides.hasOwnProperty(lastDir[lastDir.length - 1])) {
var ext = path.extname(fileStats);
var type = 'image';
switch (ext) {
@@ -53,46 +56,79 @@ exports.rebuildFiles = function() {
type = 'video';
break;
}
- files[lastDir[lastDir.length - 1]]['slides'].push({ type: type, file: path.join(lastDir[lastDir.length - 1], fileStats.name), head: '', text: ''});
+ newSlides[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'];
+ for(var slideset in newSlides) {
+ var collectDescs = function(slideset) {
+ fs.readFile(path.join(__dirname, configuration.dir, slideset, configuration.description), function(err, data) {
+ if(err) { throw err; }
+ var descs = JSON.parse(data.toString());
+ for(var file in newSlides[slideset]['slides']) {
+ if(!newSlides[slideset]['slides'].hasOwnProperty(file)) { continue; }
+ if(!newSlides[slideset]['slides'][file].hasOwnProperty('file')) { continue; }
+ for(var desc in descs) {
+ if(!descs.hasOwnProperty(desc)) { continue; }
+ var comp = newSlides[slideset]['slides'][file]['file'].split('/');
+ if(descs[desc]['file'] == comp[comp.length - 1]) {
+ newSlides[slideset]['slides'][file]['head'] = descs[desc]['head'];
+ newSlides[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());
- }
- var events = JSON.parse(data.toString());
- if(events['type'] == 'text') {
- files[slideset]['events'] = events['events'];
+ exports.addSlides(slideset, newSlides[slideset], function(err) { if(err) { throw err; }});
+ fs.readFile(path.join(__dirname, configuration.dir, 'default', configuration.events), function(err, data) {
+ var events = JSON.parse(data.toString());
+ if(events['type'] == 'text') {
+ files[slideset]['events'] = events['events'];
+ } else if(events['type'] == 'ical') {
+ var compEv = [];
+ ical.fromURL(events['url'], {}, function(err, data) {
+ for (var k in data) {
+ if (data.hasOwnProperty(k)) {
+ var ev = data[k];
+ if(ev.start && ev.summary) {
+ compEv.push({
+ date:
+ ev.start,
+ text:
+ ev.summary
+ });
+ }
+ }
+ }
+ });
+ }
+ });
+ });
}
+ collectDescs(slideset);
}
});
};
+exports.setEvents = function(slideset, events, cb) {
+
+ var found = 0;
+ for (var searchset in files) {
+ if(!files.hasOwnProperty(searchset)) { continue; }
+ if(files[searchset].name == slideset) {
+ files[searchset]['events'] = events;
+ found = 1;
+ }
+ }
+ if(found) { cb('Couldn\'t find slideset'); }
+ else { cb(null); }
+};
+
+exports.addSlides = function(slideName, newSlides, cb) {
+ files[slideName] = newSlides;
+ cb(null);
+};
exports.getSlides = function(slideset, cb) {