From d4f656fd87c0005775c0ada1acacdecfb6f53a51 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Wed, 26 Jul 2023 16:21:10 +0200 Subject: [PATCH] code refactoring --- server/internal/stack.go | 10 ++++++-- server/rest/handlers.go | 2 +- server/sys/fs.go | 14 +++------- server/updater/forced_update.go | 45 --------------------------------- server/updater/types.go | 6 ----- server/updater/update.go | 13 +++++----- 6 files changed, 20 insertions(+), 70 deletions(-) delete mode 100644 server/updater/forced_update.go delete mode 100644 server/updater/types.go diff --git a/server/internal/stack.go b/server/internal/stack.go index c7738c1..3ab48d6 100644 --- a/server/internal/stack.go +++ b/server/internal/stack.go @@ -9,13 +9,19 @@ type Stack[T any] struct { count int } -func (s *Stack[T]) Push(n *Node[T]) { +func NewStack[T any]() *Stack[T] { + return &Stack[T]{ + Nodes: make([]*Node[T], 10), + } +} + +func (s *Stack[T]) Push(val T) { if s.count >= len(s.Nodes) { Nodes := make([]*Node[T], len(s.Nodes)*2) copy(Nodes, s.Nodes) s.Nodes = Nodes } - s.Nodes[s.count] = n + s.Nodes[s.count] = &Node[T]{Value: val} s.count++ } diff --git a/server/rest/handlers.go b/server/rest/handlers.go index 20c260f..94380d2 100644 --- a/server/rest/handlers.go +++ b/server/rest/handlers.go @@ -2,7 +2,6 @@ package rest import ( "encoding/hex" - "encoding/json" "net/http" "os" "path/filepath" @@ -11,6 +10,7 @@ import ( "time" "github.com/go-chi/chi/v5" + "github.com/goccy/go-json" "github.com/golang-jwt/jwt/v5" "github.com/marcopeocchi/yt-dlp-web-ui/server/config" "github.com/marcopeocchi/yt-dlp-web-ui/server/utils" diff --git a/server/sys/fs.go b/server/sys/fs.go index 2473ab3..112f340 100644 --- a/server/sys/fs.go +++ b/server/sys/fs.go @@ -28,15 +28,11 @@ func DirectoryTree() (*[]string, error) { rootPath := config.Instance().GetConfig().DownloadPath - stack := internal.Stack[Node]{ - Nodes: make([]*internal.Node[Node], 5), - } + stack := internal.NewStack[Node]() flattened := make([]string, 0) - root := Node{path: rootPath} - stack.Push(&internal.Node[Node]{ - Value: root, - }) + stack.Push(Node{path: rootPath}) + flattened = append(flattened, rootPath) for stack.IsNotEmpty() { @@ -51,9 +47,7 @@ func DirectoryTree() (*[]string, error) { if entry.IsDir() { current.children = append(current.children, childNode) - stack.Push(&internal.Node[Node]{ - Value: childNode, - }) + stack.Push(childNode) flattened = append(flattened, childNode.path) } } diff --git a/server/updater/forced_update.go b/server/updater/forced_update.go deleted file mode 100644 index 1dbb909..0000000 --- a/server/updater/forced_update.go +++ /dev/null @@ -1,45 +0,0 @@ -package updater - -import ( - "io" - "log" - "net/http" - - "github.com/goccy/go-json" -) - -const ( - gitHubAPILatest = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest" - gitHubAPIDownload = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/download" -) - -var ( - client = &http.Client{ - CheckRedirect: http.DefaultClient.CheckRedirect, - } -) - -func getLatestReleaseTag() (string, error) { - res, err := client.Get(gitHubAPILatest) - if err != nil { - log.Println("Cannot get release tag from GitHub API") - return "", err - } - defer res.Body.Close() - - body, err := io.ReadAll(res.Body) - - if err != nil { - log.Println("Cannot parse response from GitHub API") - return "", err - } - - tag := ReleaseLatestResponse{} - json.Unmarshal(body, &tag) - - return tag.TagName, nil -} - -func ForceUpdate() { - getLatestReleaseTag() -} diff --git a/server/updater/types.go b/server/updater/types.go deleted file mode 100644 index d967425..0000000 --- a/server/updater/types.go +++ /dev/null @@ -1,6 +0,0 @@ -package updater - -type ReleaseLatestResponse struct { - Name string `json:"name"` - TagName string `json:"tag_name"` -} diff --git a/server/updater/update.go b/server/updater/update.go index 994e2ac..2c09ea1 100644 --- a/server/updater/update.go +++ b/server/updater/update.go @@ -6,13 +6,14 @@ import ( "github.com/marcopeocchi/yt-dlp-web-ui/server/config" ) -var path = config.Instance().GetConfig().DownloaderPath - // Update using the builtin function of yt-dlp func UpdateExecutable() error { - cmd := exec.Command(path, "-U") - cmd.Start() + cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, "-U") - err := cmd.Wait() - return err + err := cmd.Start() + if err != nil { + return err + } + + return cmd.Wait() }