-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
121 lines (93 loc) · 2.71 KB
/
handler.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
'use strict';
let fs = require('fs');
let AWS = require('aws-sdk');
let rekognition = new AWS.Rekognition();
let s3 = new AWS.S3();
const image_bucket_name = "secrethotdogbucket";
module.exports.home = (event, context, callback) => {
fs.readFile(__dirname + '/index.html', 'utf8', function (err,data) {
if (err) {
console.log(err);
callback(null, {
statusCode: 500, body: 'It broke'
})
}
callback(null, {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*'
},
body: data
})
});
};
module.exports.patreon = (event, context, callback) => {
console.log(event);
let crypto = require('crypto');
const patreonSecret = process.env.PATREON_SECRET;
let hash = event.headers['X-Patreon-Signature'],
hmac = crypto.createHmac("md5", patreonSecret);
hmac.update(event.body);
let crypted = hmac.digest("hex");
if (crypted === hash) {
console.log("That's good hash! " + hash);
} else {
console.log("Bad hash! " + hash + " crypted " + crypted);
return callback(null, {
statusCode: 403,
body: "Bad Hash!"
})
}
//we got a good patreon webhook here
callback(null, {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*'
},
body: "Thanks, Patreon!"
})
};
module.exports.requestUploadURL = (event, context, callback) => {
let params = JSON.parse(event.body);
let uploadURL = s3.getSignedUrl('putObject', {
Bucket: image_bucket_name,
Key: params.name,
ContentType: params.type,
ACL: 'public-read',
});
callback(null, {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({uploadURL: uploadURL}),
})
};
module.exports.detectHotDog = (event, context, callback) => {
console.log(event);
let params = JSON.parse(event.body);
let rekogParams = {
Image: {
S3Object: {
Bucket: image_bucket_name,
Name: params.name
}
}
};
rekognition.detectLabels(rekogParams, function (err, data) {
const response = {
statusCode: 200,
body: null,
};
if (err) {
response.statusCode = 500;
console.log("REKOG ERROR:", err);
} else {
console.log(data);
response.body = JSON.stringify(data);
}
callback(null, response);
});
};