-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
144 lines (118 loc) · 4.41 KB
/
api.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
package friendmail
import (
"bytes"
"encoding/base64"
"time"
"github.com/dchest/captcha"
"github.com/ottemo/commerce/api"
"github.com/ottemo/commerce/app"
"github.com/ottemo/commerce/db"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
)
// setupAPI setups package related API endpoint routines
func setupAPI() error {
service := api.GetRestService()
service.POST("friend/email", APIFriendEmail)
service.GET("friend/captcha", APIFriendCaptcha)
return nil
}
// APIFriendCaptcha will generate a captcha for use in a form
func APIFriendCaptcha(context api.InterfaceApplicationContext) (interface{}, error) {
var captchaDigits []byte
var captchaValue string
var result interface{}
captchaValuesMutex.Lock()
if len(captchaValues) < ConstMaxCaptchaItems {
captchaDigits = captcha.RandomDigits(captcha.DefaultLen)
for i := range captchaDigits {
captchaValue += string(captchaDigits[i] + '0')
}
} else {
for key := range captchaValues {
captchaValue = key
break
}
captchaDigits = make([]byte, len(captchaValue))
for idx, digit := range []byte(captchaValue) {
captchaDigits[idx] = digit - '0'
}
}
captchaValues[captchaValue] = time.Now()
captchaValuesMutex.Unlock()
// generate a captcha image
image := captcha.NewImage(captchaValue, captchaDigits, captcha.StdWidth, captcha.StdHeight)
if image == nil {
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "7224c8fe-6079-4bb3-9dc3-ad0847db8e29", "Unable to generate a captcha image.")
}
if context.GetRequestArguments()["json"] != "" {
buffer := new(bytes.Buffer)
buffer.WriteString("data:image/png;base64,")
if _, err := image.WriteTo(base64.NewEncoder(base64.StdEncoding, buffer)); err != nil {
_ = env.ErrorNew(ConstErrorModule, ConstErrorLevel, "d56c1218-1d04-4e67-a389-048bf2e0a9be", err.Error())
}
result = map[string]interface{}{
"captcha": buffer.String(),
}
} else {
if err := context.SetResponseContentType("image/png"); err != nil {
_ = env.ErrorNew(ConstErrorModule, ConstErrorLevel, "2490dcbc-58e3-4243-a9d5-cb0490810281", err.Error())
}
if _, err := image.WriteTo(context.GetResponseWriter()); err != nil {
_ = env.ErrorNew(ConstErrorModule, ConstErrorLevel, "adfabaa6-7ad2-4e01-b8bd-897754711bac", err.Error())
}
}
return result, nil
}
// APIFriendEmail sends an email to a friend
func APIFriendEmail(context api.InterfaceApplicationContext) (interface{}, error) {
var friendEmail string
// checking request values
requestData, err := api.GetRequestContentAsMap(context)
if err != nil {
return nil, env.ErrorDispatch(err)
}
if !utils.KeysInMapAndNotBlank(requestData, "captcha", "friend_email") {
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "3c3d0918-b951-43b7-943c-54e8571d0c32", "'captcha' and/or 'friend_email' fields are not specified or blank")
}
friendEmail = utils.InterfaceToString(requestData["friend_email"])
if !utils.ValidEmailAddress(friendEmail) {
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "5821734e-4c84-449b-9f75-fd1154623c42", "The email address, "+friendEmail+", is not in valid format.")
}
// checking captcha
captchaValue := utils.InterfaceToString(requestData["captcha"])
captchaValuesMutex.Lock()
if _, present := captchaValues[captchaValue]; !present {
captchaValuesMutex.Unlock()
return nil, env.ErrorNew(ConstErrorModule, env.ConstErrorLevelAPI, "8bd3ad79-e464-4355-8a13-27ff55980fbb", "The entered character sequence does not match the captcha value.")
}
captchaStore.Get(captchaValue, true)
delete(captchaValues, captchaValue)
captchaValuesMutex.Unlock()
// sending an e-mail
emailSubject := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathFriendMailEmailSubject))
emailTemplate := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathFriendMailEmailTemplate))
emailTemplate, err = utils.TextTemplate(emailTemplate, requestData)
if err != nil {
return nil, env.ErrorDispatch(err)
}
err = app.SendMail(friendEmail, emailSubject, emailTemplate)
if err != nil {
return nil, env.ErrorDispatch(err)
}
// storing data to database
saveData := map[string]interface{}{
"date": time.Now(),
"email": friendEmail,
"data": requestData,
}
dbCollection, err := db.GetCollection(ConstCollectionNameFriendMail)
if err != nil {
return nil, env.ErrorDispatch(err)
}
_, err = dbCollection.Save(saveData)
if err != nil {
return nil, env.ErrorDispatch(err)
}
return "ok", nil
}