mirror of
https://gitee.com/zhangsken/aliyunpan.git
synced 2026-10-16 02:23:49 +08:00
add kv dictionary for plugin
This commit is contained in:
@@ -1158,6 +1158,33 @@ var html = "<html>"
|
||||
PluginUtil.Email.sendHtmlMail("smtp.qq.com:465", "123xxx@qq.com", "pwdxxxxxx", "12545xxx@qq.com", "文件上传通知", html);
|
||||
```
|
||||
|
||||
7.PluginUtil.KV.putString()
|
||||
存储键值对,定义如下
|
||||
```
|
||||
PluginUtil.KV.putString(key, value)
|
||||
|
||||
其中:
|
||||
key - 键,字符串
|
||||
value - 值,字符串
|
||||
```
|
||||
样例
|
||||
```js
|
||||
PluginUtil.KV.putString("mykey", "1670419352");
|
||||
```
|
||||
|
||||
8.PluginUtil.KV.getString()
|
||||
获取存储的键值,指定对应的键,返回存储的值,如果没有则返回空字符串,定义如下
|
||||
```
|
||||
PluginUtil.KV.getString(key)
|
||||
|
||||
其中:
|
||||
key - 键,字符串
|
||||
```
|
||||
样例
|
||||
```js
|
||||
var value = PluginUtil.KV.getString("mykey");
|
||||
```
|
||||
|
||||
### 常见场景样例
|
||||
这里收集了一些常见的需求样例,可以作为插件定制的样例模板。
|
||||
|
||||
|
||||
@@ -323,6 +323,11 @@ func GetPluginDir() string {
|
||||
return strings.TrimSuffix(GetConfigDir(), "/") + "/plugin"
|
||||
}
|
||||
|
||||
// GetPluginKvFile 获取插件KV键值对存储文件
|
||||
func GetPluginKvFile() string {
|
||||
return GetPluginDir() + "/kv.bolt"
|
||||
}
|
||||
|
||||
// GetSyncDriveDir 获取同步备份的文件夹路径
|
||||
func GetSyncDriveDir() string {
|
||||
return strings.TrimSuffix(GetConfigDir(), "/") + "/sync_drive"
|
||||
|
||||
@@ -3,6 +3,7 @@ package plugins
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/tickstep/aliyunpan/internal/config"
|
||||
"github.com/tickstep/library-go/logger"
|
||||
"strings"
|
||||
)
|
||||
@@ -61,6 +62,13 @@ func (js *JsPlugin) Start() error {
|
||||
emailObj.Set("sendTextMail", SendTextMail) // PluginUtil.Email.sendTextMail()
|
||||
emailObj.Set("sendHtmlMail", SendHtmlMail) // PluginUtil.Email.sendHtmlMail()
|
||||
|
||||
// PluginUtil.KV
|
||||
PersistenceFilePath = config.GetPluginKvFile()
|
||||
kvObj := js.vm.NewObject()
|
||||
pluginObj.Set("KV", kvObj)
|
||||
kvObj.Set("getString", GetString) // PluginUtil.KV.getString()
|
||||
kvObj.Set("putString", PutString) // PluginUtil.KV.putString()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,28 @@ 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请求
|
||||
@@ -83,3 +100,69 @@ func sendEmail(mailServer, userName, password, to, subject, body, mailType strin
|
||||
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
|
||||
}
|
||||
|
||||
@@ -12,3 +12,14 @@ func TestDeleteLocalFile(t *testing.T) {
|
||||
func TestSendEmail(t *testing.T) {
|
||||
fmt.Println(sendEmail("smtp.qq.com:465", "111xxx@qq.com", "xxxxxx", "12545xxx@qq.com", "title", "hello", "text", true))
|
||||
}
|
||||
|
||||
func TestPutString(t *testing.T) {
|
||||
PersistenceFilePath = "/Volumes/Downloads/kv.bolt"
|
||||
PutString("test1", "ok1234-new")
|
||||
}
|
||||
|
||||
func TestGetString(t *testing.T) {
|
||||
PersistenceFilePath = "/Volumes/Downloads/kv.bolt"
|
||||
v := GetString("test1")
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user