-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
executable file
·773 lines (697 loc) · 27.2 KB
/
routes.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
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
var fs = require('fs');
var Thumbnail = require('thumbnail');
var tmp_thumb_dir = './tmp_thumb';
const md5File = require('md5-file');
if(!fs.existsSync(tmp_thumb_dir)) {
fs.mkdirSync(tmp_thumb_dir);
}
var image_types = ["image/jpeg", "image/png", "image/gif", "image/bmp"];
var video_types = ["video/mp4", "video/mov", "video/3gpp"];
var document_types = ["application/msword", "application/msexcel", "application/pdf", "application/txt"];
var contact_types = ["text/vcard"];
var audio_types = ["audio/mpeg", "audio/mp4", "audio/wav", "audio/aac"];
function getDateTime() {
return (new Date()).toJSON().slice(0, 19).replace(/[-T]/g, ':');
}
module.exports = function(app, upload, mongoose, dbConn, NODE_UUID, NODE_TYPE, ffmpeg)
{
var m = require('./models.js')(mongoose);
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = Grid(dbConn.db);
//******************//
//*** API routes ***//
//******************//
// Route for requesting the UUID of this node
app.get('/uuid', function(req, res)
{
console.log("Received an id request");
res.setHeader('Content-Type', 'application/json');
res.json({'uuid':NODE_UUID, 'type':NODE_TYPE});
});
// Route for uploading a new file
app.post('/file/data', upload.single('filedata'), function(req, res, next)
{
console.log("["+getDateTime()+"] Upload request received for new file");
//Check if all fields are available. If not, reply with error.
if(!req.file || !req.body || !req.body.descriptiveName || !req.file.originalname
|| !req.body.context || !req.body.mimetype || !req.body.extension
|| !req.body.isPrivate || !req.body.phoneID)
{
console.log("["+getDateTime()+"] Invalid upload request received");
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
//Get field values from the request
var context = parseCtxToModel(req.body.context);
var uuid = req.file.originalname.replace(/\.[^/.]+$/, "");
var originalname = req.body.descriptiveName;
var mimetype = req.body.mimetype;
var extension = req.body.extension;
var filesize = req.body.filesize;
var creationdate = ((typeof req.body.creationdate !== undefined)
? req.body.creationdate
: new Date().getTime());
var isPrivate = ((req.body.isPrivate.indexOf("true") !== -1) ? true : false);
var phoneID = req.body.phoneID;
console.log(" UUID: " + uuid);
//Compute MD5 hash of uploaded file synchronously and check if
//this file is already in the database
const hash = md5File.sync(req.file.path);
//Populate a new file model with the metadata
var fileModel = new m.File(
{
"uuid" : uuid,
"md5": hash,
"descriptiveName" : originalname,
"mimetype" : mimetype,
"extension": extension,
"filesize" : filesize,
"creationTimestamp" : creationdate,
"context" : context,
"isPrivate" : isPrivate,
"phoneID" : phoneID
});
//Check if file with the given hash is already contained in the database.
m.File.find({ "md5" : hash }, function(err, result)
{
if(!result || result.length != 0)
{
//Reply with error that file already exists.
res.status(409).json({'error':1, 'error_msg':"The file already exists."});
return;
}
//First, write to GridFS, then create thumbnail,
//then save metadata in database
var writestream = gfs.createWriteStream(
{
filename: uuid
});
fs.createReadStream(req.file.path).pipe(writestream);
var thumbWriteStream = gfs.createWriteStream(
{
filename: 'thumb_'+uuid
});
//Create a thumbnail according to the filetype.
//Needs improvement.
if(image_types.includes(mimetype))
{
//Rename file to have file extension so that
//thumbnail module will accept it
fs.renameSync(req.file.path, req.file.path+"."+extension);
//Create thumbnail and store in GridFS as well
var original_path = req.file.destination;
var thumbnail = new Thumbnail(original_path, tmp_thumb_dir);
thumbnail.ensureThumbnail(req.file.filename+"."+extension, 256, null, function(err, createdThumbName)
{
if(err) { console.log("Error creating image thumbnail. Details: " + err); return; }
fs.createReadStream(tmp_thumb_dir + '/' + createdThumbName).pipe(thumbWriteStream);
thumbWriteStream.on('close', function(file)
{
//Store meta information in document
fileModel.save(function(err){});
res.status(201).json({'error':0, 'reply':'File stored successfully.'});
//Delete both tempfiles (thumb and original file)
fs.unlinkSync(tmp_thumb_dir + '/' + createdThumbName);
fs.unlinkSync(req.file.path+"."+extension);
});
});
}
else if(video_types.includes(mimetype))
{
var proc = new ffmpeg(req.file.path).thumbnail(
{
count: 1,
timemarks: ['0'],
folder: tmp_thumb_dir,
filename: 'thumb_'+uuid,
size: '256x?'
})
.on('end', function(stdout, stderr) {
fs.createReadStream(tmp_thumb_dir + '/thumb_'+uuid+'.png')
.pipe(thumbWriteStream);
thumbWriteStream.on('close', function(file)
{
//Store meta information in document
fileModel.save(function(err){});
res.status(201).json({'error':0, 'reply':'File stored successfully.'});
//Delete both tempfiles (thumb and original file)
fs.unlinkSync(tmp_thumb_dir + '/thumb_'+uuid+'.png');
fs.unlinkSync(req.file.path);
});
});
}
else if(contact_types.includes(mimetype))
{
fs.createReadStream('./icons/ic_contact.png').pipe(thumbWriteStream);
thumbWriteStream.on('close', function(file)
{
//Store meta information in document
fileModel.save(function(err){});
res.status(201).json({'error':0, 'reply':'File stored successfully.'});
//Delete original file
fs.unlinkSync(req.file.path);
});
}
else if(audio_types.includes(mimetype))
{
fs.createReadStream('./icons/ic_audio.png').pipe(thumbWriteStream);
thumbWriteStream.on('close', function(file)
{
//Store meta information in document
fileModel.save(function(err){});
res.status(201).json({'error':0, 'reply':'File stored successfully.'});
//Delete original file
fs.unlinkSync(req.file.path);
});
}
else
{
fs.createReadStream('./icons/ic_unknown_file.png').pipe(thumbWriteStream);
thumbWriteStream.on('close', function(file)
{
//Store meta information in document
fileModel.save(function(err){});
res.status(201).json({'error':0, 'reply':'File stored successfully.'});
//Delete original file
fs.unlinkSync(req.file.path);
});
}
});
});
// Route for getting a thumbnail by uuid
// The thumbnail will only be provided if the requested file is not
// marked as private. If it is marked private, it will only be provided if
// the requesting device ID matches the file creator's device id in the database.
app.get('/thumbnail/:uuid/:phoneID', function(req, res)
{
//Check if necessary parameters are given. If not, reply with error.
if(!req.params || !req.params.uuid || !req.params.phoneID)
{
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
var fUUID = req.params.uuid;
var phoneID = req.params.phoneID
console.log("["+getDateTime()+"] Thumbnail request received for " + fUUID);
//Read metadata for file from database and check if file is not private.
//If file is private, check if the correct phone id is requesting the file
m.File.findOne({'uuid': req.params.uuid}, function(err, result)
{
//Check if error occurred
if(err)
{
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
console.log(err);
return;
}
//Check if result is not valid
if(!result)
{
res.status(404).json({'error':1, 'error_msg':"Thumbnail not found."});
return;
}
//Check if user is allowed to read thumbnail
if(result.isPrivate && (phoneID != result.phoneID))
{
res.status(404).json({'error':1, 'error_msg':"Thumbnail not found!"});
return;
}
//Read thumbnail from GridFS
gfs.exist({filename: 'thumb_'+fUUID}, function(err, found)
{
if(!found)
{
res.status(404).json({'error':1, 'error_msg':"Thumbnail not found."});
return;
}
var readstream = gfs.createReadStream({filename: 'thumb_'+fUUID});
//Set response header to the corresponding mime type
res.setHeader('Content-Type', "image/jpeg");
//Allow caching of thumbnails for 24 hours
res.setHeader('Cache-Control', "max-age=86400");
try
{
readstream.pipe(res);
}
catch(err)
{
console.log(err);
res.status(500).json({'error':1, 'error_msg':"Failed to write stream!"});
return;
}
});
});
});
// Route for requesting metadata information about the file
// The metadata will only be provided if the requested file is not
// marked as private. If it is marked private, the metadata will only be
// provided if the requesting device ID matches the file creator's device
// id in the database.
app.get('/file/metadata/full/:uuid/:phoneID', function(req, res)
{
if(!req.params || !req.params.uuid || !req.params.phoneID)
{
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
var fUUID = req.params.uuid;
var phoneID = req.params.phoneID;
m.File.findOne({'uuid': req.params.uuid}, function(err, result)
{
if(err)
{
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
console.log(err);
return;
}
if(!result)
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
if(result.isPrivate && (phoneID != result.phoneID))
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
//Delete phone id from creator from the result
delete result.phoneID;
//Read MetaData from GridFS (to determine filesize)
gfs.findOne({filename: fUUID}, function(err, metadata) {
if(!err && metadata)
{
res.status(200).json(
{
'error':0,
'reply':
{
'metadata':result,
'filesize': metadata.length //in bytes
}
});
}
else
{
res.status(404).json({'error':1, 'reply':"File not found."});
}
});
});
});
// Route for requesting only lightweight metadata information about the file.
// This means: Without the context information.
// The metadata will only be provided if the requested file is not
// marked as private. If it is marked private, the metadata will only be
// provided if the requesting device ID matches the file creator's device
app.get('/file/metadata/light/:uuid/:phoneID', function(req, res)
{
if(!req.params || !req.params.uuid || !req.params.phoneID)
{
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
var fUUID = req.params.uuid;
var phoneID = req.params.phoneID;
m.File.findOne({'uuid': req.params.uuid}, function(err, result)
{
if(err)
{
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
console.log(err);
return;
}
if(!result)
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
if(result.isPrivate && (phoneID != result.phoneID))
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
//Delete phone id and context from creator from the result
delete result.phoneID;
delete result.context;
//Read MetaData from GridFS (to determine filesize)
gfs.findOne({filename: fUUID}, function(err, metadata) {
if(!err && metadata)
{
res.status(200).json(
{
'error':0,
'reply':
{
'metadata':result,
'filesize': metadata.length //in bytes
}
});
}
else
{
res.status(404).json({'error':1, 'reply':"File not found."});
}
});
});
});
// Route for downloading the full file by uuid
// The file will only be provided if it is not marked as private. If it
// is marked private, it will only be provided if the requesting device ID
// matches the file creator's device id in the database.
app.get('/file/data/:uuid/:phoneID', function(req, res)
{
if(!req.params || !req.params.uuid || !req.params.phoneID)
{
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
var fUUID = req.params.uuid;
var phoneID = req.params.phoneID;
console.log("["+getDateTime()+"] Download request received for file " + fUUID);
m.File.findOne({'uuid': fUUID}, function(err, result)
{
if(err)
{
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
console.log(err);
return;
}
if(!result)
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
if(result.isPrivate && (phoneID != result.phoneID))
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
gfs.exist({filename: req.params.uuid}, function(err, found)
{
if(!found)
{
//File not found in GridFS, so we delete it from the file collection
File.delete({'uuid':fUUID});
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
var filename = result.uuid + '.' + result.extension;
var readstream = gfs.createReadStream({filename: req.params.uuid});
//Set response header to the corresponding mime type
res.set('Content-Type', result.mimetype);
res.set('Content-Disposition', 'attachment; filename="'+filename+'"');
//Allow caching of file for 24 hours
res.setHeader('Cache-Control', "max-age=86400");
//Handle deletion while reading using try/catch
readstream.on('error', function(err) {
res.status(404).end();
return;
});
readstream.pipe(res);
});
});
});
// Route for getting the mime type of the file with the provided UUID
app.get('/file/mimetype/:uuid/:phoneID', function(req, res)
{
if(!req.params || !req.params.uuid || !req.params.phoneID)
{
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
var fUUID = req.params.uuid;
var phoneID = req.params.phoneID;
console.log("["+getDateTime()+"] Mimetype request received for " + fUUID);
m.File.findOne({'uuid': fUUID}, function(err, result)
{
if(err)
{
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
console.log(err);
return;
}
if(!result)
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
if(result.isPrivate && (phoneID != result.phoneID))
{
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
res.status(200).json({'error':0, 'reply': {'mimetype':result.mimetype}});
});
});
// Route for getting a file list based on the context passed as parameter
// Empty upload array as placeholder for multer
app.post('/file/search', upload.array(), function(req, res, next)
{
console.log("["+getDateTime()+"] New search request received");
if(!req.body || !req.body.context)
{
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
var rawCtx;
try {
rawCtx = JSON.parse(req.body.context);
}
catch(err)
{
console.log(err);
res.status(400).json({'error':1, 'error_msg':"Malformed request 2."});
return;
}
// Parse context into mongoose context model
var context = parseCtxToModel(req.body.context);
//Build the query based on context filter from request
var query = {};
if(context.location)
{
if(rawCtx.radius)
{
//Limit to 5km max
if(rawCtx.radius > 5000)
{
rawCtx.radius = 2000;
}
}
else
{
rawCtx.radius = 250;
}
query['context.location.loc'] =
{
$near:
{
$geometry:
{
type: "Point",
coordinates: context.location.loc
},
$maxDistance: rawCtx.radius
}
};
}
if(context.place)
{
query['context.places.places.name'] = context.place;
}
if(context.activity)
{
query['context.activity'] = context.activity;
}
if(context.network)
{
query['context.network'] = context.network;
}
if(context.noise)
{
query['context.noise.isSilent'] = context.noise.isSilent;
}
if(context.weekday)
{
query['context.weekday'] = context.weekday;
}
if(rawCtx.timeOfDay && rawCtx.timeSpanMS && context.time)
{
//Timespan of files must be timespan before and after
var timeBefore = new Date(context.time.getTime() - rawCtx.timeSpanMS);
var timeAfter = new Date(context.time.getTime() + rawCtx.timeSpanMS);
query['context.hours'] = {$gte: timeBefore.getHours(), $lte: timeAfter.getHours()};
}
//Do not start to query the database if no filter is provided,
//because we cannot provide all files for a request.
if(Object.keys(query).length == 0)
{
var reply = {"files" : []};
res.status(200).json({'error':0, 'reply':reply});
return;
}
query.isPrivate = false;
//Fetch matching files from database and reply with their UUIDs
var files = [];
m.File.find(query, function(err, results)
{
if(err)
{
console.log(err);
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
console.log(err);
return;
}
if(!results)
{
var reply = {"files" : []};
res.status(200).json({'error':0, 'reply':reply});
}
results.forEach(function(doc)
{
files.push(
{
"uuid":doc.uuid,
"creationTimestamp":doc.creationTimestamp,
"descriptiveName" : doc.descriptiveName,
"mimetype":doc.mimetype,
"filesize" : doc.filesize
});
});
var reply = {"files" : files};
res.status(200).json({'error':0, 'reply':reply});
});
});
//Route for deleting a file. Only the creator of the file can issue
//a delete request by providing his phone ID.
app.delete('/file/:uuid/:phoneID', function(req, res)
{
if(!req.params || !req.params.uuid || !req.params.phoneID)
{
console.log(" Malformed request.");
res.status(400).json({'error':1, 'error_msg':"Malformed request."});
return;
}
var fUUID = req.params.uuid;
var phoneID = req.params.phoneID
console.log("["+getDateTime()+"] Delete request received for " + fUUID);
//Read metadata for file from database and check if file is not private.
//Only allow deletion of the file, if the correct user who uploaded
//the file sent the request
m.File.findOne({uuid : fUUID, phoneID : phoneID}, function(err, result)
{
if(err)
{
console.log(" Error while accessing MongoDB.");
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
return;
}
if(!result)
{
console.log(" No file entry found in MongoDB.");
res.status(404).json({'error':1, 'reply':"File not found."});
return;
}
//Remove the document from MongoDB
result.remove();
//Check if the file exists in GridFS
gfs.exist({filename: 'thumb_'+fUUID}, function(err, found)
{
if(!found)
{
console.log(" File not found in GridFS.");
res.status(404).json({'error':1, 'error_msg':"File not found!"});
return;
}
//Remove the file from GridFS
gfs.remove({filename: fUUID}, function(err)
{
if(err)
{
console.log(" Error removing it from GridFS.");
res.status(500).json({'error':1, 'error_msg':"Internal server error."});
return;
}
res.status(200).json({'error':0, 'reply':"File successfully deleted."});
//Remove thumbnail from GridFS
gfs.remove({filename: 'thumb_'+fUUID}, function(err) {});
});
});
});
});
/**
* Tries to parse the given json string into a ContextSchema
*/
function parseCtxToModel(stringJsonContext)
{
//Fill models with data coming from context
var context = new m.Context;
var ctx = JSON.parse(stringJsonContext);
if(ctx.location)
{
context.location = new m.Location(
{
"description" : ctx.location.description,
"time" : ctx.location.time,
"loc" : [ctx.location.lng, ctx.location.lat]
});
}
if(ctx.places)
{
context.places = new m.Places;
context.places.time = ctx.places.time;
for(var key in ctx.places.places)
{
var p = ctx.places.places[key];
var place = new m.Place(
{
"id" : p.id,
"name" : p.name,
"loc" : new m.Location(
{
"loc" : [p.lng, p.lat]
}),
"type" : p.type,
"category": p.category,
"likelihood" : p.likelihood
});
context.places.places.push(place);
}
}
if(ctx.place)
{
context.place = ctx.place;
}
if(ctx.activity)
{
context.activity = ctx.activity.activity;
}
if(ctx.noise)
{
context.noise = new m.Noise({
"soundDb": ctx.noise.sound_db,
"soundRms": ctx.noise.sound_rms,
"isSilent": ctx.noise.isSilent,
"time": ctx.noise.time,
});
}
if(ctx.network)
{
context.network = new m.Network({
"isWifiConnected": ctx.network.isWifiConnected,
"wifiSsid": ctx.network.wifiSsid,
"mobileNetworkType": ctx.network.mobileNetworkType,
});
}
if(ctx.weekday)
{
context.weekday = ctx.weekday;
}
if(ctx.timestamp) {
var date = new Date(ctx.timestamp);
context.time = date;
context.hours = date.getHours();
context.minutes = date.getMinutes();
}
return context;
}
};