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

View File

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

View File

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

View File

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