aliyunpan/internal/plugins/plugin_util.go

169 lines
3.7 KiB
Go
Raw Normal View History

2022-04-20 14:14:18 +08:00
package plugins
2022-04-19 20:23:17 +08:00
import (
2022-12-06 20:40:19 +08:00
"crypto/tls"
"github.com/jordan-wright/email"
2022-12-07 21:31:33 +08:00
jsoniter "github.com/json-iterator/go"
"github.com/tickstep/bolt"
2022-04-19 20:23:17 +08:00
"github.com/tickstep/library-go/logger"
"github.com/tickstep/library-go/requester"
2022-12-06 20:40:19 +08:00
"net/smtp"
"os"
2022-12-06 20:40:19 +08:00
"strings"
2022-12-07 21:31:33 +08:00
"sync"
"time"
)
type (
PersistenceItem struct {
Key string `json:"key"`
Value string `json:"value"`
Type string `json:"type"`
}
)
var (
locker = &sync.Mutex{}
PersistenceFilePath = ""
2022-04-19 20:23:17 +08:00
)
// 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
}
2022-12-06 20:40:19 +08:00
2022-12-07 10:34:46 +08:00
// 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
}
2022-12-06 20:40:19 +08:00
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)
}
}
2022-12-07 21:31:33 +08:00
// 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
}