No files matched your search
@@ -316,33 +316,34 @@ type Play struct {
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ModifiedAt time.Time
|
||||
LeftPath string `gorm:"unique_index:idx_album_abs_path"`
|
||||
RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"`
|
||||
RightPathUDec string `sql:"default: null"`
|
||||
Parent *Album
|
||||
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
|
||||
Genres []*Genre `gorm:"many2many:album_genres"`
|
||||
Cover string `sql:"default: null"`
|
||||
Artists []*Artist `gorm:"many2many:album_artists"`
|
||||
TagTitle string `sql:"default: null"`
|
||||
TagAlbumArtist string // display purposes only
|
||||
TagTitleUDec string `sql:"default: null"`
|
||||
TagBrainzID string `sql:"default: null"`
|
||||
TagYear int `sql:"default: null"`
|
||||
TagCompilation bool `sql:"default: null"`
|
||||
TagReleaseType string `sql:"default: null"`
|
||||
Tracks []*Track
|
||||
ChildCount int `sql:"-"`
|
||||
Duration int `sql:"-"`
|
||||
AlbumStar *AlbumStar
|
||||
AlbumRating *AlbumRating
|
||||
AverageRating float64 `sql:"default: null"`
|
||||
Play *Play
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ModifiedAt time.Time
|
||||
LeftPath string `gorm:"unique_index:idx_album_abs_path"`
|
||||
RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"`
|
||||
RightPathUDec string `sql:"default: null"`
|
||||
Parent *Album
|
||||
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
|
||||
Genres []*Genre `gorm:"many2many:album_genres"`
|
||||
Cover string `sql:"default: null"`
|
||||
EmbeddedCoverTrackID *int `sql:"default: null; type:int REFERENCES tracks(id) ON DELETE SET NULL"`
|
||||
Artists []*Artist `gorm:"many2many:album_artists"`
|
||||
TagTitle string `sql:"default: null"`
|
||||
TagAlbumArtist string // display purposes only
|
||||
TagTitleUDec string `sql:"default: null"`
|
||||
TagBrainzID string `sql:"default: null"`
|
||||
TagYear int `sql:"default: null"`
|
||||
TagCompilation bool `sql:"default: null"`
|
||||
TagReleaseType string `sql:"default: null"`
|
||||
Tracks []*Track
|
||||
ChildCount int `sql:"-"`
|
||||
Duration int `sql:"-"`
|
||||
AlbumStar *AlbumStar
|
||||
AlbumRating *AlbumRating
|
||||
AverageRating float64 `sql:"default: null"`
|
||||
Play *Play
|
||||
}
|
||||
|
||||
func (a *Album) SID() *specid.ID {
|
||||
@@ -353,6 +354,10 @@ func (a *Album) ParentSID() *specid.ID {
|
||||
return &specid.ID{Type: specid.Album, Value: a.ParentID}
|
||||
}
|
||||
|
||||
func (a *Album) EmbeddedCoverTrackSID() *specid.ID {
|
||||
return &specid.ID{Type: specid.Track, Value: *a.EmbeddedCoverTrackID}
|
||||
}
|
||||
|
||||
func (a *Album) IndexRightPath() string {
|
||||
if len(a.RightPathUDec) > 0 {
|
||||
return a.RightPathUDec
|
||||
|
||||
@@ -79,6 +79,7 @@ func (db *DB) Migrate(ctx MigrationContext) error {
|
||||
construct(ctx, "202505262025", migrateAlbumAddIndexOnBrainzID),
|
||||
construct(ctx, "202507062103", migrateAlbumCompilationReleaseType),
|
||||
construct(ctx, "202508261102", migrateAddLyrics),
|
||||
construct(ctx, "202509291741", migrateAddAlbumEmbeddedCoverTrackID),
|
||||
}
|
||||
|
||||
return gormigrate.
|
||||
@@ -857,3 +858,7 @@ func migrateAlbumCompilationReleaseType(tx *gorm.DB, _ MigrationContext) error {
|
||||
func migrateAddLyrics(tx *gorm.DB, _ MigrationContext) error {
|
||||
return tx.AutoMigrate(Track{}).Error
|
||||
}
|
||||
|
||||
func migrateAddAlbumEmbeddedCoverTrackID(tx *gorm.DB, _ MigrationContext) error {
|
||||
return tx.AutoMigrate(Album{}).Error
|
||||
}
|
||||
@@ -451,12 +451,42 @@ func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db
|
||||
return fmt.Errorf("populate track artists: %w", err)
|
||||
}
|
||||
|
||||
// embedded covers come only from the first track
|
||||
if i == 0 && album.Cover == "" && track.ID > 0 {
|
||||
if err := populateEmbeddedCover(tx, s.tagReader, album, track, absPath); err != nil {
|
||||
return fmt.Errorf("populate embedded cover: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
st.seenTracks[track.ID] = struct{}{}
|
||||
st.seenTracksNew++
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func populateEmbeddedCover(tx *db.DB, tagReader tags.Reader, album *db.Album, track *db.Track, absPath string) error {
|
||||
var trackID int
|
||||
if data, _ := tagReader.ReadCover(absPath); len(data) > 0 {
|
||||
trackID = track.ID
|
||||
}
|
||||
var prevTrackID int
|
||||
if album.EmbeddedCoverTrackID != nil {
|
||||
prevTrackID = *album.EmbeddedCoverTrackID
|
||||
}
|
||||
if prevTrackID == trackID {
|
||||
return nil
|
||||
}
|
||||
|
||||
album.EmbeddedCoverTrackID = nil
|
||||
if trackID > 0 {
|
||||
album.EmbeddedCoverTrackID = &trackID
|
||||
}
|
||||
if err := tx.Save(&album).Error; err != nil {
|
||||
return fmt.Errorf("saving album for embedded cover track id: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func populateAlbum(tx *db.DB, album *db.Album, trags map[string][]string, modTime, createTime time.Time) error {
|
||||
albumName := tags.MustAlbum(trags)
|
||||
album.TagTitle = albumName
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specidpaths"
|
||||
"go.senan.xyz/gonic/tags"
|
||||
"go.senan.xyz/gonic/transcode"
|
||||
)
|
||||
|
||||
@@ -49,7 +50,7 @@ func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *s
|
||||
_, err = os.Stat(cachePath)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
reader, err := coverFor(c.dbc, c.artistInfoCache, id)
|
||||
reader, err := coverFor(c.dbc, c.artistInfoCache, c.tagReader, id)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "couldn't find cover %q: %v", id, err)
|
||||
}
|
||||
@@ -76,7 +77,7 @@ var (
|
||||
)
|
||||
|
||||
// TODO: can we use specidpaths.Locate here?
|
||||
func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, id specid.ID) (io.ReadCloser, error) {
|
||||
func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, tagReader tags.Reader, id specid.ID) (io.ReadCloser, error) {
|
||||
switch id.Type {
|
||||
case specid.Album:
|
||||
return coverForAlbum(dbc, id.Value)
|
||||
@@ -86,6 +87,8 @@ func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, id s
|
||||
return coverForPodcast(dbc, id.Value)
|
||||
case specid.PodcastEpisode:
|
||||
return coverForPodcastEpisode(dbc, id.Value)
|
||||
case specid.Track:
|
||||
return coverForTrack(dbc, tagReader, id.Value)
|
||||
default:
|
||||
return nil, errCoverNotFound
|
||||
}
|
||||
@@ -150,6 +153,29 @@ func coverForPodcastEpisode(dbc *db.DB, id int) (*os.File, error) {
|
||||
return os.Open(filepath.Join(pe.Podcast.RootDir, pe.Podcast.Image))
|
||||
}
|
||||
|
||||
func coverForTrack(dbc *db.DB, tagReader tags.Reader, id int) (io.ReadCloser, error) {
|
||||
var tr db.Track
|
||||
err := dbc.
|
||||
Preload("Album").
|
||||
First(&tr, id).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select track: %w", err)
|
||||
}
|
||||
|
||||
absPath := tr.AbsPath()
|
||||
|
||||
cover, err := tagReader.ReadCover(absPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read cover: %w", err)
|
||||
}
|
||||
if len(cover) == 0 {
|
||||
return nil, errCoverEmpty
|
||||
}
|
||||
|
||||
return io.NopCloser(bytes.NewReader(cover)), nil
|
||||
}
|
||||
|
||||
func coverScaleAndSave(reader io.Reader, cachePath string, size int) error {
|
||||
src, err := imaging.Decode(reader)
|
||||
if err != nil {
|
||||
|
||||
@@ -30,6 +30,8 @@ func NewAlbumByFolder(f *db.Album) *Album {
|
||||
}
|
||||
if f.Cover != "" {
|
||||
a.CoverID = f.SID()
|
||||
} else if f.EmbeddedCoverTrackID != nil {
|
||||
a.CoverID = f.EmbeddedCoverTrackSID()
|
||||
}
|
||||
return a
|
||||
}
|
||||
@@ -52,7 +54,10 @@ func NewTCAlbumByFolder(f *db.Album) *TrackChild {
|
||||
}
|
||||
if f.Cover != "" {
|
||||
trCh.CoverID = f.SID()
|
||||
} else if f.EmbeddedCoverTrackID != nil {
|
||||
trCh.CoverID = f.EmbeddedCoverTrackSID()
|
||||
}
|
||||
|
||||
return trCh
|
||||
}
|
||||
|
||||
@@ -86,7 +91,10 @@ func NewTCTrackByFolder(t *db.Track, parent *db.Album) *TrackChild {
|
||||
}
|
||||
if parent.Cover != "" {
|
||||
trCh.CoverID = parent.SID()
|
||||
} else if parent.EmbeddedCoverTrackID != nil {
|
||||
trCh.CoverID = parent.EmbeddedCoverTrackSID()
|
||||
}
|
||||
|
||||
if t.Album != nil {
|
||||
trCh.Album = t.Album.RightPath
|
||||
trCh.AlbumID = t.Album.SID()
|
||||
@@ -163,6 +171,8 @@ func NewArtistByFolder(f *db.Album) *Artist {
|
||||
}
|
||||
if f.Cover != "" {
|
||||
a.CoverID = f.SID()
|
||||
} else if f.EmbeddedCoverTrackID != nil {
|
||||
a.CoverID = f.EmbeddedCoverTrackSID()
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ func NewAlbumByTags(a *db.Album, artists []*db.Artist) *Album {
|
||||
}
|
||||
if a.Cover != "" {
|
||||
ret.CoverID = a.SID()
|
||||
} else if a.EmbeddedCoverTrackID != nil {
|
||||
ret.CoverID = a.EmbeddedCoverTrackSID()
|
||||
}
|
||||
if a.AlbumStar != nil {
|
||||
ret.Starred = &a.AlbumStar.StarDate
|
||||
@@ -90,6 +92,8 @@ func NewTrackByTags(t *db.Track, album *db.Album) *TrackChild {
|
||||
}
|
||||
if album.Cover != "" {
|
||||
ret.CoverID = album.SID()
|
||||
} else if album.EmbeddedCoverTrackID != nil {
|
||||
ret.CoverID = album.EmbeddedCoverTrackSID()
|
||||
}
|
||||
if t.TrackStar != nil {
|
||||
ret.Starred = &t.TrackStar.StarDate
|
||||
|
||||
Reference in New Issue
Block a user