From 65b0c8bc0ec04e5c56e6a83687196d06a5fc7ee6 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Mon, 12 Feb 2024 12:02:23 +0100 Subject: [PATCH] code refactoring --- server/internal/stack.go | 26 +++++++++++--------------- server/rest/container.go | 1 + server/rest/handlers.go | 19 +++++++++++++++++++ server/rest/service.go | 5 +++++ server/rpc/service.go | 2 +- server/sys/fs.go | 26 +++++++++++--------------- ui/src/lib/ffetch.ts | 4 +++- 7 files changed, 51 insertions(+), 32 deletions(-) diff --git a/server/internal/stack.go b/server/internal/stack.go index 3ab48d6..a8ce622 100644 --- a/server/internal/stack.go +++ b/server/internal/stack.go @@ -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 { diff --git a/server/rest/container.go b/server/rest/container.go index a85db8b..ee3c0f9 100644 --- a/server/rest/container.go +++ b/server/rest/container.go @@ -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()) } } diff --git a/server/rest/handlers.go b/server/rest/handlers.go index b275c43..8c4d8b9 100644 --- a/server/rest/handlers.go +++ b/server/rest/handlers.go @@ -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) + } + } +} diff --git a/server/rest/service.go b/server/rest/service.go index 84b9baa..b03d48e 100644 --- a/server/rest/service.go +++ b/server/rest/service.go @@ -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() +} diff --git a/server/rpc/service.go b/server/rpc/service.go index f21e01a..b6347d9 100644 --- a/server/rpc/service.go +++ b/server/rpc/service.go @@ -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 diff --git a/server/sys/fs.go b/server/sys/fs.go index 88fda33..f4245c9 100644 --- a/server/sys/fs.go +++ b/server/sys/fs.go @@ -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 } diff --git a/ui/src/lib/ffetch.ts b/ui/src/lib/ffetch.ts index ee7a15e..e30b3d8 100644 --- a/ui/src/lib/ffetch.ts +++ b/ui/src/lib/ffetch.ts @@ -1,11 +1,13 @@ import { tryCatch } from 'fp-ts/TaskEither' +/** + * functional fetch(): composable as TaskEither +*/ export const ffetch = (url: string, opt?: RequestInit) => tryCatch( () => fetcher(url, opt), (e) => `error while fetching: ${e}` ) - const fetcher = async (url: string, opt?: RequestInit) => { const jwt = localStorage.getItem('token')