converted dir tree

This commit is contained in:
2023-01-11 23:19:37 +01:00
parent 4d4582b3f7
commit 4c7faa1b46
14 changed files with 229 additions and 107 deletions

37
server/internal/stack.go Normal file
View File

@@ -0,0 +1,37 @@
package internal
type Node[T any] struct {
Value T
}
type Stack[T any] struct {
Nodes []*Node[T]
count int
}
func (s *Stack[T]) Push(n *Node[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.count++
}
func (s *Stack[T]) Pop() *Node[T] {
if s.count == 0 {
return nil
}
node := s.Nodes[s.count-1]
s.count--
return node
}
func (s *Stack[T]) IsEmpty() bool {
return s.count == 0
}
func (s *Stack[T]) IsNotEmpty() bool {
return s.count != 0
}

View File

@@ -157,7 +157,12 @@ func (p *Process) GetFormatsSync() (DownloadFormats, error) {
cmd.Wait()
info := DownloadFormats{URL: p.url}
best := Format{}
json.Unmarshal(stdout, &info)
json.Unmarshal(stdout, &best)
info.Best = best
return info, nil
}

View File

@@ -10,6 +10,7 @@ import (
"net/rpc"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/websocket/v2"
)
@@ -29,6 +30,8 @@ func RunBlocking(ctx context.Context) {
app := fiber.New()
app.Use(cors.New())
app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(fe),
}))

View File

@@ -84,3 +84,9 @@ func (t *Service) FreeSpace(args NoArgs, free *uint64) error {
*free = freeSpace
return err
}
func (t *Service) DirectoryTree(args NoArgs, tree *[]string) error {
dfsTree, err := sys.DirectoryTree("downloads")
*tree = *dfsTree
return err
}

View File

@@ -2,7 +2,9 @@ package sys
import (
"os"
"path/filepath"
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
"golang.org/x/sys/unix"
)
@@ -18,3 +20,42 @@ func FreeSpace() (uint64, error) {
unix.Statfs(wd+"/downloads", &stat)
return (stat.Bavail * uint64(stat.Bsize)), nil
}
func DirectoryTree(rootPath string) (*[]string, error) {
type Node struct {
path string
children []Node
}
stack := internal.Stack[Node]{
Nodes: make([]*internal.Node[Node], 5),
}
flattened := make([]string, 0)
root := Node{path: rootPath}
stack.Push(&internal.Node[Node]{
Value: root,
})
flattened = append(flattened, rootPath)
for stack.IsNotEmpty() {
current := stack.Pop().Value
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}
if entry.IsDir() {
current.children = append(current.children, childNode)
stack.Push(&internal.Node[Node]{
Value: childNode,
})
flattened = append(flattened, childNode.path)
}
}
}
return &flattened, nil
}