Skip to content

Commit 615dee8

Browse files
committed
fix: move taglib logic from tagcommon to taglib package
1 parent e803ca1 commit 615dee8

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

.github/workflows/release.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Install dependencies
1818
run: |
1919
sudo apt update -qq
20-
sudo apt install -y -qq build-essential git sqlite libtag1-dev ffmpeg mpv zlib1g-dev
20+
sudo apt install -y -qq build-essential git sqlite3 libtag1-dev ffmpeg mpv zlib1g-dev
2121
- name: Lint
2222
uses: golangci/golangci-lint-action@v6
2323
with:

mockfs/mockfs.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"io"
89
"os"
910
"path/filepath"
1011
"strings"
@@ -368,8 +369,9 @@ func (i *TagInfo) ReplayGainTrackPeak() float32 { return 0 }
368369
func (i *TagInfo) ReplayGainAlbumGain() float32 { return 0 }
369370
func (i *TagInfo) ReplayGainAlbumPeak() float32 { return 0 }
370371

371-
func (i *TagInfo) Length() int { return firstInt(100, i.RawLength) }
372-
func (i *TagInfo) Bitrate() int { return firstInt(100, i.RawBitrate) }
372+
func (i *TagInfo) Length() int { return firstInt(100, i.RawLength) }
373+
func (i *TagInfo) Bitrate() int { return firstInt(100, i.RawBitrate) }
374+
func (i *TagInfo) EmbeddedCover(path string) io.Reader { return nil }
373375

374376
var _ tagcommon.Reader = (*tagReader)(nil)
375377

scanner/scanner.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -309,30 +309,15 @@ func (s *Scanner) scanDir(tx *db.DB, st *State, absPath string) error {
309309
sort.Strings(tracks)
310310
for i, basename := range tracks {
311311
absPath := filepath.Join(musicDir, relPath, basename)
312-
if err := s.populateTrackAndArtists(tx, st, i, &album, basename, absPath); err != nil {
312+
if err := s.populateTrackAndArtists(tx, st, i, &album, basename, absPath, &cover); err != nil {
313313
return fmt.Errorf("populate track %q: %w", basename, err)
314314
}
315-
316-
// This is done after track populating in case of any unexpected errors
317-
// Grabbing the first cover available is not ideal but it's the best solution at the moment
318-
if cover == "" {
319-
img := tagcommon.EmbeddedCover(absPath)
320-
if img != nil {
321-
cachePath := tagcommon.CachePath(s.cacheCoverPath, album.SID().String(), tagcommon.CoverDefaultSize)
322-
if err = tagcommon.CoverScaleAndSave(img, cachePath, tagcommon.CoverDefaultSize); err != nil {
323-
return fmt.Errorf("caching embedded art: %w", err)
324-
}
325-
326-
// This is a lazy way to do this, but is the easiest without moving too much around
327-
cover = "embedded"
328-
}
329-
}
330315
}
331316

332317
return nil
333318
}
334319

335-
func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db.Album, basename string, absPath string) error {
320+
func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db.Album, basename string, absPath string, cover *string) error {
336321
// useful to get the real create/birth time for filesystems and kernels which support it
337322
timeSpec, err := times.Stat(absPath)
338323
if err != nil {
@@ -427,6 +412,21 @@ func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db
427412
st.seenTracks[track.ID] = struct{}{}
428413
st.seenTracksNew++
429414

415+
// This is done after track populating in case of any unexpected errors
416+
// Grabbing the first cover available is not ideal but it's the best solution at the moment
417+
if *cover == "" {
418+
img := trags.EmbeddedCover(absPath)
419+
if img != nil {
420+
cachePath := tagcommon.CachePath(s.cacheCoverPath, album.SID().String(), tagcommon.CoverDefaultSize)
421+
if err = tagcommon.CoverScaleAndSave(img, cachePath, tagcommon.CoverDefaultSize); err != nil {
422+
return fmt.Errorf("caching embedded art: %w", err)
423+
}
424+
425+
// This is a lazy way to do this, but is the easiest without moving too much around
426+
*cover = "embedded"
427+
}
428+
}
429+
430430
return nil
431431
}
432432

tags/tagcommon/tagcommmon.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"path/filepath"
88

99
"github.com/disintegration/imaging"
10-
"github.com/sentriz/audiotags"
1110
)
1211

1312
var ErrUnsupported = errors.New("filetype unsupported")
@@ -31,6 +30,7 @@ type Info interface {
3130
TrackNumber() int
3231
DiscNumber() int
3332
Year() int
33+
EmbeddedCover(string) io.Reader
3434

3535
ReplayGainTrackGain() float32
3636
ReplayGainTrackPeak() float32
@@ -69,21 +69,6 @@ func CoverScaleAndSave(reader io.Reader, cachePath string, size int) error {
6969
return nil
7070
}
7171

72-
// TODO: Find a better place to put this
73-
func EmbeddedCover(absPath string) io.Reader {
74-
f, err := audiotags.Open(absPath)
75-
if err != nil {
76-
return nil
77-
}
78-
defer f.Close()
79-
80-
raw := f.ReadImageRaw()
81-
if raw == nil || raw.Len() == 0 {
82-
return nil
83-
}
84-
return raw
85-
}
86-
8772
func MustAlbum(p Info) string {
8873
if r := p.Album(); r != "" {
8974
return r

tags/taglib/taglib.go

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package taglib
22

33
import (
44
"fmt"
5+
"io"
56
"path/filepath"
67
"strconv"
78
"strings"
@@ -60,6 +61,20 @@ func (i *info) ReplayGainAlbumPeak() float32 { return flt(first(find(i.raw, "rep
6061
func (i *info) Length() int { return i.props.Length }
6162
func (i *info) Bitrate() int { return i.props.Bitrate }
6263

64+
func (i *info) EmbeddedCover(path string) io.Reader {
65+
f, err := audiotags.Open(path)
66+
if err != nil {
67+
return nil
68+
}
69+
defer f.Close()
70+
71+
raw := f.ReadImageRaw()
72+
if raw == nil || raw.Len() == 0 {
73+
return nil
74+
}
75+
return raw
76+
}
77+
6378
func first[T comparable](is []T) T {
6479
var z T
6580
for _, i := range is {

0 commit comments

Comments
 (0)