Fix logic of fetching torrents
This commit is contained in:
87
internal/fs/node.go
Normal file
87
internal/fs/node.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
)
|
||||
|
||||
// FileNode represents a directory or a file. If IsDir is true, Children is used.
|
||||
type FileNode struct {
|
||||
Name string
|
||||
IsDir bool
|
||||
Children cmap.ConcurrentMap[string, *FileNode]
|
||||
}
|
||||
|
||||
// NewFileNode creates a new FileNode, initializing the concurrent map if it is a directory.
|
||||
func NewFileNode(name string, isDir bool) *FileNode {
|
||||
node := &FileNode{
|
||||
Name: name,
|
||||
IsDir: isDir,
|
||||
}
|
||||
if isDir {
|
||||
node.Children = cmap.New[*FileNode]()
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// AddChild adds a child node to a directory node.
|
||||
func (n *FileNode) AddChild(child *FileNode) {
|
||||
if n.IsDir {
|
||||
n.Children.Set(child.Name, child)
|
||||
}
|
||||
}
|
||||
|
||||
// AddPath adds a path to the file system like mkdir -p.
|
||||
func (n *FileNode) MakeDirectoryWithPath(path string) {
|
||||
currentNode := n
|
||||
for _, part := range SplitPath(path) {
|
||||
child, ok := currentNode.Children.Get(part)
|
||||
if !ok {
|
||||
child = NewFileNode(part, true)
|
||||
currentNode.AddChild(child)
|
||||
}
|
||||
currentNode = child
|
||||
}
|
||||
}
|
||||
|
||||
// AddChildToPath adds a child to a path in the file system. Path should be an existing directory.
|
||||
func (n *FileNode) AddChildToPath(path string, child *FileNode) {
|
||||
parent := n.GetFileNode(path)
|
||||
if parent != nil && parent.IsDir {
|
||||
parent.AddChild(child)
|
||||
}
|
||||
}
|
||||
|
||||
// ListFiles returns a list of files in the file system.
|
||||
func (n *FileNode) ListFiles() []*FileNode {
|
||||
var files []*FileNode
|
||||
n.Children.IterCb(func(key string, value *FileNode) {
|
||||
files = append(files, value)
|
||||
})
|
||||
return files
|
||||
}
|
||||
|
||||
// GetFileNode returns the FileNode of the given path.
|
||||
func (n *FileNode) GetFileNode(path string) *FileNode {
|
||||
currentNode := n
|
||||
for _, part := range SplitPath(path) {
|
||||
child, ok := currentNode.Children.Get(part)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
currentNode = child
|
||||
}
|
||||
return currentNode
|
||||
}
|
||||
|
||||
// SplitPath splits a path into its parts.
|
||||
func SplitPath(path string) []string {
|
||||
var parts []string
|
||||
for _, part := range strings.Split(path, "/") {
|
||||
if part != "" {
|
||||
parts = append(parts, part)
|
||||
}
|
||||
}
|
||||
return parts
|
||||
}
|
||||
52
internal/fs/node_test.go
Normal file
52
internal/fs/node_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestNewFileNode tests the creation of a new FileNode.
|
||||
func TestNewFileNode(t *testing.T) {
|
||||
node := NewFileNode("root", true)
|
||||
if node.Name != "root" || !node.IsDir {
|
||||
t.Errorf("NewFileNode failed to initialize properly")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAddChild and TestListFiles tests adding children to a node and listing them.
|
||||
func TestAddChild(t *testing.T) {
|
||||
root := NewFileNode("root", true)
|
||||
child1 := NewFileNode("child1", false)
|
||||
child2 := NewFileNode("child2", true)
|
||||
|
||||
root.AddChild(child1)
|
||||
root.AddChild(child2)
|
||||
|
||||
// Test if children are added properly
|
||||
if _, ok := root.Children.Get("child1"); !ok {
|
||||
t.Errorf("Failed to add child1")
|
||||
}
|
||||
if _, ok := root.Children.Get("child2"); !ok {
|
||||
t.Errorf("Failed to add child2")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetFileNode tests retrieving a node from the file system based on a given path.
|
||||
func TestGetFileNode(t *testing.T) {
|
||||
root := NewFileNode("root", true)
|
||||
child1 := NewFileNode("child1", false)
|
||||
child2 := NewFileNode("child2", true)
|
||||
|
||||
root.AddChild(child1)
|
||||
root.AddChild(child2)
|
||||
|
||||
// Retrieve node by path
|
||||
resultNode := root.GetFileNode("child2")
|
||||
if resultNode != child2 {
|
||||
t.Errorf("GetFileNode failed to retrieve the correct node")
|
||||
}
|
||||
|
||||
// Test non-existent path
|
||||
if resultNode = root.GetFileNode("child3"); resultNode != nil {
|
||||
t.Errorf("GetFileNode should return nil for non-existent node")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user