-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebServer.js
More file actions
693 lines (601 loc) · 21.1 KB
/
webServer.js
File metadata and controls
693 lines (601 loc) · 21.1 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
/**
* 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).
*/
const mongoose = require("mongoose");
const session = require("express-session");
const bodyParser = require("body-parser");
const multer = require("multer");
const async = require("async");
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
app.use(session({ secret: "secretKey", resave: false, saveUninitialized: false }));
app.use(bodyParser.json());
// 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");
mongoose.Promise = require("bluebird");
// XXX - Your submission should work without this line. Comment out or delete
// this line for tests and before submission!
mongoose.set("strictQuery", false);
mongoose.connect("mongodb://127.0.0.1/project6", {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => console.log('MongoDB connected...'))
.catch(err => console.error(err));
const db= mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', function() {
console.log("We're connected to the database!");
});
// Session check middleware
const checkSession = (req, res, next) => {
if (!req.session.userId) {
// Instead of redirecting, send a 401 status code
return res.status(401).send('Unauthorized - No active session found');
}
return next();
};
// We have the express static module
// (http://expressjs.com/en/starter/static-files.html) do all the work for us.
app.use(express.static(__dirname));
app.get("/", function (request, response) {
response.send("Simple web server of files from " + __dirname);
});
// Configure multer for file storage
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const uploadDir = path.join(__dirname, '/images');
// console.log('Upload directory:', uploadDir);
cb(null, uploadDir);
},
filename: function (req, file, cb) {
// Use the original filename from the client-side
cb(null, file.originalname);
}
});
const upload = multer({ storage: storage });
/**
* 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) {
// Express parses the ":p1" from the URL and returns it in the request.params
// objects.
console.log("/test called with param1 = ", request.params.p1);
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(500).send("Missing SchemaInfo");
return;
}
// We got the object - return it in JSON format.
console.log("SchemaInfo", info[0]);
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 collections. 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);
}
});
/**
* URL /user - adds a new user
*/
app.post("/user", function (request, response) {
const first_name = request.body.first_name || "";
const last_name = request.body.last_name || "";
const location = request.body.location || "";
const description = request.body.description || "";
const occupation = request.body.occupation || "";
const login_name = request.body.login_name || "";
const password = request.body.password || "";
if (first_name === "") {
console.error("Error in /user", first_name);
response.status(400).send("first_name is required");
return;
}
if (last_name === "") {
console.error("Error in /user", last_name);
response.status(400).send("last_name is required");
return;
}
if (login_name === "") {
console.error("Error in /user", login_name);
response.status(400).send("login_name is required");
return;
}
if (password === "") {
console.error("Error in /user", password);
response.status(400).send("password is required");
return;
}
User.exists({login_name: login_name}, function (err, returnValue){
if (err){
console.error("Error in /user", err);
response.status(500).send();
} else if (returnValue) {
console.error("Error in /user", returnValue);
response.status(400).send();
} else {
User.create(
{
_id: new mongoose.Types.ObjectId(),
first_name: first_name,
last_name: last_name,
location: location,
description: description,
occupation: occupation,
login_name: login_name,
password: password
})
.then((user) => {
request.session.user_id = user._id;
session.user_id = user._id;
response.end(JSON.stringify(user));
})
.catch(errr => {
console.error("Error in /user", errr);
response.status(500).send();
});
}
});
});
/**
* URL /user/list - Returns all the User objects.
*/
app.get("/user/list",checkSession ,function (request, response) {
User.find({},'_id first_name last_name', function (err, users) {
if (err) {
console.error("Error fetching users:", err);
response.status(500).send("Internal Server Error");
return;
}
response.status(200).json(users);
});
});
/**
* URL /user/:id - Returns the information for User (id).
*/
app.get("/user/:id", checkSession, function (request, response) {
const userId = request.params.id;
if (!mongoose.isValidObjectId(userId)) {
return response.status(400).send("Invalid user ID");
}
User.findById(userId)
.select('-__v')
.then(user => {
if (!user) {
return response.status(400).send("User not found");
}
const userResponse = {
_id: user._id,
first_name: user.first_name,
last_name: user.last_name,
location: user.location,
description: user.description,
occupation: user.occupation
};
response.status(200).json(userResponse);
})
.catch(err => {
console.error("Error fetching user:", err);
response.status(500).send("Internal Server Error");
});
// return;
});
app.get("/user/list", checkSession, function (request, response) {
console.log('API CALL');
const projection = {
_id:1,
first_name:1,
last_name:1
};
User.find({}, projection, function (err, userDetails) {
if (err) {
console.error("Error in /user/list:", err);
response.status(500).send(JSON.stringify(err));
} else if (userDetails.length === 0) {
response.status(400).send("Missing user list");
} else {
response.end(JSON.stringify(userDetails));
}
});
});
/**
* URL /photosOfUser/:id - Returns the Photos for User (id).
*/
app.get('/photosOfUser/:id', checkSession, async (req, response) => {
const userId = req.params.id;
const currentLoggedUserId = req.query.currentLoggedUserId;
console.log( `params: ${currentLoggedUserId}` );
if (!mongoose.isValidObjectId(userId)) {
return response.status(400).send("Invalid user ID");
}
try {
const userExists = await User.findById(userId);
if (!userExists) {
return response.status(404).send("User not found");
}
let current_photos = await Photo.find({ user_id: userId }).lean();
let photos = [];
// if( userId === currentLoggedUserId ){
// photos = current_photos;
// } else{
// current_photos.forEach( (photo => {
// if(( !photo.isPrivate && photo.sharingList.includes( currentLoggedUserId )) ){
// photos.push(photo);
// }
// }) );
// }
if (userId === currentLoggedUserId) {
console.log(`userId: ${userId}, ${currentLoggedUserId}`);
photos = current_photos;
} else {
current_photos.forEach((photo) => {
if (
(!photo.isPrivate && (photo.sharingList.includes(currentLoggedUserId) || photo.sharingList.length === 0))
){
photos.push( photo );
}
});
}
const promises = photos.map(photo =>
Promise.all(photo.comments.map(async comment => {
const user = await User.findById(comment.user_id, '_id first_name last_name');
return {
...comment,
user: {
_id: user._id,
first_name: user.first_name,
last_name: user.last_name
}
};
}))
.then(commentsWithUser => {
photo.comments = commentsWithUser; // Assign the enhanced comments back to the photo
photo.num_likes = photo.num_likes; // Assign the number of likes to the photo
return photo;
})
);
photos = await Promise.all(promises); // Ensure all photos have their comments enhanced
photos.sort((a, b) => {
if (a.num_likes !== b.num_likes) {
return b.num_likes - a.num_likes; // Sort descending order
} else {
return b.date_time - a.date_time; // Sort date_time in descending order (most recent first)
}
});
return response.status(200).json(photos);
} catch (error) {
console.error("Error processing request:", error);
return response.status(500).send("Internal Server Error");
}
});
app.put('/likephoto/:id', checkSession, async (req, response) => {
const photoId = req.params.id;
const userId = req.session.userId;
if (!mongoose.isValidObjectId(photoId) || !mongoose.isValidObjectId(userId)) {
return response.status(400).send("Invalid ID");
}
try {
// Check if the photo exists
const photo = await Photo.findById(photoId);
if (!photo) {
return response.status(404).send("Photo not found");
}
// Determine if the user has already liked the photo
const alreadyLiked = photo.likes.some(like => like.user_id.equals(userId));
if (alreadyLiked) {
// Unlike the photo
await Photo.updateOne(
{ _id: photoId },
{
$pull: { likes: { user_id: userId } },
$inc: { num_likes: -1 }
}
);
// Remove like from currentLoggedInUser's likedPhotos
await User.updateOne(
{ _id: userId },
{ $pull: { likedPhotos: { photo_id_of_photo_owner: photoId } } }
);
} else {
// Like the photo
await Photo.updateOne(
{ _id: photoId },
{
$push: { likes: { user_id: userId, date_time: new Date() } },
$inc: { num_likes: 1 }
}
);
// Add like to currentLoggedInUser's likedPhotos
await User.updateOne(
{ _id: userId },
{
$push: { likedPhotos: { _id: new mongoose.Types.ObjectId(), photo_id_of_photo_owner: photoId, user_id_of_photo_owner: photo.user_id } }
}
);
}
return response.status(200).json({ message: `Photo ${alreadyLiked ? 'unliked' : 'liked'}` });
} catch (error) {
console.error("Error processing request:", error);
return response.status(500).send("Internal Server Error");
}
});
app.post("/admin/login", async function (request, response) {
const { login_name,password } = request.body;
try {
if (!login_name || !password) {
return response.status(400).send("Username and password is required");
}
const user = await User.findOne({ login_name: login_name});
console.log("user:", user);
if (!user) {
return response.status(400).send("Username does not exist");
}
if (user.password !== password) {
return response.status(400).send("Incorrect password");
}
request.session.userId = user._id;
return response.status(200).send(user);
} catch (error) {
console.error("Error during login:", error);
return response.status(500).send("Internal Server Error");
}
});
app.post("/admin/logout", function (request, response) {
if (request.session) {
request.session.destroy(err => {
if (err) {
console.error("Logout error:", err);
response.status(500).send("Error logging out");
} else {
response.status(200).send("Logout successful");
}
});
} else {
response.status(400).send("Not logged in");
}
});
// Upload photo endpoint
app.post('/photos/new', checkSession, upload.single('uploadedphoto'), async (req, res) => {
console.log("3");
//let response;
if (!req.file) {
return res.status(400).send('No file uploaded');
}
try {
const sharingList = JSON.parse(req.body.sharingList || "[]");
const isPrivate = req.body.isPrivate;
const newPhoto = new Photo({
file_name: req.file.filename,
user_id: req.session.userId,
date_time: new Date(),
comments: [],
sharingList: sharingList,
isPrivate: isPrivate
});
// newPhoto.save().then((photo) => {
// res.status(200).send('ok');
// console.log("4");
// });
await newPhoto.save();
return res.status(200).send('Photo uploaded successfully');
} catch (error) {
console.error('Error uploading photo:', error);
return res.status(500).send('Internal Server Error');
}
});
app.post("/commentsOfPhoto/:photo_id", checkSession, async (req, res) => {
const { photo_id } = req.params;
const { comment } = req.body;
const userId = req.session.userId;
if (!comment) {
return res.status(400).send("Comment text is required");
}
try {
const photo = await Photo.findById(photo_id);
if (!photo) {
return res.status(404).send("Photo not found");
}
// Add the comment to the photo's comments array
photo.comments.push({
comment: comment,
date_time: new Date(),
user_id: userId
});
await photo.save();
return res.status(200).send("Comment added successfully");
} catch (error) {
console.error("Error adding comment:", error);
return res.status(500).send("Internal Server Error");
}
});
app.delete("/deleteaccount", checkSession, async (req, res) => {
const userId = req.session.userId;
if (!userId) {
return res.status(401).send("Unauthorized - No active session found");
}
try {
// Retrieve the user document to access liked photos
const user = await User.findById(userId);
if (!user) {
return res.status(404).send("User not found");
}
// Remove all likes by this user from other photos using an atomic operation
const likedPhotos = user.likedPhotos || [];
for (const likedPhoto of likedPhotos) {
await Photo.updateOne(
{ _id: likedPhoto.photo_id_of_photo_owner },
{ $pull: { likes: { user_id: userId } }, $inc: { num_likes: -1 } }
);
}
// Fetch the list of photo filenames before deleting them from the database
const photos = await Photo.find({ user_id: userId });
// Delete the user's photos
await Photo.deleteMany({ user_id: userId });
// Delete the photo files from the /images folder
for (const photo of photos) {
const filePath = path.join(__dirname, '/images', photo.file_name);
fs.unlink(filePath, err => {
if (err) {
console.error("Failed to delete photo file:", err);
}
});
}
// Delete the user
await User.findByIdAndDelete(userId);
// Destroy the session after deleting the account
req.session.destroy(err => {
if (err) {
console.error("Session destruction error:", err);
return res.status(500).send("Error logging out");
}
res.status(200).send("Account and related data deleted successfully");
});
} catch (error) {
console.error("Error deleting account:", error);
res.status(500).send("Internal Server Error");
}
});
app.delete("/deletecommentbyid", checkSession, async (req, res) => {
const userId = req.session.userId;
const commentId = req.body.commentId;
if (!userId) {
return res.status(401).send("Unauthorized - No active session found");
}
try {
// Locate the photo containing the comment and pull (remove) the comment from the array
const updateResult = await Photo.updateOne(
{ "comments._id": commentId, "comments.user_id": userId },
{ $pull: { comments: { _id: commentId } } }
);
// Check if the comment was successfully found and deleted
if (updateResult.modifiedCount === 0) {
return res.status(404).send("Comment not found or does not belong to user");
}
return res.status(200).send("Comment deleted successfully");
} catch (error) {
console.error("Error deleting comment:", error);
res.status(500).send("Internal Server Error");
}
});
app.delete("/deletephotobyid", checkSession, async (req, res) => {
const userId = req.session.userId;
const photoId = req.body.photoId;
if (!userId) {
return res.status(401).send("Unauthorized - No active session found");
}
try {
// Check if file belongs to the actual user and then delete
const photo = await Photo.findOne({ user_id: userId, _id: photoId }).exec();
if (!photo) {
return res.status(404).send("Photo not found or does not belong to user");
}
// delete file in local
const filePath = path.join(__dirname, '/images', photo.file_name);
fs.unlink(filePath, async err => {
if (err) {
console.error("Failed to delete photo file:", err);
return res.status(500).send("Failed to delete photo file");
}
// delete the user's photo record in the database
await Photo.deleteOne({ _id: photo._id });
return res.status(200).send("Photo deleted successfully");
});
} catch (error) {
console.error("Error deleting photo:", error);
res.status(500).send("Internal Server Error");
}
});
const server = app.listen(3000, function () {
const port = server.address().port;
console.log(
"Listening at http://localhost:" +
port +
" exporting the directory " +
__dirname
);
});