-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
180 lines (131 loc) · 4.79 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Load Node Dependencies
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
var formidable = require('formidable');
// Load Node Modules
var path = require('path');
var fs = require('fs');
var uuid = require('node-uuid');
//Local Modules
var bucket = require('./bucket');
var ml = require('./ml');
var sl = require('./sl');
var leo = require('./leo');
var fb = require('./fb');
var config = require('./config.json');
//Initialization
//Load Credentials and Region
AWS.config.update(config.AWS.Credentials);
// Create an AWS clients (for file management)
var s3 = new AWS.S3({apiVersion: '2006-03-01'});
var rek = new AWS.Rekognition({apiVersion: '2016-06-27'})
//Configure Express
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use("/imgs", express.static(config.SmartShop.imgDir));
// Updates SAP Leonardo Vectors DB
leo.UpdateVectorsBase();
// Main html page
/** Endpoints */
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'views/index.html'));
});
app.get(path.join(config.SmartShop.imgDir,':img'), function (req, res) {
res.sendFile(filepath);
});
// Facebook Services //
//Facebook picture retriever
app.post('/fb/trainLeoWithUserPics', function(req, res){
//Call function to retrieve the profile array of pictures
fb.GetUserProfilePictures(req.body.accessToken, function(fbData){
var bucketName = config.SmartShop.namespace+"-"+req.body.user+"-"+uuid.v4();
bucket.create(s3, req.body.user, bucketName, fbData.pics, rek);
res.send("All good");
});
});
// Service Layer Services //
// Login started by external apps or managed internally?
app.get('/sl/Connect', function(req, res){
sl.Connect(res);
console.log('Connect SL started');
});
// Create Draft Order
app.post('/sl/CreateDraftOrder', function(req, res){
var draft = JSON.stringify(req.body);
sl.CreateDraftOrder(draft, res);
console.log('CreateDraftOrder ');
});
// Get last Order Draft for a specific customer - bpCode as query string (if empty default value taken)
app.get('/sl/GetDraftOrder', function(req, res){
var bpCode = req.query.bpCode;
sl.GetDraftOrder(bpCode, res);
console.log('Got bpCode from QUERY ' + bpCode);
});
// Create Order from a Draft DocEntry and Lines details
app.post('/sl/CreateOrderFromDraft', function(req, res){
var input = JSON.stringify(req.body);
sl.CreateOrderFromDraft(input, res);
console.log('CreateOrderFromDraft');
});
// Face Recognition Services //
// Receives a Json with user + Pictures array
app.post('/trainSystem', function(req, res){
//Store the images on the S3 Bucket, then add them to the collection
var bucketName = config.SmartShop.namespace+"-"+req.body.user+"-"+uuid.v4();
bucket.create(s3, req.body.user, bucketName, req.body.pics, rek)
res.send({msg:'All Good!'});
});
// Identifies the user of a given image
app.post('/searchFace', function(req, res){
var output = {}
console.log("Call Received");
//Store image on the defaulFacesBucket
console.dir(req.body);
bucket.put(s3, config.AWS.S3.faceBucket,'xxx', req.body.pics,null, function(ret){
ml.searchFaces(rek, config.AWS.S3.faceBucket, ret.Key, function(err, data){
if (err){
output = err;
res.send(output);
}
if (data && data.FaceMatches.length > 0){
data = data.FaceMatches;
var extImgId = '';
extImgId = data[0].Face.ExternalImageId;
output.user = extImgId.substring(0,extImgId.indexOf('-'));
output.Similarity = data[0].Similarity
res.send(output);
}else{
output = data;
res.send(output);
}
console.log(output);
})
});
});
app.post('/initialize', function(req,res){
//Initialize all the system
if (req.body.collections){
ml.deleteCollections(rek, function(data){
//Create a collection on Rekognition for the give user
ml.createCollection(rek,config.AWS.Rekognition.faceCollection)
});
}
if(req.body.buckets){
bucket.deleteBuckets(s3, function(){
bucket.create(s3, null, config.AWS.S3.faceBucket, null, null);
});
}
});
// SAP Leonardo Services //
app.post('/GetSimilarItems', function(req, res){
leo.GetSimilarItems(req, function(body){
res.send(body);
});
console.log('GetSimilarItems')
});
var server = app.listen(config.SmartShop.serverPort, function(){
console.log('Server listening on port '+config.SmartShop.serverPort);
});