-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_file_uploads_mme.go
540 lines (475 loc) · 18.2 KB
/
handle_file_uploads_mme.go
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
package emory
import (
"bytes"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/cliffordlab/amoss_services/capacity"
"github.com/cliffordlab/amoss_services/database"
"github.com/cliffordlab/amoss_services/participant"
"github.com/cliffordlab/amoss_services/support/moyo_mom_emory"
"github.com/dgrijalva/jwt-go"
)
const (
defaultMaxMemory = 32 << 20
partialSucess = `{"partial success":"able to upload some data to awsS3Bucket files",
"description":"all files were not able to be upload may be due to empty files"}`
invalidAccessToken = `{"logout user":"Invalid access token."}`
insertVitalsData = `INSERT INTO bp_readings
(created_at, participant_id, systolic_bp, diastolic_bp, pulse, jpg_s3_key, csv_s3_key)
VALUES($1, $2, $3, $4, $5, $6, $7)`
insertSymptomsData = `INSERT INTO mme_symptoms
(created_at, participant_id, blurried_vision, headache, difficulty_breathing, side_pain)
VALUES($1, $2, $3, $4, $5, $6)`
)
type UploadMMEVitalsHandler struct {
Name string
Svc *s3.S3
}
type UploadMMESymptomsHandler struct {
Name string
Svc *s3.S3
}
type ParticipantSymptomsRequest struct {
BV bool
HA bool
DB bool
SP bool
CreatedAt int64
}
func (u UploadMMESymptomsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("Handling symptom data upload to Moyo Mom - Emory..")
contentType := r.Header.Get("Content-type")
log.Println("This is the Content-Type: ")
log.Println(contentType)
err := r.ParseMultipartForm(defaultMaxMemory)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println("This is the error: ")
log.Println(err)
return
}
m := r.Form
var psr ParticipantSymptomsRequest
psr.BV, _ = strconv.ParseBool(m.Get("blurried_vision"))
psr.HA, _ = strconv.ParseBool(m.Get("headache"))
psr.DB, _ = strconv.ParseBool(m.Get("difficulty_breathing"))
psr.SP, _ = strconv.ParseBool(m.Get("side_pain"))
psr.CreatedAt, _ = strconv.ParseInt(m.Get("created_at"), 10, 64)
log.Println("blurried_vision: " + strconv.FormatBool(psr.BV))
log.Println("headache: " + strconv.FormatBool(psr.HA))
log.Println("difficulty_breathing: " + strconv.FormatBool(psr.DB))
log.Println("side_pain: " + strconv.FormatBool(psr.SP))
startOfWeekMillis := r.Header.Get("weekMillis")
if len(startOfWeekMillis) != 12 {
log.Println("token length is wrong")
w.Header().Add("Content-Type", "application/json; charset=UTF-8")
w.Write([]byte("{\"error\": \"invalid header\"}"))
return
}
//validate header is formatted properly
headerValue := r.Header.Get("Authorization")
splitHeaderValue := strings.Split(headerValue, " ")
if splitHeaderValue[0] != "Mars" {
http.Error(w, "Invalid token type", http.StatusUnauthorized)
return
}
bearerToken := splitHeaderValue[1]
token, err := jwt.ParseWithClaims(bearerToken, &capacity.NonAdminClaims{}, func(token *jwt.Token) (interface{}, error) {
// Make sure token's signature wasn't changed
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected siging method")
}
return []byte(capacity.JwtSecret), nil
})
if err != nil {
log.Println("token not valid")
http.Error(w, "Invalid token type", http.StatusUnauthorized)
return
}
var currentParticipant participant.Participant
if claims, ok := token.Claims.(*capacity.NonAdminClaims); ok && token.Valid {
currentParticipant.Study = claims.Study
currentParticipant.ID = claims.ID
} else {
log.Println("token not valid")
http.Error(w, "Invalid token type", http.StatusUnauthorized)
return
}
checkSymptomsThreshold(psr, currentParticipant)
//todo query database to match header token with token in DB and continue
// else return you are already logged in and log them out return 400 unauthorized or forbidden?
// Bearer token or token?
log.Println("Querying db for access token")
query := "SELECT access_token FROM participants WHERE participant_id = $1 AND access_token = $2"
rows, err := database.ADB.Db.Query(query, currentParticipant.ID, bearerToken)
if err != nil {
log.Println("failed to execute get access_token query")
log.Println(err)
http.Error(w, "Invalid access token. Please log user out", http.StatusForbidden)
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte(invalidAccessToken))
return
}
var accessTokenDB string
for rows.Next() {
err = rows.Scan(&accessTokenDB)
if err != nil {
log.Println("failed to scan row for access token")
log.Println(err)
}
}
rows.Close()
log.Println("This is the access token: " + accessTokenDB)
log.Println("This is the bearer token: " + bearerToken)
if accessTokenDB == bearerToken {
log.Println("Access token matches Database token...")
bucket := "awsS3Bucket"
fullUpload := true
err = r.ParseMultipartForm(defaultMaxMemory)
if err != nil {
partialKey := SetPartialKey(currentParticipant, startOfWeekMillis)
log.Printf("{Error: %s, Key: %s}\n", err.Error(), partialKey)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//get a ref to the parsed multipart form
m := r.MultipartForm
//get the *fileheaders
files := m.File["upload"]
for i, f := range files {
//for each fileheader, get a handle to the actual file
filename := files[i].Filename
key := setKey(currentParticipant, startOfWeekMillis, filename)
//file, err := files[i].Open()
file, err := f.Open()
defer file.Close()
if err != nil {
log.Printf("{Error: %s, Key: %s}\n", err.Error(), key)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
uploadResult, err := u.Svc.PutObject(&s3.PutObjectInput{
Bucket: &bucket,
Key: &key,
Body: file,
})
if err != nil {
fullUpload = false
log.Printf("Failed to upload data to %s/%s, %s\n", bucket, key, err.Error())
}
if !fullUpload {
log.Printf("This is the result of the upload: %s\n{Key: %s, Success: partial}\n", uploadResult.GoString(), key)
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte(partialSucess))
} else {
log.Printf("This is the result of the upload: %s\n{Key: %s, Success: full}\n", uploadResult.GoString(), key)
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte("{\"success\": \"you have completed upload to awsS3Bucket\"}"))
}
}
insertSymptomsIntoDB(currentParticipant, psr)
} else {
log.Println("Access token does not match that of the database")
log.Println("Participant_ID: ", currentParticipant.ID)
//http.(w, "Invalid access token. Please log user out", http.StatusConflict)
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte(invalidAccessToken))
return
}
}
func insertSymptomsIntoDB(currentParticipant participant.Participant, psr ParticipantSymptomsRequest) {
log.Println("Inserting symptoms into DB..")
stmt, err := database.ADB.Db.Prepare(insertSymptomsData)
if err != nil {
log.Println("failed to prepare insert symptom statement")
log.Fatalln(err)
}
defer stmt.Close()
rows, err := stmt.Query(psr.CreatedAt, currentParticipant.ID, psr.BV, psr.HA, psr.DB, psr.SP)
if err != nil {
log.Println("failed to execute query statement")
log.Fatalln(err)
}
log.Println("Symptom data inserted successfully into db.")
rows.Close()
}
func checkSymptomsThreshold(psr ParticipantSymptomsRequest, currentParticipant participant.Participant) {
log.Println("Checking symptoms thresholds... ")
//systolic BP >160 mm Hg or diastolic BP>110 mm Hg
patientID := currentParticipant.ID
patientIDString := strconv.FormatInt(patientID, 10)
bvString := strconv.FormatBool(psr.BV)
haString := strconv.FormatBool(psr.HA)
dbString := strconv.FormatBool(psr.DB)
spString := strconv.FormatBool(psr.SP)
if psr.BV == true || psr.HA == true || psr.DB == true || psr.SP == true {
log.Println("Symptoms threshold reached. Attempting to send email...")
moyo_mom_emory.SendSymptomsEmail(aws.String("\n Symptom alert for participant: " + patientIDString + "\n Blurried vision: " + bvString + "\n Head ache: " + haString + "\n Difficulty Breathing: " + dbString + "\n Side Pain: " + spString))
}
}
type ParticipantVitalsRequest struct {
SBP int
DBP int
Pulse int
CreatedAt int64
}
func (uh UploadMMEVitalsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("Handling vital data upload to Moyo Mom - Emory..")
contentType := r.Header.Get("Content-type")
log.Println("This is the Content-Type: ")
log.Println(contentType)
var pvr ParticipantVitalsRequest
err := r.ParseMultipartForm(defaultMaxMemory)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println("This is the error: ")
log.Println(err)
return
}
m := r.Form
pvr.SBP, _ = strconv.Atoi(m.Get("sbp"))
pvr.DBP, _ = strconv.Atoi(m.Get("dbp"))
pvr.Pulse, _ = strconv.Atoi(m.Get("pulse"))
pvr.CreatedAt, _ = strconv.ParseInt(m.Get("created_at"), 10, 64)
log.Println("SBP: " + strconv.Itoa(pvr.SBP))
log.Println("DBP" + strconv.Itoa(pvr.DBP))
log.Println("Pulse" + strconv.Itoa(pvr.Pulse))
startOfWeekMillis := r.Header.Get("weekMillis")
if len(startOfWeekMillis) != 12 {
log.Println("token length is wrong")
w.Header().Add("Content-Type", "application/json; charset=UTF-8")
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte("{\"error\": \"invalid header\"}"))
return
}
//validate header is formatted properly
headerValue := r.Header.Get("Authorization")
splitHeaderValue := strings.Split(headerValue, " ")
if splitHeaderValue[0] != "Mars" {
http.Error(w, "Invalid token type", http.StatusUnauthorized)
return
}
bearerToken := splitHeaderValue[1]
token, err := jwt.ParseWithClaims(bearerToken, &capacity.NonAdminClaims{}, func(token *jwt.Token) (interface{}, error) {
// Make sure token's signature wasn't changed
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected siging method")
}
return []byte(capacity.JwtSecret), nil
})
if err != nil {
log.Println("token not valid")
http.Error(w, "Invalid token type", http.StatusUnauthorized)
return
}
var currentParticipant participant.Participant
if claims, ok := token.Claims.(*capacity.NonAdminClaims); ok && token.Valid {
currentParticipant.Study = claims.Study
currentParticipant.ID = claims.ID
} else {
log.Println("token not valid")
http.Error(w, "Invalid token type", http.StatusUnauthorized)
return
}
checkThreshold(pvr, currentParticipant)
//todo query database to match header token with token in DB and continue
// else return you are already logged in and log them out return 400 unauthorized or forbidden?
// Bearer token or token?
log.Println("Querying db for access token")
query := "SELECT access_token FROM participants WHERE participant_id = $1 AND access_token = $2"
rows, err := database.ADB.Db.Query(query, currentParticipant.ID, bearerToken)
if err != nil {
log.Println("failed to execute get access_token query")
log.Println(err)
http.Error(w, "Invalid access token. Please log user out", http.StatusForbidden)
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte(invalidAccessToken))
return
}
var accessTokenDB string
for rows.Next() {
err = rows.Scan(&accessTokenDB)
if err != nil {
log.Println("failed to scan row for access token")
log.Println(err)
}
}
rows.Close()
log.Println("This is the access token: " + accessTokenDB)
log.Println("This is the bearer token: " + bearerToken)
if accessTokenDB == bearerToken {
bucket := "awsS3Bucket"
fullUpload := true
err = r.ParseMultipartForm(defaultMaxMemory)
if err != nil {
partialKey := SetPartialKey(currentParticipant, startOfWeekMillis)
log.Printf("{Error: %s, Key: %s}\n", err.Error(), partialKey)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
csvFilename := strconv.FormatInt(currentParticipant.ID, 10) + "_" + strconv.FormatInt(pvr.CreatedAt, 10) + "_bp.csv"
csvS3Key := setKey(currentParticipant, startOfWeekMillis, csvFilename)
fullUpload = uh.uploadCSV(csvFilename, pvr, bucket, fullUpload, csvS3Key)
fullUpload, done := uh.uploadJPEG(w, r, currentParticipant, startOfWeekMillis, bucket, fullUpload)
if done {
return
}
if !fullUpload {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte(partialSucess))
} else {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte("{\"success\": \"you have completed upload to awsS3Bucket/moyo-mom-emory/\"}"))
}
insertVitalsToDB(currentParticipant, mMEJPEGS3Key, csvS3Key, pvr)
} else {
log.Println("Access token does not match that of the database")
log.Println("Participant_ID: ", currentParticipant.ID)
//http.(w, "Invalid access token. Please log user out", http.StatusConflict)
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte(invalidAccessToken))
return
}
}
var mMEJPEGS3Key string
func checkThreshold(pvr ParticipantVitalsRequest, currentParticipant participant.Participant) {
log.Println("Checking vital thresholds... ")
//systolic BP >160 mm Hg or diastolic BP>110 mm Hg
patientID := currentParticipant.ID
patientIDString := strconv.FormatInt(patientID, 10)
sbpString := strconv.Itoa(pvr.SBP)
dbpString := strconv.Itoa(pvr.DBP)
pulseString := strconv.Itoa(pvr.Pulse)
if pvr.SBP > 160 || pvr.DBP > 110 {
log.Println("Vital threshold reached. Attempting to send email...")
moyo_mom_emory.SendVitalEmail(aws.String("Threshold reached for participant: " + patientIDString + " SDP: " + sbpString + " DBP: " + dbpString + " Pulse: " + pulseString))
}
}
func (uh UploadMMEVitalsHandler) uploadJPEG(w http.ResponseWriter, r *http.Request, currentParticipant participant.Participant, startOfWeekMillis string, bucket string, fullUpload bool) (bool, bool) {
log.Println("Uploading JPEG to S3...")
m := r.MultipartForm
//get the *fileheaders
files := m.File["upload"]
for i, f := range files {
//for each fileheader, get a handle to the actual file
filename := files[i].Filename
mMEJPEGS3Key = setKey(currentParticipant, startOfWeekMillis, filename)
log.Println("this is the key: ")
log.Println(mMEJPEGS3Key)
//file, err := files[i].Open()
file, err := f.Open()
defer file.Close()
if err != nil {
log.Printf("{Error: %s, Key: %s}\n", err.Error(), mMEJPEGS3Key)
http.Error(w, err.Error(), http.StatusInternalServerError)
return false, true
}
uploadResult, err := uh.Svc.PutObject(&s3.PutObjectInput{
Bucket: &bucket,
Key: &mMEJPEGS3Key,
Body: file,
})
if err != nil {
fullUpload = false
log.Printf("Failed to upload data to %s/%s, %s\n", bucket, mMEJPEGS3Key, err.Error())
}
if !fullUpload {
log.Printf("This is the result of the upload: %s\n{Key: %s, Success: partial}\n", uploadResult.GoString(), mMEJPEGS3Key)
} else {
log.Printf("This is the result of the upload: %s\n{Key: %s, Success: full}\n", uploadResult.GoString(), mMEJPEGS3Key)
}
}
return fullUpload, false
}
func (uh UploadMMEVitalsHandler) uploadCSV(csvFilename string, pvr ParticipantVitalsRequest, bucket string, fullUpload bool, s3key string) bool {
log.Println("Writing new CSV file to upload to ...")
log.Println("SBP: " + strconv.Itoa(pvr.SBP))
log.Println("DBP" + strconv.Itoa(pvr.DBP))
log.Println("Pulse" + strconv.Itoa(pvr.Pulse))
// init byte buffer var
var bb bytes.Buffer
bb.Write([]byte("SBP: " + strconv.Itoa(pvr.SBP) + ", "))
bb.Write([]byte("DBP: " + strconv.Itoa(pvr.DBP) + ", "))
bb.Write([]byte("Pulse: " + strconv.Itoa(pvr.Pulse) + ", "))
reader := bytes.NewReader(bb.Bytes())
log.Println("Uploading new csv File... ")
uploadResult, err := uh.Svc.PutObject(&s3.PutObjectInput{
Body: reader,
Bucket: &bucket,
Key: &s3key,
})
//file, err := os.Create(csvFilename)
//if err != nil {
// log.Fatalf("failed creating file: %s", err)
//}
//defer file.Close()
//
//writer := csv.NewWriter(file)
//defer writer.Flush()
//bpData := [][]string{
// {"sbp", "dbp", "pulse"},
// {strconv.Itoa(pvr.SBP), strconv.Itoa(pvr.DBP), strconv.Itoa(pvr.Pulse)},
//}
//
//for _, value := range bpData {
// err := writer.Write(value)
// if err != nil {
// log.Fatalf("failed writing to file: %s", err)
// }
//}
//uploadResult, err := uh.Svc.PutObject(&s3.PutObjectInput{
// Bucket: &bucket,
// Key: &s3key,
// Body: file,
//})
if err != nil {
fullUpload = false
log.Printf("Failed to upload data to %s/%s, %s\n", bucket, s3key, err.Error())
}
if !fullUpload {
log.Printf("This is the result of the upload: %s\n{Key: %s, Success: partial}\n", uploadResult.GoString(), s3key)
} else {
// Removing file from the directory
// Using Remove() function
//e := os.Remove(csvFilename)
//if e != nil {
// log.Fatal(e)
//}
log.Printf("This is the result of the upload: %s\n{Key: %s, Success: full}\n", uploadResult.GoString(), s3key)
}
return fullUpload
}
func insertVitalsToDB(currentParticipant participant.Participant, jpgS3Key string, csvS3Key string, pvr ParticipantVitalsRequest) {
log.Println("Inserting s3Key into DB..")
log.Println("pvr.SBP: " + strconv.Itoa(pvr.SBP))
log.Println("pvr.DBP: " + strconv.Itoa(pvr.DBP))
log.Println("pvr.Pulse: " + strconv.Itoa(pvr.Pulse))
stmt, err := database.ADB.Db.Prepare(insertVitalsData)
if err != nil {
log.Println("failed to prepare insert s3 key statement")
log.Fatalln(err)
}
defer stmt.Close()
rows, err := stmt.Query(pvr.CreatedAt, currentParticipant.ID, pvr.SBP, pvr.DBP, pvr.Pulse, jpgS3Key, csvS3Key)
if err != nil {
log.Println("failed to execute query statement")
log.Fatalln(err)
}
log.Println("S3 Key inserted successfully into db.")
rows.Close()
}
func SetPartialKey(currentParticipant participant.Participant, startOfWeekMillis string) string {
var partialKey string
partialKey = "test/" + currentParticipant.Study + "/" + strconv.FormatInt(currentParticipant.ID, 10) + "/" + startOfWeekMillis
return partialKey
}
func setKey(currentParticipant participant.Participant, startOfWeekMillis string, filename string) string {
var key string
key = "test/" + currentParticipant.Study + "/" + strconv.FormatInt(currentParticipant.ID, 10) + "/" + startOfWeekMillis + "/" + filename
return key
}