-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
690 lines (609 loc) · 18.6 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/**********************************************
utility functions
***********************************************/
// returns true if an array contains the specified element
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
}
/**********************************************
module dependencies
***********************************************/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, fs = require('fs')
, bcrypt = require("bcrypt-nodejs")
, sessionStore = require('connect-mongo')(express)
, mongoose = require('mongoose')
, async = require('async');
// category names (used for comparison when handling a request to track a category)
var categories = [
'funny',
'sports',
'nature',
'people',
'art',
'misc'
];
/**********************************************
database set up
***********************************************/
// mongoose model classes
var Pic;
var User;
// connect to the database (db must be started manually)
mongoose.connect('mongodb://localhost/users');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
// once the DB connection is open...
db.once('open', function callback() {
// create mongoose schemas and instantiate them into models
// a picture (stored in /public/uploads)
var PicSchema = mongoose.Schema({
filename: String,
path: String,
srcUser: String,
categories: [String],
caption: String
});
Pic = mongoose.model("Pic", PicSchema);
// a user
var UserSchema = mongoose.Schema({
username: String,
password: String,
favCategories: [String],
favUsers: [String]
});
User = mongoose.model("User", UserSchema);
});
/**********************************************
configure the application
***********************************************/
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({
uploadDir: './public/uploads/',
keepExtensions: true
}));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
cookie: {maxAge: 60000 * 60},
secret: 'quo vadis',
store: new sessionStore({
db: "sessions"
})
}));
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
/**********************************************
route/handle GET requests
***********************************************/
// index page, or if logged in, home page
app.get('/', function(req, res){
if(req.session.username){
res.redirect("/home");
} else {
routes.index(req, res);
}
});
// registration form
app.get('/register', routes.register);
// redirect to this page if error
app.get('/error', routes.error);
// user's home page; shows most recent uploads for tracked categories/users
app.get('/home', function(req, res){
var lastUpload;
var trackedCats = [];
var trackedUsers = [];
var recentPics = {};
var userPics = {};
// helper function to retrieve recent picture from category
var getPicsInCategory = function(cat, callback) {
Pic.find({categories: { $in: [cat.name] } }, function(err, pics){
if(err) callback(err);
else {
cat.pics = pics;
callback();
}
});
};
// helper function to retrieve recent picture from user
var getPicsFromUser = function(user, callback) {
Pic.find({ srcUser: user.name }, function(err, pics){
if(err) callback(err);
else {
user.pics = pics;
callback();
}
});
};
// if user is logged in, get their content
if(req.session.username){
async.parallel({
// these methods execute in parallel
uploads: function(callback){
Pic.find({srcUser: req.session.username}, function(err, result){
callback(err || null, result);
});
},
categories: function(callback){
User.find({username: req.session.username}, function(err, users){
if(err) callback(err);
else {
var user = users[0];
for (var i = 0; i < user.favCategories.length; i++) {
trackedCats[i] = {
name: user.favCategories[i],
pics: []
}
}
async.each(trackedCats, getPicsInCategory, function(err){
if(err) callback(err);
else {
for (var i = 0; i < trackedCats.length; i++) {
recentPics[trackedCats[i].name] = trackedCats[i].pics[trackedCats[i].pics.length-1];
}
callback();
}
});
}
});
},
users: function(callback) {
User.find({username: req.session.username}, function(err, users){
if(err) callback(err);
else {
var user = users[0];
for (var i = 0; i < user.favUsers.length; i++)
{
trackedUsers[i] = {
name: user.favUsers[i],
pics: []
}
}
async.each(trackedUsers, getPicsFromUser, function(err){
if(err) callback(err);
else {
for (var i = 0; i < trackedUsers.length; i++) {
userPics[trackedUsers[i].name] = trackedUsers[i].pics[trackedUsers[i].pics.length-1];
}
callback();
}
});
}
});
}
},
function(err, results){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
// pics are in results object
lastUpload = results.uploads[results.uploads.length-1];
routes.home(req, res, lastUpload, recentPics, userPics);
});
} else {
res.redirect("/");
}
});
// file upload form
app.get('/upload', function(req, res) {
if(req.session.username) routes.upload(req, res);
else res.redirect("/");
});
// gallery of user's own uploads
app.get('/myUploads', function(req, res){
if(req.session.username){
Pic.find({srcUser: req.session.username}, function(err, pics){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
routes.catViewer(req, res, "My Uploads", pics);
});
} else {
res.redirect("/");
}
});
// log this user out
app.get('/logout', function(req, res) {
if(req.session.username){
req.session.destroy(function(err) {
if(err) {
console.error(err);
res.redirect("/error");
return;
}
res.redirect("/");
});
} else {
res.redirect("/");
}
});
// category index page; don't check for log in, categories are publicly available
app.get('/catIndex', function(req, res){
var recentPics = {
funny: [],
sports: [],
nature: [],
people: [],
art: [],
misc: []
};
// find recent pictures from each category
async.parallel({
f: function(callback){
Pic.find({categories: { $in: ["funny"] } }, function(err, result){
callback(err || null, result);
});
},
s: function(callback){
Pic.find({categories: { $in: ["sports"] } }, function(err, result){
callback(err || null, result);
});
},
n: function(callback){
Pic.find({categories: { $in: ["nature"] } }, function(err, result){
callback(err || null, result);
});
},
p: function(callback){
Pic.find({categories: { $in: ["people"] } }, function(err, result){
callback(err || null, result);
});
},
a: function(callback){
Pic.find({categories: { $in: ["art"] } }, function(err, result){
callback(err || null, result);
});
},
m: function(callback){
Pic.find({categories: { $in: ["misc"] } }, function(err, result){
callback(err || null, result);
});
}
},
function(err, results){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
if (results.f[results.f.length-1] !== undefined) recentPics.funny.push(results.f[results.f.length-1]);
if (results.f[results.f.length-2] !== undefined) recentPics.funny.push(results.f[results.f.length-2]);
if (results.f[results.f.length-3] !== undefined) recentPics.funny.push(results.f[results.f.length-3]);
if (results.s[results.s.length-1] !== undefined) recentPics.sports.push(results.s[results.s.length-1]);
if (results.s[results.s.length-2] !== undefined) recentPics.sports.push(results.s[results.s.length-2]);
if (results.s[results.s.length-3] !== undefined) recentPics.sports.push(results.s[results.s.length-3]);
if (results.n[results.n.length-1] !== undefined) recentPics.nature.push(results.n[results.n.length-1]);
if (results.n[results.n.length-2] !== undefined) recentPics.nature.push(results.n[results.n.length-2]);
if (results.n[results.n.length-3] !== undefined) recentPics.nature.push(results.n[results.n.length-3]);
if (results.p[results.p.length-1] !== undefined) recentPics.people.push(results.p[results.p.length-1]);
if (results.p[results.p.length-2] !== undefined) recentPics.people.push(results.p[results.p.length-2]);
if (results.p[results.p.length-3] !== undefined) recentPics.people.push(results.p[results.p.length-3]);
if (results.a[results.a.length-1] !== undefined) recentPics.art.push(results.a[results.a.length-1]);
if (results.a[results.a.length-2] !== undefined) recentPics.art.push(results.a[results.a.length-2]);
if (results.a[results.a.length-3] !== undefined) recentPics.art.push(results.a[results.a.length-3]);
if (results.m[results.m.length-1] !== undefined) recentPics.misc.push(results.m[results.m.length-1]);
if (results.m[results.m.length-2] !== undefined) recentPics.misc.push(results.m[results.m.length-2]);
if (results.m[results.m.length-3] !== undefined) recentPics.misc.push(results.m[results.m.length-3]);
routes.catIndex(req, res, recentPics);
});
});
// cateogry routing
app.get('/catFunny', function(req, res){
Pic.find({categories: { $in: ["funny"] } }, function(err, pics){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
routes.catViewer(req, res, "Funny", pics);
});
});
app.get('/catSports', function(req, res){
Pic.find({categories: { $in: ["sports"] } }, function(err, pics){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
routes.catViewer(req, res, "Sports", pics);
});
});
app.get('/catNature', function(req, res){
Pic.find({categories: { $in: ["nature"] } }, function(err, pics){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
routes.catViewer(req, res, "Nature", pics);
});
});
app.get('/catPeople', function(req, res){
Pic.find({categories: { $in: ["people"] } }, function(err, pics){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
routes.catViewer(req, res, "People", pics);
});
});
app.get('/catArt', function(req, res){
Pic.find({categories: { $in: ["art"] } }, function(err, pics){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
routes.catViewer(req, res, "Art", pics);
});
});
app.get('/catMisc', function(req, res){
Pic.find({categories: { $in: ["misc"] } }, function(err, pics){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
routes.catViewer(req, res, "Miscellaneous", pics);
});
});
/**********************************************
route/handle POST requests
***********************************************/
// login form submission
app.post('/login', function(req, res){
var username = req.body.username;
var password = req.body.password;
// look for the requested username
User.find({username: username}, function(err, users){
if(err) {
console.error(err);
res.redirect("/error");
return;
}
else if (users.length==0) {
res.redirect("/?error=Invalid Username");
return;
}
var user = users[0];
// compare entered password to stored password
bcrypt.compare(password, user.password, function(err, authenticated) {
if(authenticated) {
req.session.username = user.username;
res.redirect("/home");
}else{
res.redirect("/?error=Incorrect Password for this Username.");
}
});
});
});
// file upload form submission
app.post('/uploadFile', function(req, res) {
// check if a file was selected and then removed via file upload form
// note that uploaded file is defined by default, so this check only works for above situation
if (req.files.uploadedFile === undefined)
{
res.redirect("/upload?error=No file selected.");
return;
}
var newPic;
var type = req.files.uploadedFile.type;
var newCaption = req.body.caption;
if (newCaption === "")
newCaption = "< No caption >";
// build tracked category array from checkbox results
var choices = [];
if (req.body.catBoxFunny) choices.push("funny");
if (req.body.catBoxSports) choices.push("sports");
if (req.body.catBoxNature) choices.push("nature");
if (req.body.catBoxPeople) choices.push("people");
if (req.body.catBoxArt) choices.push("art");
if (req.body.catBoxMisc) choices.push("misc");
// get initial path of file
var tmp_path = req.files.uploadedFile.path;
// set the target path using the actual name of the file
var target_path = './public/uploads/' + req.files.uploadedFile.name;
// attempt to rename the file; simultaneously check that the file is valid
fs.rename(tmp_path, target_path, function(err) {
if (err) // error thrown if no file was selected at all
{
console.error(err);
res.redirect("/upload?error=No file selected.");
return;
}
// make sure the file is an image
if (type.slice(0,5) !== "image") {
// note that the file is already uploaded, we simply do not create a db document for it...would be better to prevent upload in the first place
res.redirect("/upload?error=The file you submitted is not an image.");
return;
}
// file is an image, so create new picture document
newPic = new Pic({
filename: req.files.uploadedFile.name,
path: './uploads/' + req.files.uploadedFile.name,
srcUser: req.session.username,
categories: choices,
caption: newCaption
});
newPic.save(function(err, newPic) {
console.log("new pic saved: " + newPic);
// delete the temporary file (with garbage filename)
fs.unlink(tmp_path, function() {
if (err) console.error(err);
routes.uploadSuccess(req, res, newPic.path, newCaption);
});
});
});
});
// account registration form submitted
app.post('/registerSubmit', function(req, res) {
var username = req.body.username;
var password = req.body.password;
var passconf = req.body.passconf;
// store catgegories selected from checkboxes
var choices = [];
if (req.body.catBoxFunny) choices.push("funny");
if (req.body.catBoxSports) choices.push("sports");
if (req.body.catBoxNature) choices.push("nature");
if (req.body.catBoxPeople) choices.push("people");
if (req.body.catBoxArt) choices.push("art");
if (req.body.catBoxMisc) choices.push("misc");
// check if password confirmation does not match
// this could also be checked client-side, but meh
if (req.body.password !== req.body.passconf) {
req.body.password = "";
req.body.passconf = "";
res.redirect('/register?error=The passwords you entered do not match.');
return;
}
User.find({username: username}, function(err, users) {
if (err) {
console.error(err);
res.redirect("/register?error=An error occured.");
return;
}
// check if the user already exists
if(users.length!=0){
res.redirect("/register?error=This Username has been taken.");
return;
}
// hash the password and store the hash in a new User document
bcrypt.genSalt(10, function(err, salt) {
if (err) {
console.error(err);
res.redirect("/register?error=An error occured.");
return;
}
bcrypt.hash(password, salt, null, function(err, hash) {
if (err) {
console.error(err);
res.redirect("/register?error=An error occured.");
return;
}
var newUser = new User({
username: username,
password: hash,
uploads: [],
favCategories: choices
});
// save new user's info to DB
newUser.save(function(err, newUser) {
routes.registerSuccess(req, res, newUser.username);
});
});
});
});
});
// search for a user and track them
app.post('/trackUser', function(req, res) {
// prevent self-tracking
if (req.body.user === req.session.username) {
res.redirect("/home?error=You cannot track yourself!");
return;
}
User.find({username: req.body.user}, function(err,users) {
if (err) {
console.error(err);
res.redirect("/error");
return;
}
if (users.length !== 0) {
// user exists; add it this users' tracked users
User.find({username: req.session.username}, function(err,thisUser) {
if (err) {
console.error(err);
res.redirect("/error");
return;
}
var user = thisUser[0];
if (user.favUsers.contains(users[0].username)) {
res.redirect("/home?error=You are already tracking user \"" + req.body.user + "\"");
return;
}
user.favUsers.push(users[0].username);
var conditions = { username: req.session.username };
var update = { $set: { favUsers: user.favUsers }};
User.update(conditions, update, null, function(err){
if (err) {
console.error(err);
res.redirect("/error");
return;
}
res.redirect("/home?msg=Now tracking user \"" + req.body.user + "\"");
});
});
} else {
// user does not exist
res.redirect("/home?error=No user by the name \"" + req.body.user + "\" exists.");
}
});
});
// search for a category and track it if it is valid
app.post('/trackCat', function(req, res) {
var user;
// confirm the input is a valid category
if (categories.contains(req.body.cat)) {
// retrieve user info
User.find({username: req.session.username}, function(err,results) {
if (err) {
console.error(err);
res.redirect("/error");
return;
}
user = results[0];
if (!user) {
res.redirect("/error");
return;
}
console.log(user.favCategories);
if (user.favCategories.contains(req.body.cat))
res.redirect("/home?error=You are already tracking category \"" + req.body.cat + "\"");
else
{
// update this user's schema
var conditions = { username: req.session.username };
var update = { $set: { favCategories: user.favCategories }};
user.favCategories.push(req.body.cat);
User.update(conditions, update, null, function(err) {
if (err) {
console.error(err);
res.redirect("/error");
return;
}
res.redirect("/home?msg=You are now tracking category \"" + req.body.cat + ".\"");
});
}
});
} else {
res.redirect("/home?error=No category by the name \"" + req.body.cat + "\" exists.");
}
});
// run app
http.createServer(app).listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});