-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapp.js
67 lines (51 loc) · 1.65 KB
/
app.js
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
var sockjs = require('sockjs'),
express = require("express"),
app = express(),
server = require('http').createServer(app),
fileStatusSocket = sockjs.createServer({}),
chokidar = require('chokidar'),
fs = require('fs');
var watcher = chokidar.watch('gallery', {ignored: /[\/\\]\./, persistent: true});
fileStatusSocket.on('connection', function(conn) {
['add', 'addDir', 'change', 'unlink', 'unlinkDir'].forEach(function(eventType){
watcher.on(eventType, function(path){
setTimeout(function(){
conn.write(JSON.stringify({ type : eventType, path : "/" + path}));
},500);
});
});
fs.readdir('gallery', function(err, files){
if (!err){
var initialFiles = files.filter(function(filename){
return !(filename.match(/^\./));
});
initialFiles.forEach(function(filename){
conn.write(JSON.stringify({ type : 'add', path : "/gallery/" + filename}));
});
}
});
conn.on('data', function(message) {});
conn.on('close', function() {});
});
fileStatusSocket.installHandlers(server, {prefix:'/filestatus'});
app.use(express.static(__dirname+'/client'));
app.get('/thumbs/gallery/*', function(req, res, next) {
var src_path = __dirname + '/gallery/' + req.params[0];
var dst_path = __dirname + '/thumbs/' + req.params[0];
fs.exists(dst_path, function(exists) {
if (exists) {
res.sendfile(dst_path);
} else {
require('imagemagick').crop({
srcPath: src_path,
dstPath: dst_path,
width: 200,
height: 120
}, function(err, stdout, stderr){
if (!err){res.sendfile(dst_path);}
});
}
});
});
app.use("/gallery", express.static(__dirname + '/gallery'));
server.listen(8000);