aliyunpan/internal/plugins/js_plugin.go

215 lines
5.8 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 (
"fmt"
"github.com/dop251/goja"
"github.com/tickstep/library-go/logger"
2022-04-20 09:49:16 +08:00
"strings"
2022-04-19 20:23:17 +08:00
)
type (
JsPlugin struct {
2022-04-20 14:14:18 +08:00
Name string
vm *goja.Runtime
2022-04-19 20:23:17 +08:00
}
)
2022-04-20 14:14:18 +08:00
func NewJsPlugin() *JsPlugin {
2022-04-19 20:23:17 +08:00
return &JsPlugin{
2022-04-20 14:14:18 +08:00
Name: "JsPlugin",
vm: nil,
2022-04-19 20:23:17 +08:00
}
}
// jsLog 支持js中的console.log方法
func jsLog(call goja.FunctionCall) goja.Value {
str := call.Argument(0)
2022-04-20 09:49:16 +08:00
buf := &strings.Builder{}
2022-04-22 15:23:07 +08:00
fmt.Fprintf(buf, "JAVASCRIPT: %+v", str.Export())
2022-04-20 09:49:16 +08:00
logger.Verboseln(buf.String())
2022-04-19 20:23:17 +08:00
return str
}
func (js *JsPlugin) Start() error {
js.Name = "JsPlugin"
js.vm = goja.New()
js.vm.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
// 内置log
console := js.vm.NewObject()
console.Set("log", jsLog)
js.vm.Set("console", console) // console.log()
2022-04-19 20:23:17 +08:00
2022-04-29 22:10:09 +08:00
// 内置系统函数PluginUtil
pluginObj := js.vm.NewObject()
js.vm.Set("PluginUtil", pluginObj)
// PluginUtil.Http
2022-04-29 22:10:09 +08:00
httpObj := js.vm.NewObject()
pluginObj.Set("Http", httpObj)
httpObj.Set("get", HttpGet) // PluginUtil.Http.get()
httpObj.Set("post", HttpPost) // PluginUtil.Http.post()
// PluginUtil.LocalFS
localFS := js.vm.NewObject()
pluginObj.Set("LocalFS", localFS)
localFS.Set("deleteFile", DeleteLocalFile) // PluginUtil.LocalFS.deleteFile()
2022-04-19 20:23:17 +08:00
return nil
}
// LoadScript 加载脚本
func (js *JsPlugin) LoadScript(script string) error {
_, err := js.vm.RunString(script)
if err != nil {
2022-04-20 21:56:47 +08:00
logger.Verboseln("JS代码有问题{}", err)
2022-04-19 20:23:17 +08:00
return err
}
return nil
}
2022-04-20 22:05:50 +08:00
func (js *JsPlugin) isHandlerFuncExisted(fnName string) bool {
ret := js.vm.Get(fnName)
if ret != nil {
return true
}
return false
}
2022-04-20 14:14:18 +08:00
// UploadFilePrepareCallback 上传文件前的回调函数
2022-04-19 20:23:17 +08:00
func (js *JsPlugin) UploadFilePrepareCallback(context *Context, params *UploadFilePrepareParams) (*UploadFilePrepareResult, error) {
var fn func(*Context, *UploadFilePrepareParams) (*UploadFilePrepareResult, error)
2022-04-20 22:05:50 +08:00
if !js.isHandlerFuncExisted("uploadFilePrepareCallback") {
return nil, nil
}
2022-04-19 20:23:17 +08:00
err := js.vm.ExportTo(js.vm.Get("uploadFilePrepareCallback"), &fn)
if err != nil {
2022-04-20 14:14:18 +08:00
logger.Verboseln("Js函数映射到 Go 函数失败!")
2022-04-19 20:23:17 +08:00
return nil, nil
}
r, er := fn(context, params)
if er != nil {
2022-04-20 14:14:18 +08:00
logger.Verboseln(er)
return nil, er
2022-04-19 20:23:17 +08:00
}
return r, nil
}
2022-04-21 16:53:08 +08:00
// UploadFileFinishCallback 上传文件结束的回调函数
2022-04-20 23:16:04 +08:00
func (js *JsPlugin) UploadFileFinishCallback(context *Context, params *UploadFileFinishParams) error {
var fn func(*Context, *UploadFileFinishParams) error
if !js.isHandlerFuncExisted("uploadFileFinishCallback") {
return nil
}
err := js.vm.ExportTo(js.vm.Get("uploadFileFinishCallback"), &fn)
if err != nil {
logger.Verboseln("Js函数映射到 Go 函数失败!")
return nil
}
er := fn(context, params)
if er != nil {
logger.Verboseln(er)
return nil
}
return nil
}
2022-04-21 16:30:12 +08:00
// DownloadFilePrepareCallback 下载文件前的回调函数
func (js *JsPlugin) DownloadFilePrepareCallback(context *Context, params *DownloadFilePrepareParams) (*DownloadFilePrepareResult, error) {
var fn func(*Context, *DownloadFilePrepareParams) (*DownloadFilePrepareResult, error)
if !js.isHandlerFuncExisted("downloadFilePrepareCallback") {
return nil, nil
}
err := js.vm.ExportTo(js.vm.Get("downloadFilePrepareCallback"), &fn)
if err != nil {
logger.Verboseln("Js函数映射到 Go 函数失败!")
return nil, nil
}
r, er := fn(context, params)
if er != nil {
logger.Verboseln(er)
return nil, er
}
return r, nil
}
2022-04-21 16:53:08 +08:00
// DownloadFileFinishCallback 下载文件结束的回调函数
func (js *JsPlugin) DownloadFileFinishCallback(context *Context, params *DownloadFileFinishParams) error {
var fn func(*Context, *DownloadFileFinishParams) error
if !js.isHandlerFuncExisted("downloadFileFinishCallback") {
return nil
}
err := js.vm.ExportTo(js.vm.Get("downloadFileFinishCallback"), &fn)
if err != nil {
logger.Verboseln("Js函数映射到 Go 函数失败!")
return nil
}
er := fn(context, params)
if er != nil {
logger.Verboseln(er)
return nil
}
return nil
}
2022-06-14 21:05:40 +08:00
// SyncScanLocalFilePrepareCallback 同步备份-扫描本地文件的回调函数
func (js *JsPlugin) SyncScanLocalFilePrepareCallback(context *Context, params *SyncScanLocalFilePrepareParams) (*SyncScanLocalFilePrepareResult, error) {
var fn func(*Context, *SyncScanLocalFilePrepareParams) (*SyncScanLocalFilePrepareResult, error)
if !js.isHandlerFuncExisted("syncScanLocalFilePrepareCallback") {
return nil, nil
}
err := js.vm.ExportTo(js.vm.Get("syncScanLocalFilePrepareCallback"), &fn)
if err != nil {
logger.Verboseln("Js函数映射到 Go 函数失败!")
return nil, nil
}
r, er := fn(context, params)
if er != nil {
logger.Verboseln(er)
return nil, er
}
return r, nil
}
// SyncScanPanFilePrepareCallback 同步备份-扫描本地文件的回调函数
func (js *JsPlugin) SyncScanPanFilePrepareCallback(context *Context, params *SyncScanPanFilePrepareParams) (*SyncScanPanFilePrepareResult, error) {
var fn func(*Context, *SyncScanPanFilePrepareParams) (*SyncScanPanFilePrepareResult, error)
if !js.isHandlerFuncExisted("syncScanPanFilePrepareCallback") {
return nil, nil
}
err := js.vm.ExportTo(js.vm.Get("syncScanPanFilePrepareCallback"), &fn)
if err != nil {
logger.Verboseln("Js函数映射到 Go 函数失败!")
return nil, nil
}
r, er := fn(context, params)
if er != nil {
logger.Verboseln(er)
return nil, er
}
return r, nil
}
// SyncFileFinishCallback 同步备份-同步文件完成的回调函数
func (js *JsPlugin) SyncFileFinishCallback(context *Context, params *SyncFileFinishParams) error {
var fn func(*Context, *SyncFileFinishParams) error
if !js.isHandlerFuncExisted("syncFileFinishCallback") {
return nil
}
err := js.vm.ExportTo(js.vm.Get("syncFileFinishCallback"), &fn)
if err != nil {
logger.Verboseln("Js函数映射到 Go 函数失败!")
return nil
}
er := fn(context, params)
if er != nil {
logger.Verboseln(er)
return nil
}
return nil
}
2022-04-19 20:23:17 +08:00
func (js *JsPlugin) Stop() error {
return nil
}