-
Notifications
You must be signed in to change notification settings - Fork 342
/
plugin_util.go
168 lines (151 loc) · 3.69 KB
/
plugin_util.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
package plugins
import (
"crypto/tls"
"github.com/jordan-wright/email"
jsoniter "github.com/json-iterator/go"
"github.com/tickstep/bolt"
"github.com/tickstep/library-go/logger"
"github.com/tickstep/library-go/requester"
"net/smtp"
"os"
"strings"
"sync"
"time"
)
type (
PersistenceItem struct {
Key string `json:"key"`
Value string `json:"value"`
Type string `json:"type"`
}
)
var (
locker = &sync.Mutex{}
PersistenceFilePath = ""
)
// HttpGet Http的get请求
func HttpGet(header map[string]string, url string) string {
client := requester.NewHTTPClient()
body, err := client.Fetch("GET", url, nil, header)
if err != nil {
logger.Verboseln("js HttpJsonGet error ", err)
return ""
}
return string(body)
}
// HttpPost Http的post请求
func HttpPost(header map[string]string, url string, data interface{}) string {
client := requester.NewHTTPClient()
body, err := client.Fetch("POST", url, data, header)
if err != nil {
logger.Verboseln("js HttpJsonPost error ", err)
return ""
}
return string(body)
}
// DeleteLocalFile 删除本地文件,不支持文件夹
func DeleteLocalFile(localFilePath string) bool {
err := os.Remove(localFilePath)
if err != nil {
// 删除失败
return false
} else {
// 删除成功
return true
}
return false
}
// SendTextMail 发送文本邮件
func SendTextMail(mailServer, userName, password, to, subject, body string) bool {
if e := sendEmail(mailServer, userName, password, to, subject, body, "text", true); e != nil {
logger.Verboseln("js SendTextMail error ", e)
return false
}
return true
}
// SendHtmlMail 发送HTML富文本邮件
func SendHtmlMail(mailServer, userName, password, to, subject, body string) bool {
if e := sendEmail(mailServer, userName, password, to, subject, body, "html", true); e != nil {
logger.Verboseln("js SendHtmlMail error ", e)
return false
}
return true
}
func sendEmail(mailServer, userName, password, to, subject, body, mailType string, useSsl bool) error {
mailServerHost := strings.Split(mailServer, ":")[0]
auth := smtp.PlainAuth("", userName, password, mailServerHost)
e := email.NewEmail()
e.From = userName
e.To = []string{to}
e.Subject = subject
if mailType == "html" {
e.HTML = []byte(body)
} else {
e.Text = []byte(body)
}
if useSsl {
return e.SendWithStartTLS(mailServer, auth, &tls.Config{ServerName: mailServerHost, InsecureSkipVerify: true})
} else {
return e.Send(mailServer, auth)
}
}
// GetString 获取值
func GetString(key string) string {
locker.Lock()
defer locker.Unlock()
db, err := bolt.Open(PersistenceFilePath, 0755, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
return ""
}
defer db.Close()
tx, err := db.Begin(false)
if err != nil {
return ""
}
bkt := tx.Bucket([]byte("/"))
if bkt == nil {
return ""
}
data := bkt.Get([]byte(key))
item := PersistenceItem{}
if e := jsoniter.Unmarshal(data, &item); e == nil {
return item.Value
}
return ""
}
// PutString 存储KV键值对
func PutString(key, value string) bool {
locker.Lock()
defer locker.Unlock()
db, err := bolt.Open(PersistenceFilePath, 0755, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
return false
}
defer db.Close()
// Start a writable transaction.
tx, err := db.Begin(true)
if err != nil {
return false
}
defer tx.Rollback()
rootBucket, er := tx.CreateBucketIfNotExists([]byte("/"))
if er != nil {
return false
}
data, ee := jsoniter.Marshal(&PersistenceItem{
Key: key,
Value: value,
Type: "string",
})
if ee != nil {
return false
}
if e := rootBucket.Put([]byte(key), data); e != nil {
return false
}
// Commit the transaction and check for error.
if err := tx.Commit(); err != nil {
return false
}
return true
}