-
Notifications
You must be signed in to change notification settings - Fork 3
/
scanner.go
338 lines (281 loc) · 7.44 KB
/
scanner.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Package scanner is responsible for generating a tree-like structure of
// albums and tracks based on the contents of a directory.
package scanner
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/boreq/eggplant/adapters/music/scanner/symwalk"
"github.com/boreq/eggplant/logging"
"github.com/boreq/errors"
"github.com/radovskyb/watcher"
)
// Track represents an audio track.
type Track struct {
Path string
}
func newTrack(path string) Track {
return Track{
Path: path,
}
}
// Album is a collection of songs and albums.
type Album struct {
// Thumbnail is a path of an album cover. If the thumbnail is not
// available then this field is set to an empty string.
Thumbnail string
// AccessFile is a path of an access file. If the access file is not
// available then this field is set to an empty string.
AccessFile string
// Albums uses album titles as its keys.
Albums map[string]*Album
// Tracks uses track titles as its keys.
Tracks map[string]Track
}
func newAlbum() *Album {
return &Album{
Albums: make(map[string]*Album),
Tracks: make(map[string]Track),
}
}
type Config struct {
TrackExtensions []string
ThumbnailStems []string
ThumbnailExtensions []string
}
func (c Config) Validate() error {
if len(c.TrackExtensions) == 0 {
return errors.New("missing track extensions")
}
for _, ext := range c.TrackExtensions {
if !strings.HasPrefix(ext, ".") {
return fmt.Errorf("track extension '%s' should start with a dot", ext)
}
}
if len(c.ThumbnailStems) == 0 {
return errors.New("missing thumbnail stems")
}
for _, stem := range c.ThumbnailStems {
if stem == "" {
return errors.New("passed a blank thumbnail stem")
}
}
if len(c.ThumbnailExtensions) == 0 {
return errors.New("missing thumbnail extensions")
}
for _, ext := range c.ThumbnailExtensions {
if !strings.HasPrefix(ext, ".") {
return fmt.Errorf("thumbnail extension '%s' should start with a dot", ext)
}
}
return nil
}
// Scanner watches a hard drive directory containing audio files and produces
// updates whenever its contents change.
type Scanner struct {
directory string
config Config
log logging.Logger
}
// New creates a new scanner which will watch the specified directory when
// started.
func New(directory string, config Config) (*Scanner, error) {
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid configuration")
}
l := &Scanner{
directory: directory,
config: config,
log: logging.New("scanner"),
}
return l, nil
}
// Start starts the watcher and returns a channel on which the updates are
// sent whenever available. At least one update will be sent on the channel
// immidiately after calling this method.
func (s *Scanner) Start() (<-chan Album, error) {
// fail early since the initial load carries the highest failure
// possiblity
album, err := s.load()
if err != nil {
return nil, errors.Wrap(err, "initial load failed")
}
w := watcher.New()
w.SetMaxEvents(1)
if err := w.AddRecursive(s.directory); err != nil {
return nil, errors.Wrap(err, "could not add a recursive watcher")
}
go func() {
if err := w.Start(time.Second * 10); err != nil {
s.log.Error("error starting the watcher", "err", err)
}
}()
ch := make(chan Album)
go func() {
defer close(ch)
ch <- album
for {
select {
case _, ok := <-w.Event:
if !ok {
return
}
album, err := s.load()
if err != nil {
s.log.Error("load error", "err", err)
continue
}
ch <- album
case err := <-w.Error:
s.log.Error("watcher error", "err", err)
case <-w.Closed:
return
}
}
}()
return ch, nil
}
func (s *Scanner) load() (Album, error) {
visited := make(map[string]struct{})
root := *newAlbum()
if err := symwalk.Walk(s.directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, "received an error")
}
realPath, err := filepath.EvalSymlinks(path)
if err != nil {
return errors.Wrap(err, "could not eval a symlink")
}
_, ok := visited[realPath]
if ok {
return fmt.Errorf("loop detected: '%s' visited multiple times", realPath)
}
visited[realPath] = struct{}{}
if info.Mode()&os.ModeDir != 0 { // skip directories
return nil
}
if info.Mode()&os.ModeSymlink != 0 { // skip symlinks
return nil
}
if s.isThumbnail(path) {
if err := s.addThumbnail(&root, path); err != nil {
return errors.Wrap(err, "could not add a thumbnail")
}
return nil
}
if s.isAccessFile(path) {
if err := s.addAccessFile(&root, path); err != nil {
return errors.Wrap(err, "could not add an access file")
}
return nil
}
if s.isTrack(path) {
if err := s.addTrack(&root, path); err != nil {
return errors.Wrap(err, "could not add a track")
}
return nil
}
return nil
}); err != nil {
return Album{}, errors.Wrap(err, "walk failed")
}
removeEmptyAlbums(&root)
return root, nil
}
func (s *Scanner) addTrack(root *Album, file string) error {
album, err := s.findAlbum(root, file)
if err != nil {
return errors.Wrap(err, "could not find an album")
}
title := filenameWithoutExtension(file)
album.Tracks[title] = newTrack(file)
return nil
}
func (s *Scanner) addThumbnail(root *Album, file string) error {
album, err := s.findAlbum(root, file)
if err != nil {
return errors.Wrap(err, "could not find an album")
}
album.Thumbnail = file
return nil
}
func (s *Scanner) addAccessFile(root *Album, file string) error {
album, err := s.findAlbum(root, file)
if err != nil {
return errors.Wrap(err, "could not find an album")
}
album.AccessFile = file
return nil
}
func (s *Scanner) isAccessFile(path string) bool {
_, filename := filepath.Split(path)
return filename == "eggplant.access"
}
func (s *Scanner) isThumbnail(path string) bool {
return s.hasThumbnailStem(path) && s.hasThumbnailExtension(path)
}
func (s *Scanner) hasThumbnailStem(path string) bool {
stem := filenameWithoutExtension(path)
for _, thumbnailStem := range s.config.ThumbnailStems {
if strings.EqualFold(stem, thumbnailStem) {
return true
}
}
return false
}
func (s *Scanner) hasThumbnailExtension(path string) bool {
ext := filepath.Ext(path)
for _, thumbnailExt := range s.config.ThumbnailExtensions {
if strings.EqualFold(ext, thumbnailExt) {
return true
}
}
return false
}
func (s *Scanner) isTrack(path string) bool {
ext := filepath.Ext(path)
for _, trackExt := range s.config.TrackExtensions {
if strings.EqualFold(ext, trackExt) {
return true
}
}
return false
}
func (s *Scanner) findAlbum(root *Album, file string) (*Album, error) {
relativePath, err := filepath.Rel(s.directory, file)
if err != nil {
return nil, errors.Wrap(err, "could not get a relative filepath")
}
dir, _ := filepath.Split(relativePath)
if dir == "" {
return root, nil
}
names := strings.Split(strings.Trim(dir, string(os.PathSeparator)), string(os.PathSeparator))
var album *Album = root
for _, name := range names {
child, ok := album.Albums[name]
if !ok {
child = newAlbum()
album.Albums[name] = child
}
album = child
}
return album, nil
}
func filenameWithoutExtension(file string) string {
_, filename := filepath.Split(file)
if index := strings.LastIndex(filename, "."); index >= 0 {
return filename[:index]
}
return filename
}
func removeEmptyAlbums(root *Album) {
for name, album := range root.Albums {
removeEmptyAlbums(album)
if len(album.Albums) == 0 && len(album.Tracks) == 0 {
delete(root.Albums, name)
}
}
}