forked from ironsmile/euterpe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_library.go
817 lines (649 loc) · 16.6 KB
/
local_library.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
package library
import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/howeyc/fsnotify"
taglib "github.com/landr0id/go-taglib"
_ "github.com/mattn/go-sqlite3"
"github.com/ironsmile/httpms/src/helpers"
)
// Will be used in case some media tag is missing. As a consequence
// if there are many files with missing title, artist and album only
// one of them will be saved in the library.
const UNKNOWN_LABEL = "Unknown"
// Implements the Library interface. Will represent files found on the local storage
type LocalLibrary struct {
database string // The location of the library's database
paths []string // FS locations which contain the library's media files
db *sql.DB // Database handler
walkWG sync.WaitGroup // Used to log how much time scanning took
scanWG sync.WaitGroup // Used in WaitScan to signal the end of scanning
// If something needs to be added to the database from the watcher
// it should use this channel
watchMediaChan chan string
// Used to sync the library's Close with the watcher event handling goroutine
watchClosedChan chan bool
// Directory watcher
watch *fsnotify.Watcher
}
// Closes the database connection. It is safe to call it as many times as you want.
func (lib *LocalLibrary) Close() {
lib.stopWatcher()
if lib.db != nil {
lib.WaitScan()
lib.db.Close()
lib.db = nil
}
}
func (lib *LocalLibrary) AddLibraryPath(path string) {
_, err := os.Stat(path)
if err != nil {
log.Println(err)
return
}
lib.paths = append(lib.paths, path)
}
// Does a search in the library. Will match against the track's name, artist and album.
func (lib *LocalLibrary) Search(searchTerm string) []SearchResult {
var output []SearchResult
searchTerm = fmt.Sprintf("%%%s%%", searchTerm)
rows, err := lib.db.Query(`
SELECT
t.id as track_id,
t.name as track,
al.name as album,
at.name as artist,
t.number as track_number,
t.album_id as album_id
FROM
tracks as t
LEFT JOIN albums as al ON al.id = t.album_id
LEFT JOIN artists as at ON at.id = t.artist_id
WHERE
t.name LIKE ? OR
al.name LIKE ? OR
at.name LIKE ?
ORDER BY
al.name, t.number
`, searchTerm, searchTerm, searchTerm)
if err != nil {
log.Printf("Query not successful: %s\n", err.Error())
return output
}
defer rows.Close()
for rows.Next() {
var res SearchResult
rows.Scan(&res.ID, &res.Title, &res.Album, &res.Artist,
&res.TrackNumber, &res.AlbumID)
output = append(output, res)
}
return output
}
// Returns the filsystem path for a file specified by its ID.
func (lib *LocalLibrary) GetFilePath(ID int64) string {
smt, err := lib.db.Prepare(`
SELECT
fs_path
FROM
tracks
WHERE
id = ?
`)
if err != nil {
log.Println(err)
return ""
}
defer smt.Close()
var filePath string
err = smt.QueryRow(ID).Scan(&filePath)
if err != nil {
log.Println(err)
return ""
}
return filePath
}
// Satisfies the Library interface
func (lib *LocalLibrary) GetAlbumFiles(albumID int64) []SearchResult {
var output []SearchResult
rows, err := lib.db.Query(`
SELECT
t.id as track_id,
t.name as track,
al.name as album,
at.name as artist,
t.number as track_number,
t.album_id as album_id
FROM
tracks as t
LEFT JOIN albums as al ON al.id = t.album_id
LEFT JOIN artists as at ON at.id = t.artist_id
WHERE
t.album_id = ?
ORDER BY
al.name, t.number
`, albumID)
if err != nil {
log.Printf("Query not successful: %s\n", err.Error())
return output
}
defer rows.Close()
for rows.Next() {
var res SearchResult
rows.Scan(&res.ID, &res.Title, &res.Album, &res.Artist,
&res.TrackNumber, &res.AlbumID)
output = append(output, res)
}
return output
}
//!TODO: make scan also remove files which have been deleted since the previous scan
// Scans all of the folders in paths for media files. New files will be added to the
// database.
func (lib *LocalLibrary) Scan() {
// Make sure there are no other scans working at the moment
lib.WaitScan()
start := time.Now()
mediaChan := make(chan string, 100)
lib.scanWG.Add(1)
go lib.databaseWriter(mediaChan, &lib.scanWG)
lib.initializeWatcher()
for _, path := range lib.paths {
lib.walkWG.Add(1)
go lib.scanPath(path, mediaChan)
}
lib.scanWG.Add(1)
go func() {
defer func() {
log.Printf("Walking took %s", time.Since(start))
lib.scanWG.Done()
}()
lib.walkWG.Wait()
close(mediaChan)
}()
go func() {
lib.WaitScan()
log.Printf("Scaning took %s", time.Since(start))
}()
}
// Blocks the current goroutine until the scan has been finished
func (lib *LocalLibrary) WaitScan() {
lib.scanWG.Wait()
}
// Creates the directory watcher if none was created before. On failure logs the
// problem and leaves the watcher unintialized. LocalLibrary should work even
// without a watch.
func (lib *LocalLibrary) initializeWatcher() {
if lib.watch != nil {
return
}
newWatcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("Directory watcher was not initialized properly. ")
log.Printf("New files will not be added to the library. Reason: ")
log.Println(err)
return
}
lib.watch = newWatcher
lib.watchMediaChan = make(chan string, 100)
lib.watchClosedChan = make(chan bool)
go lib.databaseWriter(lib.watchMediaChan, nil)
go lib.watchEventRoutine()
}
// Stops the filesystem watching and all supporing it goroutines.
func (lib *LocalLibrary) stopWatcher() {
if lib.watch != nil {
lib.watchClosedChan <- true
lib.watch.Close()
lib.watch = nil
close(lib.watchMediaChan)
close(lib.watchClosedChan)
}
}
// This function is resposible for selecting the watcher events
func (lib *LocalLibrary) watchEventRoutine() {
// To make sure we will not write in the database at the same time as the
// scanning goroutines we will wait them to end.
lib.WaitScan()
defer func() {
log.Println("Directory watcher event receiver stopped.")
}()
if lib.watch == nil {
return
}
for {
select {
case ev := <-lib.watch.Event:
if ev == nil {
return
}
lib.handleWatchEvent(ev)
case err := <-lib.watch.Error:
if err == nil {
return
}
log.Println("Directory watcher error:", err)
case <-lib.watchClosedChan:
return
}
}
}
// Deals with the watcher events.
// * new directories should be watched and they themselves scanned
// * new files should be added to the library
// * deleted files should be removed from the library
// * deleted directories should be unwatched
// * modfied files should be updated in the database
// * renamed ...
func (lib *LocalLibrary) handleWatchEvent(event *fsnotify.FileEvent) {
// log.Println("Watch event:", event)
if event.IsAttrib() {
// The event was just an attribute change
return
}
st, stErr := os.Stat(event.Name)
if stErr != nil && !event.IsRename() && !event.IsDelete() {
log.Printf("Watch event stat received error: %s\n", stErr.Error())
return
}
if event.IsCreate() && st.IsDir() {
lib.watch.Watch(event.Name)
lib.walkWG.Add(1)
go lib.scanPath(event.Name, lib.watchMediaChan)
return
}
if event.IsCreate() && !st.IsDir() {
if lib.isSupportedFormat(event.Name) {
lib.watchMediaChan <- event.Name
}
return
}
if event.IsModify() && !st.IsDir() {
if lib.isSupportedFormat(event.Name) {
lib.removeFile(event.Name)
lib.watchMediaChan <- event.Name
}
return
}
if event.IsDelete() || event.IsRename() {
if lib.isSupportedFormat(event.Name) {
// This is a file
lib.removeFile(event.Name)
return
} else {
// It was a directory... probably
lib.watch.RemoveWatch(event.Name)
lib.removeDirectory(event.Name)
return
}
return
}
}
// Removes the file from the library. That means finding it in the database and
// removing it from there.
// filePath argument should be absolute path.
func (lib *LocalLibrary) removeFile(filePath string) {
_, err := lib.db.Exec(`
DELETE FROM tracks
WHERE fs_path = ?
`, filePath)
if err != nil {
log.Printf("Error removing %s: %s\n", filePath, err.Error())
}
}
// Removes files which belong in this directory from the library.
func (lib *LocalLibrary) removeDirectory(dirPath string) {
// Adding slash at the end to make sure we are always removing directories
deleteMatch := fmt.Sprintf("%s/%%", strings.TrimRight(dirPath, "/"))
_, err := lib.db.Exec(`
DELETE FROM tracks
WHERE fs_path LIKE ?
`, deleteMatch)
if err != nil {
log.Printf("Error removing %s: %s\n", dirPath, err.Error())
}
}
// Reads from the media channel and saves into the database every file
// received.
func (lib *LocalLibrary) databaseWriter(media <-chan string, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
for filename := range media {
lib.AddMedia(filename)
}
}
// This is the goroutine which actually scans a library path.
// For now it ignores everything but the list of supported files. It is so
// because jplayer cannot play anything else. Sends every suitable
// file into the media channel
func (lib *LocalLibrary) scanPath(scannedPath string, media chan<- string) {
start := time.Now()
defer func() {
log.Printf("Walking %s took %s", scannedPath, time.Since(start))
lib.walkWG.Done()
}()
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Println(err)
return nil
}
if lib.isSupportedFormat(path) {
media <- path
}
if lib.watch != nil && info.IsDir() {
//log.Printf("Adding watch for %s\n", path)
lib.watch.Watch(path)
}
return nil
}
err := filepath.Walk(scannedPath, walkFunc)
if err != nil {
log.Println(err)
}
}
// Determines if the file will be saved to the database. Only media files which
// jplayer can use are saved.
func (lib *LocalLibrary) isSupportedFormat(path string) bool {
supportedFormats := []string{
".mp3",
".ogg",
".oga",
".wav",
".fla",
".flac",
".m4a",
}
for _, format := range supportedFormats {
if !strings.HasSuffix(path, format) {
continue
}
return true
break
}
return false
}
// Adds a file specified by its filesystem name to the library. Will create the
// needed Artist, Album if neccessery.
func (lib *LocalLibrary) AddMedia(filename string) error {
if lib.MediaExistsInLibrary(filename) {
return nil
}
_, err := os.Stat(filename)
if err != nil {
return err
}
file, err := taglib.Read(filename)
if err != nil {
return fmt.Errorf("Taglib error for %s: %s", filename, err.Error())
}
defer file.Close()
// log.Printf("New Song:\nArtist: %s\nAlbum: %s\nTitle: %s\nTrack: %d\n",
// file.Artist(), file.Album(), file.Title(), int(file.Track()))
artistID, err := lib.setArtistID(file.Artist())
if err != nil {
return err
}
albumID, err := lib.setAlbumID(file.Album(), artistID)
if err != nil {
return err
}
_, err = lib.setTrackID(file.Title(), filename, int64(file.Track()),
artistID, albumID)
if err != nil {
return err
}
return nil
}
// Checks if the media file with file system path "filename" has been added to the
// library already.
func (lib *LocalLibrary) MediaExistsInLibrary(filename string) bool {
smt, err := lib.db.Prepare(`
SELECT
count(id)
FROM
tracks
WHERE
fs_path = ?
`)
if err != nil {
log.Println(err)
return false
}
defer smt.Close()
var count int
err = smt.QueryRow(filename).Scan(&count)
if err != nil {
log.Println(err)
return false
}
return count >= 1
}
// Returns the id for this artist. When missing or on error returns that error.
func (lib *LocalLibrary) GetArtistID(artist string) (int64, error) {
smt, err := lib.db.Prepare(`
SELECT
id
FROM
artists
WHERE
name = ?
`)
if err != nil {
return 0, err
}
defer smt.Close()
var id int64
err = smt.QueryRow(artist).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
// Sets a new ID for this artist if it is new to the library. If not, returns
// its current id.
func (lib *LocalLibrary) setArtistID(artist string) (int64, error) {
if len(artist) < 1 {
artist = UNKNOWN_LABEL
}
id, err := lib.GetArtistID(artist)
if err == nil {
return id, nil
}
stmt, err := lib.db.Prepare(`
INSERT INTO
artists (name)
VALUES
(?)
`)
if err != nil {
return 0, err
}
defer stmt.Close()
_, err = stmt.Exec(artist)
if err != nil {
return 0, err
}
return lib.lastInsertID()
}
// Returns the id for this artist's album. When missing or on error returns that error.
func (lib *LocalLibrary) GetAlbumID(album string, artistID int64) (int64, error) {
smt, err := lib.db.Prepare(`
SELECT
id
FROM
albums
WHERE
name = ? AND
artist_id = ?
`)
if err != nil {
return 0, err
}
defer smt.Close()
var id int64
err = smt.QueryRow(album, artistID).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
// Sets a new ID for this album if it is new to the library. If not, returns
// its current id. Albums with the same name but by different artists need to have
// separate IDs hence the artistID parameter.
func (lib *LocalLibrary) setAlbumID(album string, artistID int64) (int64, error) {
if len(album) < 1 {
album = UNKNOWN_LABEL
}
id, err := lib.GetAlbumID(album, artistID)
if err == nil {
return id, nil
}
stmt, err := lib.db.Prepare(`
INSERT INTO
albums (name, artist_id)
VALUES
(?, ?)
`)
if err != nil {
return 0, err
}
defer stmt.Close()
_, err = stmt.Exec(album, artistID)
if err != nil {
return 0, err
}
return lib.lastInsertID()
}
// Returns the id for this track. When missing or on error returns that error.
func (lib *LocalLibrary) GetTrackID(title string,
artistID, albumID int64) (int64, error) {
smt, err := lib.db.Prepare(`
SELECT
id
FROM
tracks
WHERE
name = ? AND
artist_id = ? AND
album_id = ?
`)
if err != nil {
return 0, err
}
defer smt.Close()
var id int64
err = smt.QueryRow(title, artistID, albumID).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
// Sets a new ID for this track if it is new to the library. If not, returns
// its current id. Tracks with the same name but by different artists and/or album
// need to have separate IDs hence the artistID and albumID parameters.
// Additionally trackNumber and filesystem path (fs_path) are required. They are
// used when retreiving this particular song for playing.
func (lib *LocalLibrary) setTrackID(title, fs_path string,
trackNumber, artistID, albumID int64) (int64, error) {
if len(title) < 1 {
title = filepath.Base(fs_path)
}
id, err := lib.GetTrackID(title, artistID, albumID)
if err == nil {
return id, nil
}
stmt, err := lib.db.Prepare(`
INSERT INTO
tracks (name, album_id, artist_id, fs_path, number)
VALUES
(?, ?, ?, ?, ?)
`)
if err != nil {
return 0, err
}
defer stmt.Close()
_, err = stmt.Exec(title, albumID, artistID, fs_path, trackNumber)
if err != nil {
return 0, err
}
return lib.lastInsertID()
}
// Returns the last ID insert in the database.
func (lib *LocalLibrary) lastInsertID() (int64, error) {
var id int64
err := lib.db.QueryRow("SELECT last_insert_rowid();").Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
// Should be run once every time a library is created. It checks for the
// sqlite database file and creates one if it is absent. If a file is found
// it does nothing.
func (lib *LocalLibrary) Initialize() error {
_, err := os.Stat(lib.database)
if err == nil {
return nil
}
sqlSchema, err := lib.readSchema()
if err != nil {
return err
}
if lib.db == nil {
return errors.New("Library is not opened. Call its Open method first.")
}
queries := strings.Split(sqlSchema, ";")
for _, query := range queries {
query = strings.TrimSpace(query)
if len(query) < 1 {
continue
}
_, err = lib.db.Exec(query)
if err != nil {
return err
}
}
return nil
}
// Returns the SQL schema for the library. It is stored in the project root directory
// under sqls/library_schema.sql
func (lib *LocalLibrary) readSchema() (string, error) {
projRoot, err := helpers.ProjectRoot()
if err != nil {
return "", err
}
sqlSchemaPath := filepath.Join(projRoot, "sqls", "library_schema.sql")
outBytes, err := ioutil.ReadFile(sqlSchemaPath)
if err != nil {
return "", err
}
out := string(outBytes)
if len(out) < 1 {
return "", errors.New("SQL schema was empty")
}
return out, nil
}
func (lib *LocalLibrary) Truncate() error {
lib.Close()
return os.Remove(lib.database)
}
// Returns a new LocalLibrary which will use for database the file specified by
// databasePath. Also creates the database connection so you does not need to
// worry about that.
func NewLocalLibrary(databasePath string) (*LocalLibrary, error) {
lib := new(LocalLibrary)
lib.database = databasePath
var err error
lib.db, err = sql.Open("sqlite3", lib.database)
if err != nil {
return nil, err
}
return lib, nil
}