navidrome/engine/common.go

125 lines
2.4 KiB
Go
Raw Normal View History

2016-03-10 00:28:11 +01:00
package engine
import (
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/gosonic/domain"
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 string
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
UserName string
MinutesAgo int
PlayerId int
PlayerName string
2016-03-10 00:28:11 +01:00
}
type Entries []Entry
2016-03-11 15:10:40 +01:00
func FromAlbum(al *domain.Album) Entry {
e := Entry{}
e.Id = al.Id
e.Title = al.Name
e.IsDir = true
e.Parent = al.ArtistId
e.Album = al.Name
e.Year = al.Year
e.Artist = al.AlbumArtist
e.Genre = al.Genre
e.CoverArt = al.CoverArtId
e.Starred = al.StarredAt
e.PlayCount = int32(al.PlayCount)
e.Created = al.CreatedAt
e.AlbumId = al.Id
e.ArtistId = al.ArtistId
e.UserRating = al.Rating
e.Duration = al.Duration
return e
2016-03-11 15:10:40 +01:00
}
func FromMediaFile(mf *domain.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 = mf.Duration
e.Size = mf.Size
e.Suffix = mf.Suffix
e.BitRate = mf.BitRate
e.Starred = mf.StarredAt
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()
2016-03-24 01:53:28 +01:00
// Creates a "pseudo" path, to avoid sending absolute paths to the client
if mf.Path != "" {
e.Path = fmt.Sprintf("%s/%s/%s.%s", realArtistName(mf), mf.Album, mf.Title, mf.Suffix)
}
e.PlayCount = int32(mf.PlayCount)
e.DiscNumber = mf.DiscNumber
e.Created = mf.CreatedAt
e.AlbumId = mf.AlbumId
e.ArtistId = mf.ArtistId
e.Type = "music" // TODO Hardcoded for now
e.UserRating = mf.Rating
return e
2016-03-11 15:10:40 +01:00
}
2016-03-24 01:53:28 +01:00
func realArtistName(mf *domain.MediaFile) string {
switch {
case mf.Compilation:
return "Various Artists"
case mf.AlbumArtist != "":
return mf.AlbumArtist
}
return mf.Artist
}
func FromAlbums(albums domain.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 domain.MediaFiles) Entries {
entries := make(Entries, len(mfs))
for i, mf := range mfs {
entries[i] = FromMediaFile(&mf)
}
return entries
}