navidrome/scanner/refresher.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

135 lines
3.2 KiB
Go
Raw Normal View History

2022-12-19 18:08:39 +01:00
package scanner
import (
"context"
"fmt"
"path/filepath"
"strings"
2022-12-19 18:08:39 +01:00
"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/slice"
)
2022-12-21 17:30:19 +01:00
// refresher is responsible for rolling up mediafiles attributes into albums attributes,
// and albums attributes into artists attributes. This is done by accumulating all album and artist IDs
// found during scan, and "refreshing" the albums and artists when flush is called.
//
// The actual mappings happen in MediaFiles.ToAlbum() and Albums.ToAlbumArtist()
2022-12-19 18:08:39 +01:00
type refresher struct {
ctx context.Context
ds model.DataStore
album map[string]struct{}
artist map[string]struct{}
dirMap dirMap
2022-12-19 18:08:39 +01:00
}
func newRefresher(ctx context.Context, ds model.DataStore, dirMap dirMap) *refresher {
2022-12-19 18:08:39 +01:00
return &refresher{
ctx: ctx,
ds: ds,
album: map[string]struct{}{},
artist: map[string]struct{}{},
dirMap: dirMap,
2022-12-19 18:08:39 +01:00
}
}
2022-12-21 17:30:19 +01:00
func (r *refresher) accumulate(mf model.MediaFile) {
2022-12-19 18:08:39 +01:00
if mf.AlbumID != "" {
2022-12-21 17:30:19 +01:00
r.album[mf.AlbumID] = struct{}{}
2022-12-19 18:08:39 +01:00
}
if mf.AlbumArtistID != "" {
2022-12-21 17:30:19 +01:00
r.artist[mf.AlbumArtistID] = struct{}{}
2022-12-19 18:08:39 +01:00
}
}
2022-12-21 17:30:19 +01:00
func (r *refresher) flush() error {
err := r.flushMap(r.album, "album", r.refreshAlbums)
if err != nil {
return err
}
err = r.flushMap(r.artist, "artist", r.refreshArtists)
if err != nil {
return err
}
return nil
}
2022-12-19 18:08:39 +01:00
type refreshCallbackFunc = func(ids ...string) error
2022-12-21 17:30:19 +01:00
func (r *refresher) flushMap(m map[string]struct{}, entity string, refresh refreshCallbackFunc) error {
2022-12-19 18:08:39 +01:00
if len(m) == 0 {
return nil
}
var ids []string
for id := range m {
ids = append(ids, id)
delete(m, id)
}
chunks := utils.BreakUpStringSlice(ids, 100)
for _, chunk := range chunks {
2022-12-21 17:30:19 +01:00
err := refresh(chunk...)
2022-12-19 18:08:39 +01:00
if err != nil {
2022-12-21 17:30:19 +01:00
log.Error(r.ctx, fmt.Sprintf("Error writing %ss to the DB", entity), err)
2022-12-19 18:08:39 +01:00
return err
}
}
return nil
}
2022-12-21 17:30:19 +01:00
func (r *refresher) refreshAlbums(ids ...string) error {
mfs, err := r.ds.MediaFile(r.ctx).GetAll(model.QueryOptions{Filters: squirrel.Eq{"album_id": ids}})
2022-12-19 18:08:39 +01:00
if err != nil {
return err
}
if len(mfs) == 0 {
return nil
}
2022-12-21 17:30:19 +01:00
repo := r.ds.Album(r.ctx)
2022-12-19 18:08:39 +01:00
grouped := slice.Group(mfs, func(m model.MediaFile) string { return m.AlbumID })
for _, group := range grouped {
songs := model.MediaFiles(group)
a := songs.ToAlbum()
2022-12-21 17:30:19 +01:00
a.ImageFiles = r.getImageFiles(songs.Dirs())
2022-12-19 18:08:39 +01:00
err := repo.Put(&a)
if err != nil {
return err
}
}
return nil
}
2022-12-21 17:30:19 +01:00
func (r *refresher) getImageFiles(dirs []string) string {
var imageFiles []string
for _, dir := range dirs {
2022-12-21 17:30:19 +01:00
for _, img := range r.dirMap[dir].Images {
imageFiles = append(imageFiles, filepath.Join(dir, img))
}
}
return strings.Join(imageFiles, string(filepath.ListSeparator))
}
2022-12-21 17:30:19 +01:00
func (r *refresher) refreshArtists(ids ...string) error {
albums, err := r.ds.Album(r.ctx).GetAll(model.QueryOptions{Filters: squirrel.Eq{"album_artist_id": ids}})
2022-12-19 18:08:39 +01:00
if err != nil {
return err
}
2022-12-21 17:30:19 +01:00
if len(albums) == 0 {
return nil
}
repo := r.ds.Artist(r.ctx)
grouped := slice.Group(albums, func(al model.Album) string { return al.AlbumArtistID })
for _, group := range grouped {
a := model.Albums(group).ToAlbumArtist()
err := repo.Put(&a)
if err != nil {
return err
}
2022-12-19 18:08:39 +01:00
}
return nil
}