/
file.go
54 lines (42 loc) · 1.03 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package musiclib
import (
"context"
"strings"
)
type FileIndex struct {
uriLookup map[string]*Node
roots []*Node
}
func (f *FileIndex) Roots(ctx context.Context) ([]*Node, error) {
return f.roots, nil
}
func (f *FileIndex) Node(ctx context.Context, uri string) (*Node, error) {
return f.uriLookup[uri], nil
}
func (f *FileIndex) Index(ctx context.Context, files *Files) error {
if f.uriLookup == nil {
f.uriLookup = make(map[string]*Node)
}
for _, root := range files.Roots {
rootNode := f.addNode(nil, &root)
f.roots = append(f.roots, rootNode)
}
return nil
}
func (f *FileIndex) addNode(parent *Node, filePath *PathMeta) *Node {
node := &Node{
Name: filePath.Name,
LowerName: strings.ToLower(filePath.Name),
URI: encodeFileURI(filePath.Path),
ImageURI: encodeFileURI(filePath.ImagePath),
Parent: parent,
}
for _, child := range filePath.Children {
f.addNode(node, &child)
}
if parent != nil {
parent.Children = append(parent.Children, node)
}
f.uriLookup[node.URI] = node
return node
}