1.4.5.2
This commit is contained in:
136
EdgeAPI/internal/senders/mediasenders/media_script.go
Normal file
136
EdgeAPI/internal/senders/mediasenders/media_script.go
Normal file
@@ -0,0 +1,136 @@
|
||||
//go:build plus
|
||||
|
||||
package mediasenders
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/files"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/utils/string"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ScriptMedia 脚本媒介
|
||||
type ScriptMedia struct {
|
||||
Path string `yaml:"path" json:"path"`
|
||||
ScriptType string `yaml:"scriptType" json:"scriptType"` // 脚本类型,可以为path, code
|
||||
ScriptLang string `yaml:"scriptLang" json:"scriptLang"` // 脚本语言
|
||||
Script string `yaml:"script" json:"script"` // 脚本代码
|
||||
Cwd string `yaml:"cwd" json:"cwd"`
|
||||
Env []*Variable `yaml:"env" json:"env"`
|
||||
}
|
||||
|
||||
// NewScriptMedia 获取新对象
|
||||
func NewScriptMedia() *ScriptMedia {
|
||||
return &ScriptMedia{}
|
||||
}
|
||||
|
||||
// AddEnv 添加环境变量
|
||||
func (this *ScriptMedia) AddEnv(name, value string) {
|
||||
this.Env = append(this.Env, &Variable{
|
||||
Name: name,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
||||
// FormattedScript 格式化脚本
|
||||
func (this *ScriptMedia) FormattedScript() string {
|
||||
script := this.Script
|
||||
script = strings.Replace(script, "\r", "", -1)
|
||||
return script
|
||||
}
|
||||
|
||||
// Generate 保存到本地
|
||||
func (this *ScriptMedia) Generate(id string) (path string, err error) {
|
||||
var tmpDir = os.TempDir()
|
||||
if runtime.GOOS == "windows" {
|
||||
path = tmpDir + Tea.DS + "edge.script." + id + ".bat"
|
||||
} else {
|
||||
path = tmpDir + "/edge.script." + id + ".script"
|
||||
}
|
||||
shFile := files.NewFile(path)
|
||||
if !shFile.Exists() {
|
||||
err = shFile.WriteString(this.FormattedScript())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = shFile.Chmod(0777)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Send 发送
|
||||
func (this *ScriptMedia) Send(user string, subject string, body string, productName string, datetime string) (resp []byte, err error) {
|
||||
// 脚本
|
||||
if this.ScriptType == "code" {
|
||||
path, err := this.Generate(stringutil.Rand(16))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
this.Path = path
|
||||
|
||||
defer func() {
|
||||
f := files.NewFile(this.Path)
|
||||
if f.Exists() {
|
||||
err := f.Delete()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if len(this.Path) == 0 {
|
||||
return nil, errors.New("'path' should be specified")
|
||||
}
|
||||
|
||||
cmd := exec.Command(this.Path)
|
||||
|
||||
if len(this.Env) > 0 {
|
||||
for _, env := range this.Env {
|
||||
cmd.Env = append(cmd.Env, env.Name+"="+env.Value)
|
||||
}
|
||||
}
|
||||
cmd.Env = append(cmd.Env, "MessageUser="+user)
|
||||
cmd.Env = append(cmd.Env, "MessageSubject="+subject)
|
||||
cmd.Env = append(cmd.Env, "MessageBody="+body)
|
||||
|
||||
if len(this.Cwd) > 0 {
|
||||
cmd.Dir = this.Cwd
|
||||
}
|
||||
|
||||
stdout := bytes.NewBuffer([]byte{})
|
||||
stderr := bytes.NewBuffer([]byte{})
|
||||
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
if stderr.Len() > 0 {
|
||||
return stdout.Bytes(), errors.New(stderr.String())
|
||||
}
|
||||
|
||||
return stdout.Bytes(), nil
|
||||
}
|
||||
|
||||
// RequireUser 是否需要用户标识
|
||||
func (this *ScriptMedia) RequireUser() bool {
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user