forked from TheDudeWithTheThing/livephotoalbum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
207 lines (175 loc) · 6.11 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var conf = require('./conf')
, express = require('express')
, mongoose = require('mongoose')
, mongooseAuth = require('mongoose-auth')
, mongoStore = require('./lib/mongostore')
, userSchema = require('./model/user')
, pictureSchema = require('./model/picture')
, currentUserSchema = require('./model/current_user')
, fs = require('fs')
, path = require('path')
, dateformat = require('dateformat')
, form = require('connect-form');
var app = express.createServer(
form({ keepExtensions: true })
, express.cookieParser()
, express.session({ secret: 'itsasecrettoeverybody', cookie : { maxAge: 14400000 }, store: mongoStore})
, mongooseAuth.middleware()
, express['static']( __dirname + "/public")
, express.logger())
, io = require('socket.io').listen(app)
, db = mongoose.createConnection('mongodb://'+conf.db.host + '/' + conf.db.schema)
, userFactory = db.model('User', userSchema)
, pictureFactory = db.model('Picture', pictureSchema)
, currentUserFactory = db.model('CurrentUser', currentUserSchema);
app.configure(function() {
app.set('view engine', 'jade');
app.set('views', __dirname+'/views');
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.get('/', function(req, res) {
pictureFactory.find({}).sort('createdAt', -1).limit(10).run(function(err, images) {
if (err) console.log(err);
currentUserFactory.find({}).run(function(err, users) {
if (err) console.log(err);
res.render('index', {title : 'Home Page', images: images, conf: conf, users: users });
});
});
});
app.get('/login', function(req, res) {
res.render('login', {title: 'Login'});
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.post('/add_image', function(req, res) {
req.form.complete(function(err, fields, files) {
if (err) {
next(err);
} else {
if (files.upload) {
var filename = files.upload.name
, url_path = '/img/uploads/' + filename
, src_path = files.upload.path
, target_path = __dirname + '/public/img/uploads/' + filename
, newPic = new pictureFactory({src: url_path});
fs.renameSync(src_path, target_path);
newPic.save(function(err) {
if(err) { console.log(err); }
res.redirect('/');
});
}
else {
var imgURL = fields.url;
}
}
});
// get and save image to fs
// save to db
// trigger socket message with image info
});
var parseCookie = require('connect').utils.parseCookie;
io.configure('production', function(){
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
io.sockets.on('connection', function(socket) {
var session = socket.handshake.session
, user = socket.handshake.user;
// someone connected, need to add to list
if (session.auth && session.auth.loggedIn) {
currentUserFactory.find({user_id: user._id}).run( function(err, foundUser) {
if(!foundUser.length) {
var currentUser = new currentUserFactory({name: user.display_name, user_id: user._id});
currentUser.save( function(err) {
if(err) console.log(err);
socket.broadcast.emit('connect_rcv', {id: 'u' + user._id, name: user.display_name});
socket.emit('connect_rcv', {id: 'u' + user._id, name: user.display_name});
});
}
});
}
socket.on('disconnect', function() {
if (session.auth && session.auth.loggedIn) {
currentUserFactory.remove({user_id: user._id}, function(err) {
if(err) console.log(err);
socket.broadcast.emit('disconnect_rcv', {id: 'u' + user._id, name: user.display_name});
});
}
});
socket.on('get_pic', function(data) {
if (!data) {
socket.emit('no_data');
return;
}
if (session.auth && session.auth.loggedIn) {
var d = {src: data, owner: user.display_name}
, newPic = new pictureFactory(d);
newPic.save(function(err) {
if(err) { console.log(err); }
});
socket.broadcast.emit('new_pic', d);
socket.emit('new_pic', d);
socket.broadcast.emit('chat_send', {user: user.display_name, msg: 'just uploaded photo ' + data});
socket.emit('chat_send', {user: user.display_name, msg: 'just uploaded photo ' + data});
}
else {
socket.emit('not_logged_in');
}
});
socket.on('chat_rcv', function(data) {
if (data !== '') {
if(session.auth && session.auth.loggedIn) {
socket.broadcast.emit('chat_send', {user: user.display_name, msg: data});
socket.emit('chat_send', {user: user.display_name, msg: data});
}
else {
socket.emit('not_logged_in');
}
}
else {
socket.emit('no_data');
}
});
});
// grabbed from http://www.danielbaulig.de/socket-ioexpress/
io.set('authorization', function (data, accept) {
if(data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionId = data.cookie['connect.sid'];
mongoStore.get(data.sessionId, function(err, session) {
if(err || !session) {
console.log('### session not valid');
accept('Not Authenticated Error', false);
} else {
data.session = session;
for (var u in session.auth) {
var userId = session.auth.userId;
}
userFactory.findById(userId, function(err, user) {
if (err) {
console.log('###');
console.log(err);
accept('Not Authenticated Error', false);
}
data.user = user;
accept(null, true);
});
}
});
}
});
mongooseAuth.helpExpress(app);
console.log("running");
app.listen(conf.port);