code refactoring

This commit is contained in:
2024-02-12 12:02:23 +01:00
parent cc06487b0a
commit 65b0c8bc0e
7 changed files with 51 additions and 32 deletions

View File

@@ -1,37 +1,33 @@
package internal
type Node[T any] struct {
Value T
}
type Stack[T any] struct {
Nodes []*Node[T]
count int
Elements []*T
count int
}
func NewStack[T any]() *Stack[T] {
return &Stack[T]{
Nodes: make([]*Node[T], 10),
Elements: make([]*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
if s.count >= len(s.Elements) {
Elements := make([]*T, len(s.Elements)*2)
copy(Elements, s.Elements)
s.Elements = Elements
}
s.Nodes[s.count] = &Node[T]{Value: val}
s.Elements[s.count] = &val
s.count++
}
func (s *Stack[T]) Pop() *Node[T] {
func (s *Stack[T]) Pop() *T {
if s.count == 0 {
return nil
}
node := s.Nodes[s.count-1]
Element := s.Elements[s.count-1]
s.count--
return node
return Element
}
func (s *Stack[T]) IsEmpty() bool {

View File

@@ -30,5 +30,6 @@ func ApplyRouter(db *sql.DB, mdb *internal.MemoryDB, mq *internal.MessageQueue)
r.Post("/template", h.AddTemplate())
r.Get("/template/all", h.GetTemplates())
r.Delete("/template/{id}", h.DeleteTemplate())
r.Get("/tree", h.DirectoryTree())
}
}

View File

@@ -154,3 +154,22 @@ func (h *Handler) DeleteTemplate() http.HandlerFunc {
}
}
}
func (h *Handler) DirectoryTree() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
tree, err := h.service.DirectoryTree(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(tree)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/google/uuid"
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
"github.com/marcopeocchi/yt-dlp-web-ui/server/sys"
)
type Service struct {
@@ -118,3 +119,7 @@ func (s *Service) DeleteTemplate(ctx context.Context, id string) error {
return err
}
func (s *Service) DirectoryTree(ctx context.Context) (*internal.Stack[sys.FSNode], error) {
return sys.DirectoryTree()
}

View File

@@ -140,7 +140,7 @@ func (s *Service) FreeSpace(args NoArgs, free *uint64) error {
}
// Return a flattned tree of the download directory
func (s *Service) DirectoryTree(args NoArgs, tree *[]string) error {
func (s *Service) DirectoryTree(args NoArgs, tree *internal.Stack[sys.FSNode]) error {
dfsTree, err := sys.DirectoryTree()
if dfsTree != nil {
*tree = *dfsTree

View File

@@ -18,39 +18,35 @@ func FreeSpace() (uint64, error) {
return (stat.Bavail * uint64(stat.Bsize)), nil
}
type FSNode struct {
path string
children []FSNode
}
// Build a directory tree started from the specified path using DFS.
// Then return the flattened tree represented as a list.
func DirectoryTree() (*[]string, error) {
type Node struct {
path string
children []Node
}
func DirectoryTree() (*internal.Stack[FSNode], error) {
rootPath := config.Instance().DownloadPath
stack := internal.NewStack[Node]()
flattened := make([]string, 0)
stack := internal.NewStack[FSNode]()
stack.Push(Node{path: rootPath})
flattened = append(flattened, rootPath)
stack.Push(FSNode{path: rootPath})
for stack.IsNotEmpty() {
current := stack.Pop().Value
current := stack.Pop()
children, err := os.ReadDir(current.path)
if err != nil {
return nil, err
}
for _, entry := range children {
childPath := filepath.Join(current.path, entry.Name())
childNode := Node{path: childPath}
childNode := FSNode{path: childPath}
if entry.IsDir() {
current.children = append(current.children, childNode)
stack.Push(childNode)
flattened = append(flattened, childNode.path)
}
}
}
return &flattened, nil
return stack, nil
}