-
Notifications
You must be signed in to change notification settings - Fork 32
/
walk.go
265 lines (235 loc) · 4.83 KB
/
walk.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Package walk implements a path walker which reads audio files under a path and
// constructs an index.Library from supported metadata tags (see github.com/dhowden/tag).
package walk
import (
"crypto/sha1"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/dhowden/tag"
"tchaik.com/index"
)
var fileExtensions = []string{".mp3", ".m4a", ".flac", ".ogg"}
func validFiles(in <-chan string) <-chan string {
out := make(chan string)
go func() {
for path := range in {
ext := strings.ToLower(filepath.Ext(filepath.Base(path)))
for _, x := range fileExtensions {
if ext == x {
out <- path
break
}
}
}
close(out)
}()
return out
}
var workers = 4
type pathTrack struct {
path string
track *track
}
// NewLibrary constructs an index.Library by walking through the directory tree under
// the given path. Any errors are logged to stdout (TODO: fix this!)
func NewLibrary(path string) index.Library {
trackCh := make(chan pathTrack)
errCh := make(chan error)
files := validFiles(walk(path))
go func() {
for err := range errCh {
// FIXME
log.Println(err)
}
}()
process := func(files <-chan string) {
for p := range files {
t, err := processPath(p)
if err != nil {
errCh <- fmt.Errorf("error processing '%v': %v", p, err)
continue
}
trackCh <- pathTrack{p, t}
}
}
wg := &sync.WaitGroup{}
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
process(files)
wg.Done()
}()
}
go func() {
wg.Wait()
close(errCh)
close(trackCh)
}()
tracks := make(map[string]*track)
for pt := range trackCh {
tracks[pt.path] = pt.track
}
return &library{
tracks: tracks,
}
}
// library is an implementation of index.library.
type library struct {
tracks map[string]*track
}
// Track implements index.Library.
func (l *library) Track(id string) (index.Track, bool) {
t, ok := l.tracks[id]
return t, ok
}
// Tracks implements index.Library.
func (l *library) Tracks() []index.Track {
tracks := make([]index.Track, 0, len(l.tracks))
for _, t := range l.tracks {
tracks = append(tracks, t)
}
return tracks
}
// track is a wrapper around tag.Metadata which implements index.Track
type track struct {
tag.Metadata
Location string
FileInfo os.FileInfo
CreatedTime time.Time
}
// GetString implements index.Track.
func (m *track) GetString(name string) string {
switch name {
case "Name":
title := m.Title()
if title == "" {
fileName := m.FileInfo.Name()
ext := filepath.Ext(fileName)
title = strings.TrimSuffix(fileName, ext)
}
return title
case "Album":
return m.Album()
case "Artist":
return m.Artist()
case "AlbumArtist":
return m.AlbumArtist()
case "Composer":
return m.Composer()
case "Genre":
return m.Genre()
case "Location":
return m.Location
case "Kind":
return kind(m.FileType()).String()
case "ID":
sum := sha1.Sum([]byte(m.Location))
return string(fmt.Sprintf("%x", sum))
}
return ""
}
type kind tag.FileType
func (k kind) String() string {
switch tag.FileType(k) {
case tag.MP3:
return "MPEG audio file"
case tag.M4A:
return "AAC audio file"
case tag.ALAC: // FIXME: tag doesn't actually detect this at the moment.
return "Apple Lossless audio file"
case tag.FLAC:
return "FLAC audio file"
case tag.OGG:
return "OGG audio file"
}
return ""
}
// GetStrings implements index.Track.
func (m *track) GetStrings(name string) []string {
switch name {
case "Artist", "AlbumArtist", "Composer":
return index.DefaultGetStrings(m, name)
}
return nil
}
// GetInt implements index.Track.
func (m *track) GetInt(name string) int {
switch name {
case "Year":
return m.Year()
case "TrackNumber":
x, _ := m.Track()
return x
case "TrackCount":
_, n := m.Track()
return n
case "DiscNumber":
x, _ := m.Disc()
return x
case "DiscCount":
_, n := m.Disc()
return n
}
return 0
}
// GetTime implements index.Track.
func (m *track) GetTime(name string) time.Time {
switch name {
case "DateModified":
return m.FileInfo.ModTime()
case "DateAdded":
return m.CreatedTime
}
return time.Time{}
}
func walk(root string) <-chan string {
ch := make(chan string)
fn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
ch <- path
return nil
}
go func() {
err := filepath.Walk(root, fn)
if err != nil {
log.Println(err)
}
close(ch)
}()
return ch
}
func processPath(path string) (*track, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
m, err := tag.ReadFrom(f)
if err != nil {
return nil, err
}
fileInfo, err := f.Stat()
if err != nil {
return nil, err
}
createdTime, err := getCreatedTime(path)
if err != nil {
return nil, err
}
return &track{
Metadata: m,
Location: path,
FileInfo: fileInfo,
CreatedTime: createdTime,
}, nil
}