@@ -18,6 +18,7 @@ type Node struct {
1818}
1919
2020// buildFileTree constructs the hierarchical tree from a list of file paths
21+ // by creating a directory traversal structure
2122func buildFileTree (paths []string ) * Node {
2223 root := & Node {
2324 Name : "" ,
@@ -26,45 +27,44 @@ func buildFileTree(paths []string) *Node {
2627
2728 for _ , path := range paths {
2829 isDir := strings .HasSuffix (path , "/" )
29- path = strings .TrimSuffix (path , "/" ) // Remove trailing slash
30+ path = strings .TrimSuffix (path , "/" )
3031
3132 parts := strings .Split (path , "/" )
3233 current := root
3334
34- // Traverse through the directory structure
3535 for i := 0 ; i < len (parts )- 1 ; i ++ {
3636 part := parts [i ]
37- found := false
3837
39- // Check if a child with this name already exists
38+ // Find if a child with this name already exists
39+ var found * Node
4040 for j := range current .Items {
4141 if current .Items [j ].Name == part {
42- current = & current .Items [j ]
43- found = true
42+ found = & current .Items [j ]
4443 break
4544 }
4645 }
4746
48- if ! found {
47+ if found == nil {
4948 // Create a new directory node
50- newNode := & Node {
49+ newNode := Node {
5150 Name : part ,
5251 Items : []Node {},
5352 }
54- current .Items = append (current .Items , * newNode )
55- current = newNode
53+ current .Items = append (current .Items , newNode )
54+ found = & current . Items [ len ( current . Items ) - 1 ]
5655 }
56+
57+ current = found
5758 }
5859
5960 // Handle the final part (file or directory)
6061 lastPart := parts [len (parts )- 1 ]
6162 if isDir {
62- // Add a new directory node
63- newNode := & Node {
63+ newNode := Node {
6464 Name : lastPart ,
6565 Items : []Node {},
6666 }
67- current .Items = append (current .Items , * newNode )
67+ current .Items = append (current .Items , newNode )
6868 } else {
6969 // Add a file node
7070 current .Items = append (current .Items , Node {Name : lastPart , Items : []Node {}})
@@ -74,26 +74,6 @@ func buildFileTree(paths []string) *Node {
7474 return root
7575}
7676
77- // collectFlatNodes returns a flat list of nodes, with each directory containing its items
78- func collectFlatNodes (node * Node ) []Node {
79- var result []Node
80-
81- // Function to recursively traverse the tree
82- var traverse func (* Node )
83- traverse = func (n * Node ) {
84- // Add current node to the result
85- result = append (result , Node {Name : n .Name , Items : n .Items })
86-
87- // If this node has children, recursively process them
88- for _ , child := range n .Items {
89- traverse (& child )
90- }
91- }
92-
93- traverse (node )
94- return result
95- }
96-
9777func (r * Route ) Files (c * gin.Context ) {
9878 user , repoPath := c .Param ("user" ), c .Param ("path" )
9979 fullPath := "/" + user + "/" + repoPath
0 commit comments