-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js~
1547 lines (1376 loc) · 56.4 KB
/
server.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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/env node
var express = require('express'),
app = express(),
http = require('http'),
oracledb = require('oracledb'),
// dbConfig = require('./dbconfig.js'),
bodyParser = require('body-parser'),
cookieParser = require("cookie-parser"),
ejs = require('ejs'),
util = require('util'),
path = require("path"),
url = require("url"),
atob = require("atob"),
stream = require("stream"),
Q = require("q");
var dbConfig = {user: "jwoo", password: "Pikachu1202jw", connectString: "localhost:1525/CRS"};
//for lab machines, change connectString to "CRS"
app.set('view engine', ejs);
app.use(cookieParser());
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended:true}));
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', '*');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
//Set port number.
app.set('port', 8080);
app.set('ip', "127.0.0.1");
//Add public folder for static files
app.use(express.static(__dirname));
app.get("*", function (req, res) {
var resource = url.parse(req.url).pathname;
if(~resource.indexOf("node_modules") || ~resource.indexOf("bower_components")) {
res.sendFile(path.join(__dirname));
} else {
if(resource.slice(-1) !== "/") {
res.sendFile(path.join(__dirname + "/app" + resource));
} else {
res.sendFile(path.join(__dirname + "/app" + resource, "index.html"));
}
}
});
//Server
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
});
process.on('SIGINT', function() {
process.exit();
});
app.post("/login", function (req, res) {
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(
"SELECT * " +
"FROM users " +
"WHERE user_name = :user_name AND password = :password",
{user_name: req.body.userName, password: req.body.password},
function (err, result) {
if (err) {
executeError(err, res);
} else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if(result.rows.length > 0) {
res.send({success: true});
} else {
res.status(422);
res.send(
{
error: true,
errorCode: 2,
message: "Username or password is incorrect. Please try again.",
success: false
}
);
}
}
doRelease(connection);
}
);
}
);
});
app.post("/photoid", function(req, res){
var DBQueryString;
DBQueryString =
"SELECT photo_id " +
"FROM images " +
"WHERE images.photo_id = " +
"(SELECT MAX(photo_id) FROM images)";
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString,
function (err, result) {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if (err) {
executeError(err, res);
} else {
// Should return the max id currently in the table
res.send({result: result.rows, success:true});
}
doRelease(connection);
}
);
});
});
app.post("/groupid", function(req, res){
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(
"SELECT GROUP_ID " +
"FROM groups " +
"ORDER BY GROUP_ID desc",
function (err, result) {
if (err) {
executeError(err, res);
} else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if(result.rows.length > 0) {
res.send({lastID: result.rows[0][0], success: true});
} else {
res.status(500);
res.send(
{
error: true,
errorCode: 2,
message: "Could not retrieve data",
success: false
}
);
}
}
doRelease(connection);
}
);
}
);
});
app.post("/updatePhoto", function(req, res){
var DBQueryString =
"UPDATE IMAGES " +
"SET PERMITTED = :permitted1, SUBJECT = :subject1, PLACE = :place1, TIMING = TO_DATE (:timing1, 'yyyy/mm/dd'), DESCRIPTION = :desc1 " +
"WHERE PHOTO_ID = :photoid1",
DBQueryParam = {permitted1: req.body.permit,
subject1: req.body.subject,
place1: req.body.place,
timing1: req.body.timing,
desc1: req.body.desc,
photoid1: req.body.photoid
};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
res.send({success:true});
}
doRelease(connection);
}
);
});
});
//Based on code found here: https://github.com/oracle/node-oracledb/blob/master/doc/api.md#lobhandling
app.post("/upload", function(req, res){
var DBQueryString =
"INSERT INTO images (photo_id, owner_name, permitted, subject, place, timing, description, thumbnail, photo) VALUES (:photo_id, :owner_name, :permitted, :subject, :place, TO_DATE (:timing, 'yyyy/mm/dd'), :description, EMPTY_BLOB(), EMPTY_BLOB()) RETURNING photo, thumbnail INTO :lobbv, :lobtn",
DBQueryParam = {photo_id: req.body.photo_id,
owner_name: req.body.owner_name,
permitted: req.body.permitted,
subject: req.body.subject,
place: req.body.place,
timing: req.body.timing,
description: req.body.descr,
lobbv: {type: oracledb.BLOB, dir: oracledb.BIND_OUT},
lobtn: {type: oracledb.BLOB, dir: oracledb.BIND_OUT}
};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(
DBQueryString,
DBQueryParam,
{autoCommit: false},
function (err, result) {
var byteCharacters, buffer, i, lob,
bufferStream, view, arrayBuffer, bytes;
var imageDeferred = Q.defer();
var thumbnailDeferred = Q.defer();
if (err || result.rowsAffected != 1 || result.outBinds.lobbv.length != 1) {
executeError(err, res);
} else {
// Create an ArrayBuffer from the base64 image
byteCharacters = atob(req.body.photo);
bytes = new Uint8Array(byteCharacters.length);
for (i = 0; i < byteCharacters.length; i++) {
bytes[i] = byteCharacters.charCodeAt(i);
}
arrayBuffer = bytes.buffer;
// Create an ArrayBuffer from the base64 thumbnail
thumbnailCharacters = atob(req.body.thumbnail);
thumbnailBytes = new Uint8Array(thumbnailCharacters.length);
for (i = 0; i < thumbnailCharacters.length; i++) {
thumbnailBytes[i] = thumbnailCharacters.charCodeAt(i);
}
thumbnailArrayBuffer = thumbnailBytes.buffer;
// Create a Node.js Buffers from the ArrayBuffers
buffer = new Buffer(arrayBuffer.byteLength);
view = new Uint8Array(arrayBuffer);
for (i = 0; i < buffer.length; i++) {
buffer[i] = view[i];
}
thumbnailBuffer = new Buffer(thumbnailArrayBuffer.byteLength);
view = new Uint8Array(thumbnailArrayBuffer);
for (i = 0; i < thumbnailBuffer.length; i++) {
thumbnailBuffer[i] = view[i];
}
// Put the buffer into a stream so that it can be piped to oracle blob
bufferStream = new stream.PassThrough();
bufferStream.end(buffer);
thumbnailStream = new stream.PassThrough();
thumbnailStream.end(thumbnailBuffer);
// When the buffer finishes writing to the lob, resolve the deferred
bufferStream.on("end", function () {
imageDeferred.resolve();
});
thumbnailStream.on("end", function () {
thumbnailDeferred.resolve();
});
// If there is an error then handle it
bufferStream.on("error", function (err) {
executeError(err, res);
imageDeferred.reject();
});
thumbnailStream.on("error", function (err) {
executeError(err, res);
thumbnailDeferred.reject();
});
Q.allSettled([imageDeferred.promise, thumbnailDeferred.promise]).then(function () {
// Q.allSettled([imageDeferred.promise]).then(function () {
connection.commit(function (err) {
if (err) {
executeError(err, res);
} else {
res.send({success: true});
}
});
});
lob = result.outBinds.lobbv[0];
thumbnaillob = result.outBinds.lobtn[0];
// If there is an error then handle it
lob.on("error", function (err) {
executeError(err, res);
imageDeferred.reject();
});
thumbnaillob.on("error", function (err) {
executeError(err, res);
thumbnailDeferred.reject();
});
lob.on('finish', function() {
console.log("lob.on 'finish' event");
connection.commit( function(err) {
if (err)
console.error(err.message);
else
console.log("Image uploaded successfully.");
connection.release(function(err) {
if (err) console.error(err.message);
});
});
});
// Put the images into the blob
bufferStream.pipe(lob);
thumbnailStream.pipe(thumbnaillob);
}
}
);
});
});
//testing
app.get("/upload", function(req, res){
var DBQueryString =
"select * from images where photo_id = :photoid",
DBQueryParam = {photoid: req.query.photoID};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
var lob, buffer, bufferLength;
// Array of promises
var lobLoading = [];
var lobPromises = [];
if (err) {
executeError(err, res);
} else {
result.rows.forEach(function (row, index, array) {
lob = row[row.length-1];
thumbnailLob = row[row.length-2];
buffer = new Buffer(0);
bufferLength = 0;
thumbnailBuffer = new Buffer(0);
thumbnailBufferLength = 0;
if (lob) {
// Add promise at index
// Have to use index*2 because each row has two blobs
// Q sucks, so we have to make an array of the deferreds and the promises...
lobLoading[index*2] = Q.defer();
lobPromises[index*2] = lobLoading[index*2].promise;
// When data comes in from the stream, add it to the buffer
lob.on("data", function (chunk) {
bufferLength = bufferLength + chunk.length;
buffer = Buffer.concat([buffer, chunk], bufferLength);
});
//When the data is finished coming in, change it to base 64 and add to result
lob.on("end", function () {
result.rows[index][row.length-1] = buffer.toString("base64");
});
// When the stream closes, resolce the promsie
lob.on("close", function (chunk) {
// Fulfill promise
lobLoading[index*2].resolve();
});
// Make sure we reject the promise when the stream fails so that we don't have a memory leak
lob.on("error", function () {
executeError(err, res);
// Reject promise
lobLoading[index*2].reject();
});
}
if (thumbnailLob) {
// Add promise at index
lobLoading[index*2+1] = Q.defer();
lobPromises[index*2+1] = lobLoading[index*2+1].promise;
// When data comes in from the stream, add it to the buffer
thumbnailLob.on("data", function (chunk) {
thumbnailBufferLength = thumbnailBufferLength + chunk.length;
thumbnailBuffer = Buffer.concat([thumbnailBuffer, chunk], thumbnailBufferLength);
});
//When the data is finished coming in, change it to base 64 and add to result
thumbnailLob.on("end", function () {
result.rows[index][row.length-2] = thumbnailBuffer.toString("base64");
});
// When the stream closes, resolve the promsie
thumbnailLob.on("close", function (chunk) {
// Fulfill promise
lobLoading[index*2+1].resolve();
});
// Make sure we reject the promise when the stream fails so that we don't have a memory leak
thumbnailLob.on("error", function () {
executeError(err, res);
// Reject promise
lobLoading[index*2+1].reject();
});
}
});
// When all promises complete, return the results
Q.allSettled(lobPromises).then(function () {
res.send({
success: true,
data: result.rows
});
});
}
}
// doRelease(connection);
);
});
});
app.route("/displayGroup")
.get(function(req, res){
var DBQueryString =
"SELECT friend_id, date_added " +
"FROM GROUP_LISTS " +
"WHERE GROUP_ID = :groupID" ,
DBQueryParam = {groupID: req.query.groupID};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
res.send({success: true, results: result.rows});
}
doRelease(connection);
}
);
});
})
.post(function(req, res){
// console.log(util.inspect(req.body, {showHidden: false, depth: null}));
var DBQueryString =
"INSERT INTO GROUP_LISTS (GROUP_ID, FRIEND_ID, DATE_ADDED, NOTICE)" +
"VALUES (:group_id, :user_name, CURRENT_DATE, :notice)" ,
DBQueryParam = {group_id: req.body.group_id, user_name: req.body.user_name, notice: null};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
res.send({success: true, results: result.rows});
}
doRelease(connection);
}
);
});
});
app.post("/deleteMember" , function(req, res){
var DBQueryString =
"DELETE from GROUP_LISTS WHERE group_id = :group_id AND FRIEND_ID = :user_name" ,
DBQueryParam = {group_id: req.body.group_id, user_name: req.body.user_name};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if (err) {
executeError(err, res);
} else {
res.send({success: true});
}
doRelease(connection);
}
);
});
});
app.post("/deleteGroup" , function(req, res){
var DBQueryUpdate =
"UPDATE images " +
"SET permitted = 2" +
"WHERE permitted = :group_id; "
var DBQueryStringList =
"DELETE from GROUP_LISTS "+
"WHERE group_id = :group_id; ";
var DBQueryStringGroup =
"DELETE from GROUPS " +
"WHERE group_id = :group_id; ";
var DBQueryParam = {group_id: req.body.group_id};
DBQueryTotal = "BEGIN\n" + DBQueryUpdate + DBQueryStringList + DBQueryStringGroup + "END;";
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryTotal, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
res.send({success: true});
}
doRelease(connection);
}
);
});
});
app.post("/memberships", function(req, res){
var DBQueryString =
"SELECT l.group_id, g.group_name " +
"FROM GROUP_LISTS l, GROUPS g " +
"WHERE l.FRIEND_ID = :userName AND l.GROUP_ID = g.GROUP_ID" ,
DBQueryParam = {userName: req.query.userName};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
res.send({success: true, results: result.rows});
}
doRelease(connection);
}
);
});
});
app.route("/groups")
.get(function(req, res){
var DBQueryString =
"SELECT * " +
"FROM GROUPS " +
"WHERE GROUPS.USER_NAME = :userName" ,
DBQueryParam = {userName: req.query.userName};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
res.send({success: true, results: result.rows});
}
doRelease(connection);
}
);
});
})
.post(function(req, res){
var DBQueryString =
"BEGIN\n " +
"INSERT INTO GROUPS (GROUP_ID, USER_NAME, GROUP_NAME, DATE_CREATED) " +
"VALUES (:groupID, :userName, :groupName, CURRENT_DATE); " +
"INSERT INTO GROUP_LISTS (GROUP_ID, FRIEND_ID, DATE_ADDED, NOTICE) " +
"VALUES (:groupID, :userName, CURRENT_DATE, :notice); " +
"END;",
DBQueryParam = {groupID: req.body.groupID,
userName: req.body.userName,
groupName: req.body.groupName,
notice: null
};
// console.log(util.inspect(req, {showHidden: false, depth: null}));
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
res.send({success: true});
}
doRelease(connection);
}
);
});
});
app.post("/deleteImage", function(req, res){
var DBQueryString_tracking = "DELETE from IMAGE_TRACKING WHERE PHOTO_ID = :photo_id; ",
DBQueryString_images = "DELETE from IMAGES WHERE PHOTO_ID = :photo_id; " ,
DBQueryString_total = "BEGIN\n" + DBQueryString_tracking + DBQueryString_images + "END;",
DBQueryParam = {photo_id: req.body.photo_id};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString_total, DBQueryParam,
{autoCommit: true},
function (err, result) {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if (err) {
executeError(err, res);
} else {
res.send({success: true});
}
doRelease(connection);
}
);
});
});
app.route("/register")
.get(function (req, res) {
/** Use to check if a user name or email is already taken.
* All parameters should be part of the query string.
*
* Possible parameters:
* userName
* email
*/
var DBQueryString,
DBQueryParam,
username = req.query.userName,
email = req.query.email;
DBQueryString =
"SELECT * " +
"FROM persons " +
"WHERE persons.user_name = :user_name OR persons.email = :email";
DBQueryParam = {user_name: username, email: email};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
function (err, result) {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if (err) {
executeError(err, res);
} else if (result.rows.length) {
//there was a tuple returned with the username and email
//so not unique and must use another combination
res.status(409);
res.send(
{
error: true,
errorCode: 4,
message: "Element already exists",
success: false
}
);
} else {
//username and email are unique so able to register
res.send({success: true});
}
doRelease(connection);
}
);
});
})
.post (function (req, res){
var DBQueryString,
DBQueryParam,
DBQueryTotal,
username = req.body.userName;
password = req.body.password;
fname = req.body.fname;
lname = req.body.lname;
addr = req.body.addr;
email = req.body.email;
phone = req.body.phone;
// Need to find how to calculate the current datestamp
DBQueryStringUsers =
"INSERT INTO users " +
"(USER_NAME, PASSWORD, DATE_REGISTERED) " +
"VALUES (:username, :password, CURRENT_DATE); ";
DBQueryStringPersons =
"INSERT INTO persons " +
"(USER_NAME, FIRST_NAME, LAST_NAME, ADDRESS, EMAIL, PHONE) " +
"VALUES (:username, :first_name, :last_name, :address, :email, :phone); ";
DBQueryTotal = "BEGIN\n" +DBQueryStringUsers + DBQueryStringPersons + "END;";
// Need to put the parameters in here
DBQueryParam = {username: username,
password: password,
first_name: fname,
last_name: lname,
address: addr,
email: email,
phone: phone
};
// console.log(util.inspect(DBQueryParam, {showHidden: false, depth: null}));
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
//Execute both SQL statements
connection.execute(DBQueryTotal, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
console.log(util.inspect(result, {showHidden: false, depth: null}));
res.send({success: true});
}
doRelease(connection);
}
);
});
});
getSearchResults = function (row, index) {
var lob, buffer, bufferLength;
var lobLoadingThumb = Q.defer();
var lobLoadingPhoto = Q.defer();
var deferred = Q.defer();
var imageObj;
var thumbObj;
lob = row[0];
thumbnailLob = row[1];
buffer = new Buffer(0);
bufferLength = 0;
thumbnailBuffer = new Buffer(0);
thumbnailBufferLength = 0;
// console.log(util.inspect(row, false, null));
if (lob) {
// console.log("LOB " + util.inspect(lob, false, null));
// When data comes in from the stream, add it to the buffer
lob.on("data", function (chunk) {
bufferLength = bufferLength + chunk.length;
buffer = Buffer.concat([buffer, chunk], bufferLength);
// console.log("DATA " + bufferLength);
});
//When the data is finished coming in, change it to base 64 and add to result
lob.on("end", function () {
imageObj = buffer.toString("base64");
// console.log("END " + imageObj);
});
// When the stream closes, resolce the promsie
lob.on("close", function (chunk) {
// console.log("CLOSE PHOTO + RESOLVE");
// Fulfill promise
lobLoadingPhoto.resolve();
});
// Make sure we reject the promise when the stream fails so that we don't have a memory leak
lob.on("error", function (err) {
// executeError(err, res);
// Reject promise
console.log("ERROR " + err);
lobLoadingPhoto.reject();
});
}
if (thumbnailLob) {
// When data comes in from the stream, add it to the buffer
thumbnailLob.on("data", function (chunk) {
thumbnailBufferLength = thumbnailBufferLength + chunk.length;
thumbnailBuffer = Buffer.concat([thumbnailBuffer, chunk], thumbnailBufferLength);
});
//When the data is finished coming in, change it to base 64 and add to result
thumbnailLob.on("end", function () {
thumbObj = thumbnailBuffer.toString("base64");
});
// When the stream closes, resolve the promsie
thumbnailLob.on("close", function (chunk) {
// Fulfill promise
lobLoadingThumb.resolve();
});
// Make sure we reject the promise when the stream fails so that we don't have a memory leak
thumbnailLob.on("error", function () {
// executeError(err, res);
// Reject promise
lobLoadingThumb.reject();
});
}
//When all promises complete, return the results
Q.all([lobLoadingPhoto.promise, lobLoadingThumb.promise]).then(function () {
deferred.resolve({imageObj: imageObj, thumbObj: thumbObj});
}).done();
return deferred.promise;
};
getImages = function (row, index) {
var lob, buffer, bufferLength;
var lobLoadingThumb = Q.defer();
var lobLoadingPhoto = Q.defer();
var deferred = Q.defer();
var imageObj;
var thumbObj;
lob = row[row.length-1];
thumbnailLob = row[row.length-2];
buffer = new Buffer(0);
bufferLength = 0;
thumbnailBuffer = new Buffer(0);
thumbnailBufferLength = 0;
if (lob) {
// console.log("LOB " + util.inspect(lob, false, null));
// When data comes in from the stream, add it to the buffer
lob.on("data", function (chunk) {
bufferLength = bufferLength + chunk.length;
buffer = Buffer.concat([buffer, chunk], bufferLength);
// console.log("DATA " + bufferLength);
});
//When the data is finished coming in, change it to base 64 and add to result
lob.on("end", function () {
imageObj = buffer.toString("base64");
// console.log("END " + imageObj);
});
// When the stream closes, resolce the promsie
lob.on("close", function (chunk) {
// console.log("CLOSE PHOTO + RESOLVE");
// Fulfill promise
lobLoadingPhoto.resolve();
});
// Make sure we reject the promise when the stream fails so that we don't have a memory leak
lob.on("error", function (err) {
// executeError(err, res);
// Reject promise
console.log("ERROR " + err);
lobLoadingPhoto.reject();
});
}
if (thumbnailLob) {
// When data comes in from the stream, add it to the buffer
thumbnailLob.on("data", function (chunk) {
thumbnailBufferLength = thumbnailBufferLength + chunk.length;
thumbnailBuffer = Buffer.concat([thumbnailBuffer, chunk], thumbnailBufferLength);
});
//When the data is finished coming in, change it to base 64 and add to result
thumbnailLob.on("end", function () {
thumbObj = thumbnailBuffer.toString("base64");
});
// When the stream closes, resolve the promsie
thumbnailLob.on("close", function (chunk) {
// Fulfill promise
lobLoadingThumb.resolve();
});
// Make sure we reject the promise when the stream fails so that we don't have a memory leak
thumbnailLob.on("error", function () {
// executeError(err, res);
// Reject promise
lobLoadingThumb.reject();
});
}
//When all promises complete, return the results
Q.all([lobLoadingPhoto.promise, lobLoadingThumb.promise]).then(function () {
deferred.resolve({imageObj: imageObj, thumbObj: thumbObj});
}).done();
return deferred.promise;
};
// Sends edits of an image into the database
app.post("/editImage", function(req, res){
var DBQueryString =
"UPDATE images " +
"SET permitted = :permitted, subject = :subject, place = :place, timing = TO_DATE (:timing, 'yyyy/mm/dd'), description = :desc " +
"WHERE photo_id = :photo_id" ,
DBQueryParam = {photo_id: req.query.photo_id,
permitted: req.query.permitted,
subject: req.query.subject,
place: req.query.loc,
timing: req.query.timing,
desc: req.query.desc};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
res.send({success: true, results: result.rows});
}
doRelease(connection);
}
);
});
});
// Retrieves pictures that the user uploaded onto the database
app.post("/getMyPictures", function(req, res){
var DBQueryString =
"SELECT * " +
"FROM images " +
"WHERE images.owner_name = :userName" ,
DBQueryParam = {userName: req.query.userName};
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
function (err, result) {
var imageArr = [];