-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebServer.js
More file actions
800 lines (753 loc) · 26.9 KB
/
Copy pathwebServer.js
File metadata and controls
800 lines (753 loc) · 26.9 KB
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/**
* This builds on the webServer of previous projects in that it exports the
* current directory via webserver listing on a hard code (see portno below)
* port. It also establishes a connection to the MongoDB named 'project6'.
*
* To start the webserver run the command:
* node webServer.js
*
* Note that anyone able to connect to localhost:portNo will be able to fetch
* any file accessible to the current user in the current directory or any of
* its children.
*
* This webServer exports the following URLs:
* / - Returns a text status message. Good for testing web server
* running.
* /test - Returns the SchemaInfo object of the database in JSON format.
* This is good for testing connectivity with MongoDB.
* /test/info - Same as /test.
* /test/counts - Returns the population counts of the cs collections in the
* database. Format is a JSON object with properties being the
* collection name and the values being the counts.
*
* The following URLs need to be changed to fetch there reply values from the
* database:
* /user/list - Returns an array containing all the User objects from
* the database (JSON format).
* /user/:id - Returns the User object with the _id of id (JSON
* format).
* /photosOfUser/:id - Returns an array with all the photos of the User (id).
* Each photo should have all the Comments on the Photo
* (JSON format).
*/
// ADDED IN PROJ7
const session = require("express-session");
const bodyParser = require("body-parser");
const multer = require("multer");
const processFormBody = multer({storage: multer.memoryStorage()}).single('uploadedphoto');
const fs = require("fs");
const mongoose = require("mongoose");
mongoose.Promise = require("bluebird");
const async = require("async");
const express = require("express");
const app = express();
// Load the Mongoose schema for User, Photo, and SchemaInfo
const User = require("./schema/user.js");
const Photo = require("./schema/photo.js");
const SchemaInfo = require("./schema/schemaInfo.js");
const ActivityEvent = require("./schema/activityEvent.js");
// Import password hashing functions
const passwordFxns = require('./password');
function logEvent(type, photo_filename = null, user = null) {
let event = new ActivityEvent({
type: type, photo_filename: photo_filename, user: user
});
event.save();
}
mongoose.set("strictQuery", false);
mongoose.connect("mongodb://127.0.0.1/project6", {
useNewUrlParser: true, useUnifiedTopology: true,
});
// (http://expressjs.com/en/starter/static-files.html) do all
// // We have the express static module the work for us.
app.use(express.static(__dirname));
// ADDED IN PROJ7
app.use(session({secret: "secretKey", resave: false, saveUninitialized: false}));
app.use(bodyParser.json());
app.get("/", function (request, response) {
response.send("Simple web server of files from " + __dirname);
});
/**
* Use express to handle argument passing in the URL. This .get will cause
* express to accept URLs with /test/<something> and return the something in
* request.params.p1.
*
* If implement the get as follows:
* /test - Returns the SchemaInfo object of the database in JSON format.
* This is good for testing connectivity with MongoDB.
* /test/info - Same as /test.
* /test/counts - Returns an object with the counts of the different collections
* in JSON format.
*/
app.get("/test/:p1", function (request, response) {
if (request.session.login_name) {
const param = request.params.p1 || "info";
if (param === "info") {
// Fetch the SchemaInfo. There should only one of them. The query of {} will
// match it.
SchemaInfo.find({}, function (err, info) {
if (err) {
// Query returned an error. We pass it back to the browser with an
// Internal Service Error (500) error code.
console.error("Error in /user/info:", err);
response.status(500)
.send(JSON.stringify(err));
return;
}
if (info.length === 0) {
// Query didn't return an error but didn't find the SchemaInfo object -
// This is also an internal error return.
response.status(400)
.send("Missing SchemaInfo");
return;
}
// We got the object - return it in JSON format.
response.end(JSON.stringify(info[0]));
});
} else if (param === "counts") {
// In order to return the counts of all the collections we need to do an
// async call to each collection. That is tricky to do so we use the async
// package do the work. We put the collections into array and use async.each
// to do each .count() query.
const collections = [{
name: "user", collection: User
}, {
name: "photo", collection: Photo
}, {
name: "schemaInfo", collection: SchemaInfo
},];
async.each(collections, function (col, done_callback) {
col.collection.countDocuments({}, function (err, count) {
col.count = count;
done_callback(err);
});
}, function (err) {
if (err) {
response.status(500)
.send(JSON.stringify(err));
} else {
const obj = {};
for (let i = 0; i < collections.length; i++) {
obj[collections[i].name] = collections[i].count;
}
response.end(JSON.stringify(obj));
}
});
} else {
// If we know understand the parameter we return a (Bad Parameter) (400)
// status.
response.status(400)
.send("Bad param " + param);
}
} else {
response.status(401).send();
}
});
/**
* URL /user/list - Returns all the User objects.
*/
app.get("/user/list", function (request, response) {
if (request.session.login_name) {
User.find({}, {
_id: 1, first_name: 1, last_name: 1
}, function (err, users) {
if (err) {
console.error("Error in /user/list", err);
response.status(500)
.send(JSON.stringify(err));
return;
}
if (users.length === 0) {
response.status(400)
.send();
return;
}
response.end(JSON.stringify(users));
});
} else {
response.status(401).send();
}
});
/**
* URL /user/:id - Returns the information for User (id).
*/
app.get("/user/:id", function (request, response) {
if (request.session.login_name) {
const id = request.params.id;
let mongoTargetObj;
try {
mongoTargetObj = new mongoose.Types.ObjectId(id);
} catch (e) {
response.status(400).send();
}
User.find({_id: {$eq: mongoTargetObj}}, {__v: 0}, function (err, user) {
if (err) {
console.error("Error in /user/:id", err);
response.status(500)
.send(JSON.stringify(err));
return;
}
if (user.length === 0) {
response.status(400)
.send();
return;
}
const userDTO = {
_id: user[0]._id,
first_name: user[0].first_name,
last_name: user[0].last_name,
location: user[0].location,
description: user[0].description,
occupation: user[0].occupation
};
response.end(JSON.stringify(userDTO));
});
} else {
response.status(401).send();
}
});
/**
* URL /photosOfUser/:id - Returns the Photos for User (id).
*/
app.get("/photosOfUser/:id", function (request, response) {
if (request.session.login_name) {
const id = request.params.id;
let mongoTargetObj;
try {
mongoTargetObj = new mongoose.Types.ObjectId(id);
} catch (e) {
response.status(400).send();
}
Photo.aggregate([{
$match: {user_id: {$eq: mongoTargetObj}}
}, {
$addFields: {
comments: {$ifNull: ["$comments", []]}
}
}, {
$lookup: {
from: "users", localField: "comments.user_id", foreignField: "_id", as: "users"
}
}, {
$addFields: {
comments: {
$map: {
input: "$comments",
in: {
$mergeObjects: ["$$this", {
user: {
$arrayElemAt: ["$users", {
$indexOfArray: ["$users._id", "$$this.user_id"]
}]
}
}]
}
}
}
}
}, {
$project: {
users: 0,
__v: 0,
"comments.__v": 0,
"comments.user_id": 0,
"comments.user.login_name": 0,
"comments.user.password": 0,
"comments.user.location": 0,
"comments.user.description": 0,
"comments.user.occupation": 0,
"comments.user.__v": 0
}
}, {
$addFields: {
liked_count: {
$size: {
$ifNull: ["$liked_by", []]
}
}
}
},{
$sort: {
liked_count: -1,
date_Time: -1
}
}
], function (err, photos) {
if (err) {
console.error("Error in /photosOfUser/:id", err);
response.status(500)
.send(JSON.stringify(err));
return;
}
if (photos.length === 0) {
response.status(400)
.send();
return;
}
response.end(JSON.stringify(photos));
});
} else {
response.status(401).send();
}
});
app.get("/mostCommentedPhoto/:id", function (request, response) {
if (request.session.login_name) {
const id = request.params.id;
let mongoTargetObj;
try {
mongoTargetObj = new mongoose.Types.ObjectId(id);
} catch (e) {
response.status(400).send();
}
Photo.aggregate([{
$match: {user_id: {$eq: mongoTargetObj}}
}, {
$addFields: {
comments: {$ifNull: ["$comments", []]}
}
}, {
$lookup: {
from: "users", localField: "comments.user_id", foreignField: "_id", as: "users"
}
}, {
$addFields: {
comments: {
$map: {
input: "$comments",
in: {
$mergeObjects: ["$$this", {
user: {
$arrayElemAt: ["$users", {
$indexOfArray: ["$users._id", "$$this.user_id"]
}]
}
}]
}
}
}
}
}, {
$project: {
users: 0,
__v: 0,
"comments.__v": 0,
"comments.user_id": 0,
"comments.user.login_name": 0,
"comments.user.password": 0,
"comments.user.location": 0,
"comments.user.description": 0,
"comments.user.occupation": 0,
"comments.user.__v": 0
}
}], function (err, photos) {
if (err) {
console.error("Error in /photosOfUser/:id", err);
response.status(500)
.send(JSON.stringify(err));
return;
}
if (photos.length === 0) {
response.status(400)
.send();
return;
}
response.end(JSON.stringify(photos));
});
} else {
response.status(401).send();
}
});
app.get("/recentPhotoOfUser/:id", function (request, response) {
if (request.session.login_name) {
const id = request.params.id;
let mongoTargetObj;
try {
mongoTargetObj = new mongoose.Types.ObjectId(id);
} catch (e) {
response.status(400).send();
return;
}
Photo.aggregate([
{ $match: { user_id: mongoTargetObj } },
{ $sort: { date_time: -1 } }, // Sort by date_time in descending order
{ $limit: 1 }, // Limit to the most recent photo
{
$addFields: {
comments: { $ifNull: ["$comments", []] }
}
},
{
$lookup: {
from: "users",
localField: "comments.user_id",
foreignField: "_id",
as: "users"
}
},
{
$addFields: {
comments: {
$map: {
input: "$comments",
in: {
$mergeObjects: ["$$this", {
user: {
$arrayElemAt: ["$users", {
$indexOfArray: ["$users._id", "$$this.user_id"]
}]
}
}]
}
}
}
}
},
{
$project: {
users: 0,
__v: 0,
"comments.__v": 0,
"comments.user_id": 0,
"comments.user.login_name": 0,
"comments.user.password": 0,
"comments.user.location": 0,
"comments.user.description": 0,
"comments.user.occupation": 0,
"comments.user.__v": 0
}
}
], function (err, photos) {
if (err) {
console.error("Error in /recentPhotoOfUser/:id", err);
response.status(500).send(JSON.stringify(err));
return;
}
if (photos.length === 0) {
response.status(400).send();
return;
}
response.end(JSON.stringify(photos));
});
} else {
response.status(401).send();
}
});
/**
* URL /commentsOfUser/:id - Returns the Comments for User (id).
*/
app.get("/commentsOfUser/:id", function (request, response) {
if (request.session.login_name) {
const id = request.params.id;
let mongoTargetObj;
try {
mongoTargetObj = new mongoose.Types.ObjectId(id);
} catch (e) {
response.status(400).send();
}
Photo.aggregate([{
$unwind: "$comments",
}, {
$project: {
_id: "$comments._id",
user_id: "$comments.user_id",
photo_name: "$file_name",
date_time: "$comments.date_time",
text: "$comments.comment",
},
}, {
$match: {
user_id: mongoTargetObj
},
}], function (err, comments) {
if (err) {
console.error("Error in /commentsOfUser/:id", err);
response.status(500)
.send(JSON.stringify(err));
return;
}
if (comments.length === 0) {
response.status(400)
.send();
return;
}
response.end(JSON.stringify(comments));
});
} else {
response.status(401).send();
}
});
/**
* URL /admin/login - Creates User Session and Returns Username
*/
app.post("/admin/login", (request, response) => {
const {login_name, password} = request.body;
User.aggregate([{
$match: {
login_name: login_name
}
},
], function (err, users) {
const user = users[0];
if (user && passwordFxns.doesPasswordMatch(user.password.hash, user.password.salt, password)) {
request.session.login_name = login_name;
request.session.user_id = user._id;
logEvent("login");
response.status(200).json({message: "Successful Login", user: user, _id: user._id});
} else {
response.status(400).json({message: "Invalid Login Information"});
}
});
});
/**
* User /admin/logout - Clears Current Session
*/
app.post("/admin/logout", (request, response) => {
if (request.session.login_name) {
request.session.destroy();
logEvent("logout");
response.status(200).json({message: "Logout Successful"});
} else {
response.status(400).json({message: "No User Logged In"});
}
});
/**
* Post to /user - Registers a new user
*/
app.post("/user", (request, response) => {
const {first_name, last_name, location, description, occupation, login_name, password} = request.body;
// Check if any of the required fields are empty
if (!login_name) {
response.status(400).json({message: "Please enter username"});
return;
}
if (!password || password === 'invalid password') {
response.status(400).json({message: "Please enter password"});
return;
}
if (!first_name) {
response.status(400).json({message: "Please enter first name"});
return;
}
if (!last_name) {
response.status(400).json({message: "Please enter last name"});
return;
}
// Check if the username already exists
User.findOne({login_name}, (err, existingUser) => {
if (err) {
console.error("Error in /user", err);
response.status(500).json({message: "Registration failed"});
return;
}
if (existingUser) {
response.status(400).json({message: "Username already exists"});
} else {
// Create a new user
const newUser = new User({
first_name, last_name, location, description, occupation, login_name, password
});
newUser.save((errUser, user) => {
if (errUser) {
console.error("Error in /user", errUser);
response.status(500).json({message: "Registration failed"});
return;
}
request.session.login_name = login_name;
logEvent("register");
response.status(200).json({message: "Registration successful", user, login_name});
});
}
});
});
/**
* Post to /commentsOfPhoto/:photo_id - Adds a comment to a photo
*/
app.post('/commentsOfPhoto/:photo_id', function (request, response) {
if (request.session.login_name) {
const timestamp = new Date().valueOf();
const id = new mongoose.Types.ObjectId(request.params.photo_id);
const commentInput = request.body.comment;
const commentBody = {
comment: commentInput, date_time: timestamp, user_id: request.session.user_id
};
Photo.findById({
_id: id
}).then((photo) => {
photo.comments = photo.comments.concat(commentBody);
photo.save((err) => {
if (err) {
console.error('/commentsOfPhoto/:photoId', err);
response.status(400).json({message: "Comment Upload Failed"});
return;
}
logEvent("comment", photo.file_name, request.session.login_name);
response.status(200).json({message: "Comment Upload Success"});
});
});
}
});
/**
* Post to /photos/new - Adds a new photo
*/
app.post("/photos/new", (request, response) => {
if (request.session.login_name) {
processFormBody(request, response, function (err) {
if (err || !request.file) {
response.status(400)
.send(JSON.stringify(err));
return;
}
// request.file has the following properties of interest:
// fieldname - Should be 'uploadedphoto' since that is what we sent
// originalname - The name of the file the user uploaded
// mimetype - The mimetype of the image (e.g., 'image/jpeg',
// 'image/png')
// buffer - A node Buffer containing the contents of the file
// size - The size of the file in bytes
const timestamp = new Date().valueOf();
const filename = 'U' + String(timestamp) + request.file.originalname;
fs.writeFile("./images/" + filename, request.file.buffer, function (errWrite) {
if (errWrite) {
console.log("Error Writing to file in /photos/new", errWrite);
return;
}
const newPhoto = new Photo({
file_name: filename, date_time: timestamp, user_id: request.session.user_id
});
newPhoto.save((errPhoto) => {
if (errPhoto) {
console.error("Error in /photos/new", errPhoto);
response.status(400).json({message: "Photo Upload Failed"});
return;
}
logEvent("photo", filename, request.session.login_name);
response.status(200).json({message: "Photo Upload Success"});
});
});
});
}
});
app.post("/favorite/:photo_id", (request, response) => {
if (request.session.login_name) {
const id = new mongoose.Types.ObjectId(request.params.photo_id);
Photo.findById({
_id: id
}).then((photo) => {
photo.favorite_by.push(request.session.user_id);
photo.save((err) => {
if (err) {
console.error('/favorite/:photo_id', err);
response.status(400).json({message: "Failed to Favorite Photo"});
return;
}
response.status(200).json({message: "Photo Favorited"});
});
});
} else {
response.status(401).json({message: "No User Logged In"});
}
});
app.delete("/favorite/:photo_id", (request, response) => {
if (request.session.login_name) {
const id = new mongoose.Types.ObjectId(request.params.photo_id);
Photo.findById({
_id: id
}).then((photo) => {
photo.favorite_by = photo.favorite_by.filter(item => item.toString() !== request.session.user_id.toString());
photo.save((err) => {
if (err) {
response.status(404).json({message: "Failed to find Photo"});
return;
}
response.status(204).send();
});
});
} else {
response.status(401).json({message: "No User Logged In"});
}
});
app.get("/favorites", (request, response) => {
if (request.session.login_name) {
let mongoTargetObj;
try {
mongoTargetObj = new mongoose.Types.ObjectId(request.session.user_id);
} catch (e) {
response.status(400).send();
}
Photo.aggregate([{
$match: {
favorite_by: mongoTargetObj
}
}], function (err, photos) {
if (err) {
console.error("Error in /favorites", err);
response.status(500).send(JSON.stringify(err));
return;
}
response.end(JSON.stringify(photos));
});
} else {
response.status(401).json({message: "No User Logged In"});
}
});
// Serve 5 most recent activities
app.get("/activity", (request, response) => {
if (request.session.login_name) {
ActivityEvent.aggregate([
{ $sort: { date_time: -1 } },
{ $limit: 5 }
], function (err, activityEvents) {
if (err) {
console.error("Error in /activity", err);
response.status(500).send(JSON.stringify(err));
return;
}
response.end(JSON.stringify(activityEvents));
});
} else {
response.status(401).json({message: "No User Logged In"});
}
});
app.post("/like/:photo_id", (request, response) => {
if (request.session.login_name) {
const id = new mongoose.Types.ObjectId(request.params.photo_id);
Photo.findById({
_id: id
}).then((photo) => {
photo.liked_by.push(request.session.user_id);
photo.save((err) => {
if (err) {
console.error('/like/:photo_id', err);
response.status(400).json({message: "Failed to Like Photo"});
return;
}
response.status(200).json({message: "Photo Liked"});
});
});
} else {
response.status(401).json({message: "No User Logged In"});
}
});
app.delete("/like/:photo_id", (request, response) => {
if (request.session.login_name) {
const id = new mongoose.Types.ObjectId(request.params.photo_id);
Photo.findById({
_id: id
}).then((photo) => {
photo.liked_by = photo.liked_by.filter(item => item.toString() !== request.session.user_id.toString());
photo.save((err) => {
if (err) {
response.status(404).json({message: "Failed to Like Photo"});
return;
}
response.status(204).send();
});
});
} else {
response.status(401).json({message: "No User Logged In"});
}
});
const server = app.listen(3000, function () {
const port = server.address().port;
console.log("Listening at http://localhost:" + port + " exporting the directory " + __dirname);
});