Skip to content

Commit

Permalink
Simplify cover detection in roll-up code by left-joining synthesized …
Browse files Browse the repository at this point in the history
…table
  • Loading branch information
deluan committed Jul 20, 2021
1 parent cddd1b3 commit 08840f6
Showing 1 changed file with 11 additions and 35 deletions.
46 changes: 11 additions & 35 deletions persistence/album_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,6 @@ func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, e
return res, err
}

// Return a map of mediafiles that have embedded covers for the given album ids
func (r *albumRepository) getEmbeddedCoversForAlbums(ids []string) (map[string]model.MediaFile, error) {
var mfs model.MediaFiles
coverSql := Select("album_id", "id", "path").Distinct().From("media_file").
Where(And{Eq{"has_cover_art": true}, Eq{"album_id": ids}}).
GroupBy("album_id")
err := r.queryAll(coverSql, &mfs)
if err != nil {
return nil, err
}

result := map[string]model.MediaFile{}
for _, mf := range mfs {
result[mf.AlbumID] = mf
}
return result, nil
}

func (r *albumRepository) Refresh(ids ...string) error {
chunks := utils.BreakUpStringSlice(ids, 100)
for _, chunk := range chunks {
Expand Down Expand Up @@ -178,6 +160,7 @@ type refreshAlbum struct {
}

func (r *albumRepository) refresh(ids ...string) error {
stringListIds := "('" + strings.Join(ids, "','") + "')"
var albums []refreshAlbum
sel := Select(`f.album_id as id, f.album as name, f.artist, f.album_artist, f.artist_id, f.album_artist_id,
f.sort_album_name, f.sort_artist_name, f.sort_album_artist_name, f.order_album_name, f.order_album_artist_name,
Expand All @@ -196,40 +179,33 @@ func (r *albumRepository) refresh(ids ...string) error {
group_concat(f.artist_id, ' ') as song_artist_ids,
group_concat(f.album_artist_id, ' ') as album_artist_ids,
group_concat(f.year, ' ') as years`,
"mg.genre_ids").
"cf.cover_art_id", "cf.cover_art_path",
"mfg.genre_ids").
From("media_file f").
LeftJoin("album a on f.album_id = a.id").
LeftJoin(`(select mf.album_id, group_concat(mfg.genre_id, ' ') as genre_ids from media_file_genres mfg
left join media_file mf on mf.id = mfg.media_file_id where mf.album_id in ('` +
strings.Join(ids, "','") + `') group by mf.album_id) mg on mg.album_id = f.album_id`).
LeftJoin(`(select album_id, id as cover_art_id, path as cover_art_path from media_file
where has_cover_art = true and album_id in ` + stringListIds + ` group by album_id) cf
on cf.album_id = f.album_id`).
LeftJoin(`(select mf.album_id, group_concat(genre_id, ' ') as genre_ids from media_file_genres
left join media_file mf on mf.id = media_file_id where mf.album_id in ` +
stringListIds + ` group by mf.album_id) mfg on mfg.album_id = f.album_id`).
Where(Eq{"f.album_id": ids}).GroupBy("f.album_id")
err := r.queryAll(sel, &albums)
if err != nil {
return err
}

covers, err := r.getEmbeddedCoversForAlbums(ids)
if err != nil {
return nil
}

toInsert := 0
toUpdate := 0
for _, al := range albums {
embedded, hasCoverArt := covers[al.ID]
if hasCoverArt {
al.CoverArtId = embedded.ID
al.CoverArtPath = embedded.Path
}
if !hasCoverArt || !strings.HasPrefix(conf.Server.CoverArtPriority, "embedded") {
if al.CoverArtPath == "" || !strings.HasPrefix(conf.Server.CoverArtPriority, "embedded") {
if path := getCoverFromPath(al.Path, al.CoverArtPath); path != "" {
al.CoverArtId = "al-" + al.ID
al.CoverArtPath = path
}
}

if al.CoverArtId != "" {
log.Trace(r.ctx, "Found album art", "id", al.ID, "name", al.Name, "coverArtPath", al.CoverArtPath, "coverArtId", al.CoverArtId, "hasCoverArt", hasCoverArt)
log.Trace(r.ctx, "Found album art", "id", al.ID, "name", al.Name, "coverArtPath", al.CoverArtPath, "coverArtId", al.CoverArtId)
} else {
log.Trace(r.ctx, "Could not find album art", "id", al.ID, "name", al.Name)
}
Expand Down

0 comments on commit 08840f6

Please sign in to comment.