-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
162 lines (153 loc) · 5.26 KB
/
index.coffee
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
express = require('express')
bodyParser = require('body-parser')
crypto = require('crypto')
request = require('request')
fs = require 'fs'
path = require("path")
temp_dir = path.join(process.cwd(), 'temp/')
fs.mkdirSync(temp_dir) if (!fs.existsSync(temp_dir))
RequestHelper = require('./RequestHelper')
Client = require('./Client')
ResponseHandler = require('./ResponseHandler')
Contact = require('./Contact')
evercontact = (content, callback)->
requestHelper = (new RequestHelper).withSubject('FAKE TITLE').withHeaderFrom('contact@sharinpix.com').withHeaderTo('contact@sharinpix.com').withContent(content)
client = Client.getDefault('sharinpix', 'F9PbSWzTpNjA')
client.execute requestHelper.build(), (responseHandler) ->
console.log 'EVERCONTACT REPLIED !:'
if responseHandler.status == ResponseHandler.ResponseStatus.SUCCEED and responseHandler.hasDetectedContact()
detectedContact = responseHandler.detectedContact
console.log 'We have a Contact: ' + detectedContact.firstName + '\n'
console.log 'VCard: ' + responseHandler.detectedContactAsVCard + '\n'
callback(detectedContact)
console.log detectedContact.debugInfo()
else
console.log 'EVERCONTACT ERROR !'
console.log responseHandler.errors
callback({})
return
app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.set('port', (process.env.PORT || 5000))
signature = (content)->
'sha1='+crypto
.createHmac('sha1', process.env.SECRET)
.update(content)
.digest('hex')
app.get '/', (req, res)->
res.send('SharinPix Example Provider')
res.status(200)
app.post '/', (req, res)->
if req.headers['x-sharinpix-signature'] != signature(req.body.p)
console.log 'Wrong secret !'
res.status(400)
res.send('Wrong secret')
return
res.send('OK')
res.status(200)
payload = JSON.parse(req.body.p)
console.log 'PAYLOAD :'
console.log payload
console.log "Callack : #{payload.callback}"
# request {
# method: 'GET',
# url: payload.image.large
# }, (error, response, body)->
# console.log 'Image downloaded !'
# # console.log 'data:image/jpeg;base64,'+(new Buffer(body).toString('base64'))
# request.post(
# {
# url: 'http://evercontact-ocr.herokuapp.com/ocr/process/test.json',
# headers: {
# 'Content-Type': 'application/x-www-form-urlencoded',
# 'Authorization': 'Basic b2NyY2FyZHJlYWRlcjpvY3JjYXJkcmVhZGVy'
# },
# # form: { img: 'data:image/jpeg;base64,'+(new Buffer(body).toString('base64')) }
# form: { img: (new Buffer(body).toString('base64')) }
# },
# (err, response, body)->
# console.log 'Response', err, body
# )
console.log "Callack : #{payload.callback}"
filename = path.join(temp_dir, "image#{Math.round(Math.random()*100000)}.jpg")
console.log filename
ws = fs.createWriteStream(filename)
ws.on 'finish', ->
fs.readFile filename, (err, content)->
console.log 'Finished !' + content.toString('base64').length
request.post(
{
url: 'http://evercontact-ocr.herokuapp.com/ocr/process/test.json',
json: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic b2NyY2FyZHJlYWRlcjpvY3JjYXJkcmVhZGVy'
},
form: {
img: content.toString('base64')
}
}, (err, response, body)->
console.log 'response', payload.callback, body
evercontact body.original, (contact)->
console.log 'Response from evercontact : ' + contact
request({
url: payload.callback,
method: 'post'
json: true
body: {
payload: {
ocr: body.original,
contact: contact,
}
},
}, (error, response, body)->
console.log error
console.log body
)
)
# request.post(
# {
# url: 'http://evercontact-ocr.herokuapp.com/ocr/process/test.json',
# headers: {
# 'Content-Type': 'application/x-www-form-urlencoded',
# 'Authorization': 'Basic b2NyY2FyZHJlYWRlcjpvY3JjYXJkcmVhZGVy'
# },
# form: {
# # jimg: 'Test? '
# img: fs.createReadStream(filename).read()
# }
# }, (err, response, body)->
# console.log 'POSTED !', err, body
# request({
# url: payload.callback,
# method: 'post'
# json: true
# body: {
# img: response.body
# },
# }, (error, response, body)->
# console.log error
# console.log body
# )
# )
request.get(payload.image.original).pipe(ws)
# request({
# method: 'POST',
# json: true,
# }, (error, response, body)->
# request({
# url: payload.callback,
# method: 'post'
# json: true
# body: {
# payload: response.body
# },
# }, (error, response, body)->
# console.log error
# console.log body
# )
# )
#
app.listen app.get('port'), ->
console.log("Node app is running at localhost:" + app.get('port'))
console.log "Secret : #{process.env.SECRET}"