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
|
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 });
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) {
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 () {
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);
});
|