-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
243 lines (212 loc) · 5.51 KB
/
metadata.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package musiclib
import (
"context"
"fmt"
"log"
"sort"
"strconv"
"strings"
)
type NodeBuilder func(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool)
type MetadataIndex struct {
uriLookup map[string]*Node
builders []NodeBuilder
roots []*Node
}
func NewMetadataIndex(builders []NodeBuilder) *MetadataIndex {
return &MetadataIndex{
uriLookup: make(map[string]*Node),
builders: builders,
}
}
func (a *MetadataIndex) Roots(ctx context.Context) ([]*Node, error) {
return a.roots, nil
}
func (a *MetadataIndex) Node(ctx context.Context, uri string) (*Node, error) {
return a.uriLookup[uri], nil
}
func (a *MetadataIndex) Index(ctx context.Context, files *Files) error {
if err := files.WalkFiles(func(dir *PathMeta, file *PathMeta) error {
if err := ctx.Err(); err != nil {
return err
}
if file.Metadata == nil {
log.Printf("No metadata for: %s\n", file.Path)
return nil
}
uriPaths := make([]string, 0, len(a.builders))
var parent *Node
for _, builder := range a.builders {
node, newPaths, added := builder(a.uriLookup, dir, file, uriPaths)
uriPaths = newPaths
if added {
a.uriLookup[node.URI] = node
node.LowerName = strings.ToLower(node.Name)
if parent == nil {
a.roots = append(a.roots, node)
} else {
parent.AddChildren(node)
node.Parent = parent
}
}
parent = node
}
return nil
}); err != nil {
return err
}
sort.Slice(a.roots, nameSort(a.roots))
for _, root := range a.roots {
sortChildren(root)
}
return nil
}
func sortChildren(node *Node) {
if !node.IsFolder() {
return
}
if !node.Children[0].IsFolder() {
return
}
sort.Slice(node.Children, nameSort(node.Children))
for _, child := range node.Children {
sortChildren(child)
}
}
func NewGenreIndex() *MetadataIndex {
return NewMetadataIndex(
[]NodeBuilder{
genreNode,
artistNodeBuilder("genreartist"),
albumNodeBuilder("genrealbum"),
songNode,
})
}
func genreNode(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool) {
genre := file.Metadata.Genre()
uriPaths = append(uriPaths, genre)
genreURI := encodeCustomURI("genre", uriPaths...)
genreNode, ok := lookup[genreURI]
if !ok {
genreNode = &Node{
Name: genre,
URI: genreURI,
}
}
return genreNode, uriPaths, !ok
}
func NewModifiedAtIndex() *MetadataIndex {
return NewMetadataIndex(
[]NodeBuilder{
modifiedYearNode,
modifiedMonthNode,
artistNodeBuilder("modartist"),
albumNodeBuilder("modalbum"),
songNode,
})
}
func modifiedYearNode(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool) {
year := strconv.Itoa(file.Metadata.Modified().Year())
uriPaths = append(uriPaths, year)
modYearURI := encodeCustomURI("modyear", uriPaths...)
yearNode, ok := lookup[modYearURI]
if !ok {
yearNode = &Node{
Name: year,
URI: modYearURI,
}
}
return yearNode, uriPaths, !ok
}
func modifiedMonthNode(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool) {
month := fmt.Sprintf("%02d", file.Metadata.Modified().Month())
uriPaths = append(uriPaths, month)
modMonthURI := encodeCustomURI("modmonth", uriPaths...)
monthNode, ok := lookup[modMonthURI]
if !ok {
monthNode = &Node{
Name: month,
URI: modMonthURI,
}
}
return monthNode, uriPaths, !ok
}
func NewYearIndex() *MetadataIndex {
return NewMetadataIndex(
[]NodeBuilder{
yearNode,
artistNodeBuilder("modartist"),
albumNodeBuilder("modalbum"),
songNode,
})
}
func yearNode(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool) {
year := strconv.Itoa(file.Metadata.Year())
if year == "0" {
year = "Unknown year"
}
uriPaths = append(uriPaths, year)
yearURI := encodeCustomURI("year", uriPaths...)
yearNode, ok := lookup[yearURI]
if !ok {
yearNode = &Node{
Name: year,
URI: yearURI,
}
}
return yearNode, uriPaths, !ok
}
func NewArtistAlbumIndex() *MetadataIndex {
return NewMetadataIndex(
[]NodeBuilder{
artistNodeBuilder("artist"),
albumNodeBuilder("artistalbum"),
songNode,
})
}
func artistNodeBuilder(scheme string) NodeBuilder {
return func(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool) {
artist := file.Metadata.AlbumArtist()
uriPaths = append(uriPaths, artist)
artistURI := encodeCustomURI(scheme, uriPaths...)
artistNode, ok := lookup[artistURI]
if !ok {
artistNode = &Node{
Name: artist,
LowerName: strings.ToLower(artist),
URI: artistURI,
}
}
return artistNode, uriPaths, !ok
}
}
func albumNodeBuilder(scheme string) NodeBuilder {
return func(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool) {
album := file.Metadata.Album()
uriPaths = append(uriPaths, album)
albumURI := encodeCustomURI(scheme, uriPaths...)
albumNode, ok := lookup[albumURI]
if !ok {
albumNode = &Node{
Name: album,
URI: albumURI,
ImageURI: encodeFileURI(dir.ImagePath),
}
}
return albumNode, uriPaths, !ok
}
}
func songNode(lookup map[string]*Node, dir *PathMeta, file *PathMeta, uriPaths []string) (*Node, []string, bool) {
artist := file.Metadata.AlbumArtist()
song := file.Metadata.Song()
songArtist := file.Metadata.Artist()
if songArtist != artist {
song = songArtist + " - " + song
}
songURI := encodeFileURI(file.Path)
songNode := &Node{
Name: song,
URI: songURI,
}
return songNode, uriPaths, true
}