summaryrefslogtreecommitdiff
path: root/model_fs.js
blob: 036689ca5008af7a3f8a7767352d80437dfa12b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var walk = require('walk');
var fs = require('fs');
var path = require('path');
var ical = require('ical');
var forEachAsync = require('forEachAsync');
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 });

	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) {
			newSlides[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('/');
			newSlides[lastDir[lastDir.length - 1]]['template'] = path.join(lastDir[lastDir.length - 1], fileStats.name);
			next();
		} else {
			var lastDir = path.join(root).split('/');
			if(newSlides.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;
				}
				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 newSlides) {
			/* collects descriptions and adds them to the slideset */
			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'];
							}
						}
					}
					setSlides(slideset, newSlides[slideset], function(err) { if(err) { throw err; }});
				});
			}
			/* collect events and adds them to the slideset */
			var collectEvents = function(slideset) {
				fs.readFile(path.join(__dirname, configuration.dir, slideset, configuration.events), function(err, data) {
					if(err) { throw err; }
					try {
						var events = JSON.parse(data.toString());
					} catch(e) {
						throw err;
					}
					if(events['type'] == 'text') {
						setEvents(slideset, events['events'], function(err) {
							if(err) { throw err; }
						});
					} else if(events['type'] == 'ical') {
						ical.fromURL(events['url'], {}, function(err, bloat) {
							if(err) { throw err; }
							var bDate = new Date();
							var fDate = new Date();
							bDate.setMinutes(bDate.getMinutes() - events['before']);
							fDate.setMinutes(bDate.getMinutes() + events['after']);
							var data = ical.events_between(bDate, fDate);
							var compEv = [];
							for (var k in data) {
								if (!data.hasOwnProperty(k)) { continue; }
								var ev = data[k];
								ev.time = ev.start.toLocaleTimeString();
								if(ev.start && ev.summary) {
									if(typeof compEv[ev.start.toLocaleDateString()] == "undefined") {
										compEv[ev.start.toLocaleDateString()] = [];
									}
									compEv[ev.start.toLocaleDateString()].push(ev);
								}
							}
							setEvents(slideset, compEv, function(err) {
								if(err) { throw err; }
							});
						});
					}
				});
			}
			collectDescs(slideset);
			collectEvents(slideset);
		}
	});
};

var setEvents = function(slideset, events, cb) {

	for (var searchset in files) {
		if(!files.hasOwnProperty(searchset)) { continue; }
		if(files[searchset].name == slideset) {
			files[searchset]['events'] = events;
			cb(null);
			return;
		}
	}
	cb('Couldn\'t find slideset');
};

var setSlides = function(slideName, newSlides, cb) {
	files[slideName] = newSlides;
	cb(null);
};

exports.getSlides = function(slideset, cb) {
	
	for (var searchset in files) {
		if(!files.hasOwnProperty(searchset)) { continue; }
		if(files[searchset].name == slideset) {
			cb(null, files[searchset]);
			return;
		}
	}
	cb('Couldn\'t find slideset');
};

exports.getAllSlides = function(cb) {

	var slidesets = [];
	for(var slideset in files) {
		if(!files.hasOwnProperty(slideset)) { continue; }
		slidesets.push(files[slideset].name);
	}
	cb(slidesets);
};