navidrome/persistence/album_repository.go

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

427 lines
11 KiB
Go
Raw Normal View History

package persistence
2020-01-13 00:55:55 +01:00
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
2020-01-13 00:55:55 +01:00
. "github.com/Masterminds/squirrel"
2020-01-13 00:55:55 +01:00
"github.com/astaxie/beego/orm"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
2020-01-13 00:55:55 +01:00
)
type albumRepository struct {
sqlRepository
sqlRestful
2020-01-13 00:55:55 +01:00
}
func NewAlbumRepository(ctx context.Context, o orm.Ormer) model.AlbumRepository {
2020-01-13 00:55:55 +01:00
r := &albumRepository{}
r.ctx = ctx
r.ormer = o
r.tableName = "album"
r.sortMappings = map[string]string{
"name": "order_album_name asc, order_album_artist_name asc",
"artist": "compilation asc, order_album_artist_name asc, order_album_name asc",
"random": "RANDOM()",
"max_year": "max_year asc, name, order_album_name asc",
"recently_added": recentlyAddedSort(),
}
r.filterMappings = map[string]filterFunc{
2020-07-28 14:49:28 +02:00
"name": fullTextFilter,
"compilation": booleanFilter,
"artist_id": artistFilter,
"year": yearFilter,
"recently_played": recentlyPlayedFilter,
2020-08-14 19:35:28 +02:00
"starred": booleanFilter,
2021-04-07 17:04:36 +02:00
"has_rating": hasRatingFilter,
}
2020-01-13 00:55:55 +01:00
return r
}
func recentlyAddedSort() string {
if conf.Server.RecentlyAddedByModTime {
return "updated_at"
}
return "created_at"
}
2020-07-28 14:49:28 +02:00
func recentlyPlayedFilter(field string, value interface{}) Sqlizer {
return Gt{"play_count": 0}
}
2021-04-07 17:04:36 +02:00
func hasRatingFilter(field string, value interface{}) Sqlizer {
return Gt{"rating": 0}
}
func yearFilter(field string, value interface{}) Sqlizer {
return Or{
And{
Gt{"min_year": 0},
LtOrEq{"min_year": value},
GtOrEq{"max_year": value},
},
Eq{"max_year": value},
}
}
2020-03-25 23:51:13 +01:00
func artistFilter(field string, value interface{}) Sqlizer {
return Like{"all_artist_ids": fmt.Sprintf("%%%s%%", value)}
2020-03-25 23:51:13 +01:00
}
func (r *albumRepository) CountAll(options ...model.QueryOptions) (int64, error) {
2020-07-29 00:01:53 +02:00
return r.count(r.selectAlbum(), options...)
}
func (r *albumRepository) Exists(id string) (bool, error) {
return r.exists(Select().Where(Eq{"id": id}))
}
func (r *albumRepository) selectAlbum(options ...model.QueryOptions) SelectBuilder {
2021-07-16 23:15:34 +02:00
return r.newSelectWithAnnotation("album.id", options...).Columns("album.*")
2020-01-13 00:55:55 +01:00
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) Get(id string) (*model.Album, error) {
sq := r.selectAlbum().Where(Eq{"id": id})
var res model.Albums
if err := r.queryAll(sq, &res); err != nil {
2020-01-13 00:55:55 +01:00
return nil, err
}
if len(res) == 0 {
return nil, model.ErrNotFound
}
2021-07-16 23:15:34 +02:00
err := r.loadAlbumGenres(&res)
return &res[0], err
}
func (r *albumRepository) Put(m *model.Album) error {
genres := m.Genres
m.Genres = nil
defer func() { m.Genres = genres }()
_, err := r.put(m.ID, m)
if err != nil {
return err
}
return r.updateGenres(m.ID, r.tableName, genres)
2020-01-13 00:55:55 +01:00
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, error) {
2021-07-16 23:15:34 +02:00
sq := r.selectAlbum(options...).
LeftJoin("album_genres ag on album.id = ag.album_id").
LeftJoin("genre on ag.genre_id = genre.id").
GroupBy("album.id")
2020-01-31 22:47:13 +01:00
res := model.Albums{}
err := r.queryAll(sq, &res)
2021-07-16 23:15:34 +02:00
if err != nil {
return nil, err
}
err = r.loadAlbumGenres(&res)
return res, err
2020-01-13 00:55:55 +01:00
}
2020-06-29 20:17:28 +02:00
// Return a map of mediafiles that have embedded covers for the given album ids
func (r *albumRepository) getEmbeddedCovers(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
}
2020-01-16 22:53:48 +01:00
func (r *albumRepository) Refresh(ids ...string) error {
chunks := utils.BreakUpStringSlice(ids, 100)
for _, chunk := range chunks {
err := r.refresh(chunk...)
if err != nil {
return err
}
}
return nil
}
const zwsp = string('\u200b')
type refreshAlbum struct {
model.Album
CurrentId string
SongArtists string
SongArtistIds string
AlbumArtistIds string
2021-07-16 23:15:34 +02:00
GenreIds string
Years string
DiscSubtitles string
Comments string
Path string
MaxUpdatedAt string
MaxCreatedAt string
}
func (r *albumRepository) refresh(ids ...string) error {
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,
f.path, f.mbz_album_artist_id, f.mbz_album_type, f.mbz_album_comment, f.catalog_num, f.compilation, f.genre,
count(f.id) as song_count,
sum(f.duration) as duration,
sum(f.size) as size,
max(f.year) as max_year,
max(f.updated_at) as max_updated_at,
max(f.created_at) as max_created_at,
a.id as current_id,
group_concat(f.comment, "` + zwsp + `") as comments,
group_concat(f.mbz_album_id, ' ') as mbz_album_id,
2020-05-12 17:17:22 +02:00
group_concat(f.disc_subtitle, ' ') as disc_subtitles,
group_concat(f.artist, ' ') as song_artists,
group_concat(f.artist_id, ' ') as song_artist_ids,
group_concat(f.album_artist_id, ' ') as album_artist_ids,
2021-07-16 23:15:34 +02:00
group_concat(f.year, ' ') as years,
group_concat(mg.genre_id, ' ') as genre_ids`).
From("media_file f").
LeftJoin("album a on f.album_id = a.id").
2021-07-16 23:15:34 +02:00
LeftJoin("media_file_genres mg on mg.media_file_id = f.id").
Where(Eq{"f.album_id": ids}).GroupBy("f.album_id")
err := r.queryAll(sel, &albums)
if err != nil {
return err
}
2020-06-29 20:17:28 +02:00
covers, err := r.getEmbeddedCovers(ids)
if err != nil {
return nil
}
toInsert := 0
toUpdate := 0
for _, al := range albums {
2020-06-29 20:17:28 +02:00
embedded, hasCoverArt := covers[al.ID]
if hasCoverArt {
al.CoverArtId = embedded.ID
al.CoverArtPath = embedded.Path
}
if !hasCoverArt || !strings.HasPrefix(conf.Server.CoverArtPriority, "embedded") {
2020-06-29 16:50:38 +02:00
if path := getCoverFromPath(al.Path, al.CoverArtPath); path != "" {
al.CoverArtId = "al-" + al.ID
al.CoverArtPath = path
}
2020-06-29 20:17:28 +02:00
}
if al.CoverArtId != "" {
log.Trace(r.ctx, "Found album art", "id", al.ID, "name", al.Name, "coverArtPath", al.CoverArtPath, "coverArtId", al.CoverArtId, "hasCoverArt", hasCoverArt)
} else {
log.Trace(r.ctx, "Could not find album art", "id", al.ID, "name", al.Name)
}
// Somehow, beego cannot parse the datetimes for the query above
if al.UpdatedAt, err = time.Parse(time.RFC3339Nano, al.MaxUpdatedAt); err != nil {
al.UpdatedAt = time.Now()
}
if al.CreatedAt, err = time.Parse(time.RFC3339Nano, al.MaxCreatedAt); err != nil {
al.CreatedAt = al.UpdatedAt
}
al.AlbumArtistID, al.AlbumArtist = getAlbumArtist(al)
al.MinYear = getMinYear(al.Years)
al.MbzAlbumID = getMostFrequentMbzID(r.ctx, al.MbzAlbumID, r.tableName, al.Name)
2020-11-11 16:43:17 +01:00
al.Comment = getComment(al.Comments, zwsp)
if al.CurrentId != "" {
toUpdate++
} else {
toInsert++
}
al.AllArtistIDs = utils.SanitizeStrings(al.SongArtistIds, al.AlbumArtistID, al.ArtistID)
2020-04-24 16:13:59 +02:00
al.FullText = getFullText(al.Name, al.Artist, al.AlbumArtist, al.SongArtists,
2020-05-12 17:17:22 +02:00
al.SortAlbumName, al.SortArtistName, al.SortAlbumArtistName, al.DiscSubtitles)
2021-07-16 23:15:34 +02:00
al.Genres = getGenres(al.GenreIds)
err := r.Put(&al.Album)
if err != nil {
return err
}
}
if toInsert > 0 {
2020-01-31 23:57:06 +01:00
log.Debug(r.ctx, "Inserted new albums", "totalInserted", toInsert)
}
if toUpdate > 0 {
2020-01-31 23:57:06 +01:00
log.Debug(r.ctx, "Updated albums", "totalUpdated", toUpdate)
}
return err
2020-01-16 22:53:48 +01:00
}
2021-07-16 23:15:34 +02:00
func getGenres(genreIds string) model.Genres {
ids := strings.Fields(genreIds)
var genres model.Genres
unique := map[string]struct{}{}
for _, id := range ids {
if _, ok := unique[id]; ok {
continue
}
genres = append(genres, model.Genre{ID: id})
unique[id] = struct{}{}
}
return genres
}
func getAlbumArtist(al refreshAlbum) (id, name string) {
if !al.Compilation {
if al.AlbumArtist != "" {
return al.AlbumArtistID, al.AlbumArtist
}
return al.ArtistID, al.Artist
}
ids := strings.Split(al.AlbumArtistIds, " ")
allSame := true
previous := al.AlbumArtistID
for _, id := range ids {
if id == previous {
continue
}
allSame = false
break
}
if allSame {
return al.AlbumArtistID, al.AlbumArtist
}
return consts.VariousArtistsID, consts.VariousArtists
}
2020-11-11 16:43:17 +01:00
func getComment(comments string, separator string) string {
cs := strings.Split(comments, separator)
if len(cs) == 0 {
return ""
}
first := cs[0]
for _, c := range cs[1:] {
if first != c {
return ""
2020-11-11 16:43:17 +01:00
}
}
return first
2020-11-11 16:43:17 +01:00
}
func getMinYear(years string) int {
ys := strings.Fields(years)
sort.Strings(ys)
for _, y := range ys {
if y != "0" {
r, _ := strconv.Atoi(y)
return r
}
}
return 0
}
// GetCoverFromPath accepts a path to a file, and returns a path to an eligible cover image from the
// file's directory (as configured with CoverArtPriority). If no cover file is found, among
// available choices, or an error occurs, an empty string is returned. If HasEmbeddedCover is true,
// and 'embedded' is matched among eligible choices, GetCoverFromPath will return early with an
// empty path.
2020-06-29 16:50:38 +02:00
func getCoverFromPath(mediaPath string, embeddedPath string) string {
n, err := os.Open(filepath.Dir(mediaPath))
if err != nil {
return ""
}
defer n.Close()
names, err := n.Readdirnames(-1)
if err != nil {
return ""
}
for _, p := range strings.Split(conf.Server.CoverArtPriority, ",") {
pat := strings.ToLower(strings.TrimSpace(p))
if pat == "embedded" {
2020-06-29 16:50:38 +02:00
if embeddedPath != "" {
return ""
}
continue
}
for _, name := range names {
match, _ := filepath.Match(pat, strings.ToLower(name))
if match && utils.IsImageFile(name) {
2020-06-29 16:50:38 +02:00
return filepath.Join(filepath.Dir(mediaPath), name)
}
}
}
return ""
}
func (r *albumRepository) purgeEmpty() error {
2020-01-31 23:57:06 +01:00
del := Delete(r.tableName).Where("id not in (select distinct(album_id) from media_file)")
c, err := r.executeSQL(del)
if err == nil {
if c > 0 {
2020-01-31 23:57:06 +01:00
log.Debug(r.ctx, "Purged empty albums", "totalDeleted", c)
}
}
return err
2020-01-18 05:28:11 +01:00
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) Search(q string, offset int, size int) (model.Albums, error) {
2020-01-31 22:47:13 +01:00
results := model.Albums{}
err := r.doSearch(q, offset, size, &results, "name")
return results, err
}
2020-01-13 21:41:14 +01:00
func (r *albumRepository) Count(options ...rest.QueryOptions) (int64, error) {
return r.CountAll(r.parseRestOptions(options...))
}
func (r *albumRepository) Read(id string) (interface{}, error) {
return r.Get(id)
}
func (r *albumRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
return r.GetAll(r.parseRestOptions(options...))
}
func (r *albumRepository) EntityName() string {
return "album"
}
func (r *albumRepository) NewInstance() interface{} {
return &model.Album{}
2020-01-13 00:55:55 +01:00
}
2020-08-14 19:35:28 +02:00
func (r albumRepository) Delete(id string) error {
return r.delete(Eq{"id": id})
}
func (r albumRepository) Save(entity interface{}) (string, error) {
album := entity.(*model.Album)
id, err := r.put(album.ID, album)
2020-08-14 19:35:28 +02:00
return id, err
}
func (r albumRepository) Update(entity interface{}, cols ...string) error {
album := entity.(*model.Album)
_, err := r.put(album.ID, album)
2020-08-14 19:35:28 +02:00
return err
}
2020-01-15 04:22:34 +01:00
var _ model.AlbumRepository = (*albumRepository)(nil)
var _ model.ResourceRepository = (*albumRepository)(nil)
2020-08-14 19:35:28 +02:00
var _ rest.Persistable = (*albumRepository)(nil)