Skip to content

Commit

Permalink
fix: move taglib logic from tagcommon to taglib package
Browse files Browse the repository at this point in the history
  • Loading branch information
turtletowerz committed Feb 28, 2025
1 parent e803ca1 commit 615dee8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt update -qq
sudo apt install -y -qq build-essential git sqlite libtag1-dev ffmpeg mpv zlib1g-dev
sudo apt install -y -qq build-essential git sqlite3 libtag1-dev ffmpeg mpv zlib1g-dev
- name: Lint
uses: golangci/golangci-lint-action@v6
with:
Expand Down
6 changes: 4 additions & 2 deletions mockfs/mockfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -368,8 +369,9 @@ func (i *TagInfo) ReplayGainTrackPeak() float32 { return 0 }
func (i *TagInfo) ReplayGainAlbumGain() float32 { return 0 }
func (i *TagInfo) ReplayGainAlbumPeak() float32 { return 0 }

func (i *TagInfo) Length() int { return firstInt(100, i.RawLength) }
func (i *TagInfo) Bitrate() int { return firstInt(100, i.RawBitrate) }
func (i *TagInfo) Length() int { return firstInt(100, i.RawLength) }
func (i *TagInfo) Bitrate() int { return firstInt(100, i.RawBitrate) }
func (i *TagInfo) EmbeddedCover(path string) io.Reader { return nil }

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

Expand Down
34 changes: 17 additions & 17 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,30 +309,15 @@ func (s *Scanner) scanDir(tx *db.DB, st *State, absPath string) error {
sort.Strings(tracks)
for i, basename := range tracks {
absPath := filepath.Join(musicDir, relPath, basename)
if err := s.populateTrackAndArtists(tx, st, i, &album, basename, absPath); err != nil {
if err := s.populateTrackAndArtists(tx, st, i, &album, basename, absPath, &cover); err != nil {
return fmt.Errorf("populate track %q: %w", basename, err)
}

// This is done after track populating in case of any unexpected errors
// Grabbing the first cover available is not ideal but it's the best solution at the moment
if cover == "" {
img := tagcommon.EmbeddedCover(absPath)
if img != nil {
cachePath := tagcommon.CachePath(s.cacheCoverPath, album.SID().String(), tagcommon.CoverDefaultSize)
if err = tagcommon.CoverScaleAndSave(img, cachePath, tagcommon.CoverDefaultSize); err != nil {
return fmt.Errorf("caching embedded art: %w", err)
}

// This is a lazy way to do this, but is the easiest without moving too much around
cover = "embedded"
}
}
}

return nil
}

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

// This is done after track populating in case of any unexpected errors
// Grabbing the first cover available is not ideal but it's the best solution at the moment
if *cover == "" {
img := trags.EmbeddedCover(absPath)
if img != nil {
cachePath := tagcommon.CachePath(s.cacheCoverPath, album.SID().String(), tagcommon.CoverDefaultSize)
if err = tagcommon.CoverScaleAndSave(img, cachePath, tagcommon.CoverDefaultSize); err != nil {
return fmt.Errorf("caching embedded art: %w", err)
}

// This is a lazy way to do this, but is the easiest without moving too much around
*cover = "embedded"
}
}

return nil
}

Expand Down
17 changes: 1 addition & 16 deletions tags/tagcommon/tagcommmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"

"github.com/disintegration/imaging"
"github.com/sentriz/audiotags"
)

var ErrUnsupported = errors.New("filetype unsupported")
Expand All @@ -31,6 +30,7 @@ type Info interface {
TrackNumber() int
DiscNumber() int
Year() int
EmbeddedCover(string) io.Reader

ReplayGainTrackGain() float32
ReplayGainTrackPeak() float32
Expand Down Expand Up @@ -69,21 +69,6 @@ func CoverScaleAndSave(reader io.Reader, cachePath string, size int) error {
return nil
}

// TODO: Find a better place to put this
func EmbeddedCover(absPath string) io.Reader {
f, err := audiotags.Open(absPath)
if err != nil {
return nil
}
defer f.Close()

raw := f.ReadImageRaw()
if raw == nil || raw.Len() == 0 {
return nil
}
return raw
}

func MustAlbum(p Info) string {
if r := p.Album(); r != "" {
return r
Expand Down
15 changes: 15 additions & 0 deletions tags/taglib/taglib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package taglib

import (
"fmt"
"io"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -60,6 +61,20 @@ func (i *info) ReplayGainAlbumPeak() float32 { return flt(first(find(i.raw, "rep
func (i *info) Length() int { return i.props.Length }
func (i *info) Bitrate() int { return i.props.Bitrate }

func (i *info) EmbeddedCover(path string) io.Reader {
f, err := audiotags.Open(path)
if err != nil {
return nil
}
defer f.Close()

raw := f.ReadImageRaw()
if raw == nil || raw.Len() == 0 {
return nil
}
return raw
}

func first[T comparable](is []T) T {
var z T
for _, i := range is {
Expand Down

0 comments on commit 615dee8

Please sign in to comment.