-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.go
42 lines (36 loc) · 798 Bytes
/
music.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
package musictagger
import (
"io/fs"
"os"
"path/filepath"
"github.com/dhowden/tag"
)
type Music struct {
Path string
Metadata tag.Metadata
}
// GetAllTags traverses a given directory recursively and extracts all tags it
// can find. It returns a map of album directory to music.
func GetAllTags(dir string) (map[string][]Music, error) {
tags := map[string][]Music{}
if err := filepath.WalkDir(dir, func(s string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
f, err := os.Open(s)
if err != nil {
return err
}
defer f.Close()
m, _ := tag.ReadFrom(f)
if m != nil {
tags[filepath.Dir(s)] = append(tags[filepath.Dir(s)], Music{s, m})
}
}
return nil
}); err != nil {
return tags, err
}
return tags, nil
}