navidrome/engine/common.go

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

166 lines
3.1 KiB
Go
Raw Normal View History

2016-03-10 00:28:11 +01:00
package engine
import (
2020-04-06 06:26:51 +02:00
"context"
2016-03-24 01:53:28 +01:00
"fmt"
2016-03-10 00:28:11 +01:00
"time"
2016-03-11 15:10:40 +01:00
"github.com/deluan/navidrome/consts"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/model"
2016-03-10 00:28:11 +01:00
)
2016-03-11 15:10:40 +01:00
type Entry struct {
2016-03-10 00:28:11 +01:00
Id string
Title string
IsDir bool
Parent string
Album string
Year int
Artist string
Genre string
CoverArt string
Starred time.Time
Track int
Duration int
Size int
2016-03-10 00:28:11 +01:00
Suffix string
BitRate int
ContentType string
Path string
PlayCount int32
DiscNumber int
Created time.Time
AlbumId string
ArtistId string
Type string
2016-03-21 15:24:40 +01:00
UserRating int
2016-03-28 02:13:00 +02:00
SongCount int
UserName string
MinutesAgo int
PlayerId int
PlayerName string
2016-03-28 16:01:43 +02:00
AlbumCount int
AbsolutePath string
2016-03-10 00:28:11 +01:00
}
type Entries []Entry
func FromArtist(ar *model.Artist) Entry {
2016-03-28 16:01:43 +02:00
e := Entry{}
e.Id = ar.ID
2016-03-28 16:01:43 +02:00
e.Title = ar.Name
e.AlbumCount = ar.AlbumCount
e.IsDir = true
if ar.Starred {
e.Starred = ar.StarredAt
}
2016-03-28 16:01:43 +02:00
return e
}
func FromAlbum(al *model.Album) Entry {
e := Entry{}
e.Id = al.ID
e.Title = al.Name
e.IsDir = true
e.Parent = al.AlbumArtistID
e.Album = al.Name
e.Year = al.MaxYear
e.Artist = al.AlbumArtist
e.Genre = al.Genre
e.CoverArt = al.CoverArtId
e.Created = al.CreatedAt
e.AlbumId = al.ID
e.ArtistId = al.AlbumArtistID
e.Duration = int(al.Duration)
2016-03-28 02:13:00 +02:00
e.SongCount = al.SongCount
if al.Starred {
e.Starred = al.StarredAt
}
e.PlayCount = int32(al.PlayCount)
e.UserRating = al.Rating
return e
2016-03-11 15:10:40 +01:00
}
func FromMediaFile(mf *model.MediaFile) Entry {
e := Entry{}
e.Id = mf.ID
e.Title = mf.Title
e.IsDir = false
e.Parent = mf.AlbumID
e.Album = mf.Album
e.Year = mf.Year
e.Artist = mf.Artist
e.Genre = mf.Genre
e.Track = mf.TrackNumber
e.Duration = int(mf.Duration)
e.Size = mf.Size
e.Suffix = mf.Suffix
e.BitRate = mf.BitRate
2016-03-11 15:10:40 +01:00
if mf.HasCoverArt {
e.CoverArt = mf.ID
2016-03-11 15:10:40 +01:00
}
e.ContentType = mf.ContentType()
e.AbsolutePath = mf.Path
// Creates a "pseudo" Path, to avoid sending absolute paths to the client
2016-03-24 01:53:28 +01:00
if mf.Path != "" {
e.Path = fmt.Sprintf("%s/%s/%s.%s", realArtistName(mf), mf.Album, mf.Title, mf.Suffix)
}
e.DiscNumber = mf.DiscNumber
e.Created = mf.CreatedAt
e.AlbumId = mf.AlbumID
e.ArtistId = mf.ArtistID
e.Type = "music"
e.PlayCount = int32(mf.PlayCount)
if mf.Starred {
e.Starred = mf.StarredAt
}
e.UserRating = mf.Rating
return e
2016-03-11 15:10:40 +01:00
}
2020-01-15 04:22:34 +01:00
func realArtistName(mf *model.MediaFile) string {
2016-03-24 01:53:28 +01:00
switch {
case mf.Compilation:
return consts.VariousArtists
2016-03-24 01:53:28 +01:00
case mf.AlbumArtist != "":
return mf.AlbumArtist
}
return mf.Artist
}
func FromAlbums(albums model.Albums) Entries {
entries := make(Entries, len(albums))
for i, al := range albums {
entries[i] = FromAlbum(&al)
}
return entries
}
2016-03-24 00:02:58 +01:00
func FromMediaFiles(mfs model.MediaFiles) Entries {
2016-03-24 00:02:58 +01:00
entries := make(Entries, len(mfs))
for i, mf := range mfs {
entries[i] = FromMediaFile(&mf)
2016-03-24 00:02:58 +01:00
}
return entries
}
func FromArtists(ars model.Artists) Entries {
entries := make(Entries, len(ars))
for i, ar := range ars {
entries[i] = FromArtist(&ar)
}
return entries
}
2020-04-06 06:26:51 +02:00
func userName(ctx context.Context) string {
user := ctx.Value("user")
if user == nil {
return "UNKNOWN"
}
usr := user.(model.User)
return usr.UserName
}