chore: clean up update_ui code

This commit is contained in:
Larvan2 2024-09-02 11:17:35 +08:00
parent 802267fb5b
commit 56fe7d5304
4 changed files with 38 additions and 62 deletions

View File

@ -2,7 +2,6 @@ package updater
import ( import (
"archive/zip" "archive/zip"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -12,23 +11,26 @@ import (
"sync" "sync"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
) )
var ( var (
ExternalUIURL string ExternalUIURL string
ExternalUIPath string ExternalUIPath string
ExternalUIFolder string AutoUpdateUI bool
ExternalUIName string
)
var (
ErrIncompleteConf = errors.New("ExternalUI configure incomplete")
) )
var xdMutex sync.Mutex var xdMutex sync.Mutex
func UpdateUI() error { func UpdateUI() error {
xdMutex.Lock() xdMutex.Lock()
defer xdMutex.Unlock() defer xdMutex.Unlock()
err := prepareUIPath()
if err != nil {
return fmt.Errorf("prepare UI path failed: %w", err)
}
data, err := downloadForBytes(ExternalUIURL) data, err := downloadForBytes(ExternalUIURL)
if err != nil { if err != nil {
return fmt.Errorf("can't download file: %w", err) return fmt.Errorf("can't download file: %w", err)
@ -40,7 +42,7 @@ func UpdateUI() error {
} }
defer os.Remove(saved) defer os.Remove(saved)
err = cleanup(ExternalUIFolder) err = cleanup(ExternalUIPath)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return fmt.Errorf("cleanup exist file error: %w", err) return fmt.Errorf("cleanup exist file error: %w", err)
@ -52,27 +54,19 @@ func UpdateUI() error {
return fmt.Errorf("can't extract zip file: %w", err) return fmt.Errorf("can't extract zip file: %w", err)
} }
err = os.Rename(unzipFolder, ExternalUIFolder) err = os.Rename(unzipFolder, ExternalUIPath)
if err != nil { if err != nil {
return fmt.Errorf("can't rename folder: %w", err) return fmt.Errorf("rename UI folder failed: %w", err)
} }
return nil return nil
} }
func PrepareUIPath() error { func prepareUIPath() error {
if ExternalUIPath == "" || ExternalUIURL == "" { if _, err := os.Stat(ExternalUIPath); os.IsNotExist(err) {
return ErrIncompleteConf log.Infoln("dir %s does not exist, creating", ExternalUIPath)
} if err := os.MkdirAll(ExternalUIPath, os.ModePerm); err != nil {
log.Warnln("create dir %s error: %s", ExternalUIPath, err)
if ExternalUIName != "" {
ExternalUIFolder = filepath.Clean(path.Join(ExternalUIPath, ExternalUIName))
if _, err := os.Stat(ExternalUIPath); os.IsNotExist(err) {
if err := os.MkdirAll(ExternalUIPath, os.ModePerm); err != nil {
return err
}
} }
} else {
ExternalUIFolder = ExternalUIPath
} }
return nil return nil
} }

View File

@ -7,7 +7,6 @@ import (
"net" "net"
"net/netip" "net/netip"
"net/url" "net/url"
"os"
"path" "path"
"regexp" "regexp"
"strings" "strings"
@ -704,33 +703,23 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
} }
N.DisableKeepAlive = cfg.DisableKeepAlive N.DisableKeepAlive = cfg.DisableKeepAlive
updater.ExternalUIPath = cfg.ExternalUI
// checkout externalUI exist // checkout externalUI exist
if updater.ExternalUIPath != "" { if cfg.ExternalUI != "" {
updater.ExternalUIPath = C.Path.Resolve(updater.ExternalUIPath) updater.AutoUpdateUI = true
if _, err := os.Stat(updater.ExternalUIPath); os.IsNotExist(err) { updater.ExternalUIPath = C.Path.Resolve(cfg.ExternalUI)
defaultUIpath := path.Join(C.Path.HomeDir(), "ui")
log.Warnln("external-ui: %s does not exist, creating folder in %s", updater.ExternalUIPath, defaultUIpath)
if err := os.MkdirAll(defaultUIpath, os.ModePerm); err != nil {
return nil, err
}
updater.ExternalUIPath = defaultUIpath
cfg.ExternalUI = defaultUIpath
}
}
// checkout UIpath/name exist
if cfg.ExternalUIName != "" {
updater.ExternalUIName = cfg.ExternalUIName
} else { } else {
updater.ExternalUIFolder = updater.ExternalUIPath // default externalUI path
} updater.ExternalUIPath = path.Join(C.Path.HomeDir(), "ui")
if cfg.ExternalUIURL != "" {
updater.ExternalUIURL = cfg.ExternalUIURL
} }
err := updater.PrepareUIPath() // checkout UIpath/name exist
if err != nil { if cfg.ExternalUIName != "" {
log.Errorln("PrepareUIPath error: %s", err) updater.AutoUpdateUI = true
updater.ExternalUIPath = path.Join(updater.ExternalUIPath, cfg.ExternalUIName)
}
if cfg.ExternalUIURL != "" {
updater.ExternalUIURL = cfg.ExternalUIURL
} }
return &General{ return &General{

View File

@ -381,12 +381,12 @@ func updateTunnels(tunnels []LC.Tunnel) {
} }
func initExternalUI() { func initExternalUI() {
if updater.ExternalUIFolder != "" { if updater.AutoUpdateUI {
dirEntries, _ := os.ReadDir(updater.ExternalUIFolder) dirEntries, _ := os.ReadDir(updater.ExternalUIPath)
if len(dirEntries) > 0 { if len(dirEntries) > 0 {
log.Infoln("UI already exists") log.Infoln("UI already exists, skip downloading")
} else { } else {
log.Infoln("UI not exists, downloading") log.Infoln("External UI downloading ...")
updater.UpdateUI() updater.UpdateUI()
} }
} }

View File

@ -1,7 +1,6 @@
package route package route
import ( import (
"errors"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -50,15 +49,9 @@ func upgradeCore(w http.ResponseWriter, r *http.Request) {
func updateUI(w http.ResponseWriter, r *http.Request) { func updateUI(w http.ResponseWriter, r *http.Request) {
err := updater.UpdateUI() err := updater.UpdateUI()
if err != nil { if err != nil {
if errors.Is(err, updater.ErrIncompleteConf) { log.Warnln("%s", err)
log.Warnln("%s", err) render.Status(r, http.StatusInternalServerError)
render.Status(r, http.StatusNotImplemented) render.JSON(w, r, newError(fmt.Sprintf("%s", err)))
render.JSON(w, r, newError(fmt.Sprintf("%s", err)))
} else {
log.Warnln("%s", err)
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError(fmt.Sprintf("%s", err)))
}
return return
} }