code refactoring

This commit is contained in:
2023-07-26 16:21:10 +02:00
parent a4d586a3a0
commit d4f656fd87
6 changed files with 20 additions and 70 deletions

View File

@@ -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++
}

View File

@@ -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"

View File

@@ -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)
}
}

View File

@@ -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()
}

View File

@@ -1,6 +0,0 @@
package updater
type ReleaseLatestResponse struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
}

View File

@@ -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()
err := cmd.Start()
if err != nil {
return err
}
return cmd.Wait()
}