| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package console
- import (
- _ "embed"
- "fmt"
- "mtp20access/global"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "regexp"
- "runtime"
- "strings"
- "github.com/gin-gonic/gin"
- )
- //go:embed upmtpgo.sh
- var strShell string
- var IsUpdateing = false
- // MakeUpdateScript 生成脚本
- func MakeUpdateScript(c *gin.Context) {
- global.M2A_LOG.Info("111111111")
- if IsUpdateing {
- c.String(http.StatusOK, "正在升级中...")
- return
- }
- global.M2A_LOG.Info("22222222")
- if runtime.GOOS == "windows" {
- c.String(http.StatusOK, "windows不支持, 仅支持linux下执行")
- return
- }
- // logger.GetLogger().Debug("*********************auto update*******************************")
- IsUpdateing = true
- defer func() {
- IsUpdateing = false
- }()
- global.M2A_LOG.Info("33333333")
- filename := c.DefaultQuery("filename", "")
- if createsh() {
- global.M2A_LOG.Info("7777777777")
- c.String(http.StatusOK, "正在执行升级, 升级会保留原有的config.yaml,且重启服务。注意:请勿刷新本页面。")
- rx := regexp.MustCompile(`mtp20_access_*\.zip`) // mtp20_access_20221009092417.zip
- names := rx.FindStringSubmatch(filename)
- // 正则表达式检查文件名是否符合规范
- if len(names) > 0 && names[0] == filename {
- fmt.Println("filename:", filename)
- cmd := exec.Command("sh", "./update.sh", filename)
- fmt.Println("cmd param:", cmd.Args)
- cmd.Run()
- } else {
- _ = exec.Command("sh", "./update.sh").Run()
- }
- } else {
- global.M2A_LOG.Info("88888888888888888")
- c.String(http.StatusBadRequest, "创建脚本失败, 可能是权限不足")
- }
- }
- // 生成update.sh文件
- func createsh() bool {
- global.M2A_LOG.Info("4444444444")
- dir := ""
- if str, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil {
- dir = strings.Replace(str, "\\", "/", -1)
- } else {
- return false
- }
- global.M2A_LOG.Info("555555555555")
- // 创建文件
- if f, err := os.OpenFile(dir+"/update.sh", os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666); err == nil {
- f.Write([]byte(strShell))
- f.Close()
- } else {
- return false
- }
- global.M2A_LOG.Info("66666666666")
- return true
- }
|