quintodrome/engine/common.go

167 lines
3.3 KiB
Go
Raw Normal View History

2016-03-09 14:28:11 -09:00
package engine
import (
2020-01-21 19:01:43 -09:00
"context"
2016-03-23 16:53:28 -08:00
"fmt"
2016-03-09 14:28:11 -09:00
"time"
2016-03-11 05:10:40 -09:00
2020-01-14 18:22:34 -09:00
"github.com/cloudsonic/sonic-server/model"
2016-03-09 14:28:11 -09:00
)
2016-03-11 05:10:40 -09:00
type Entry struct {
2016-03-09 14:28:11 -09: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 06:24:40 -08:00
UserRating int
2016-03-27 16:13:00 -08:00
SongCount int
UserName string
MinutesAgo int
PlayerId int
PlayerName string
2016-03-28 06:01:43 -08:00
AlbumCount int
AbsolutePath string
2016-03-09 14:28:11 -09:00
}
type Entries []Entry
2020-01-21 19:01:43 -09:00
func FromArtist(ar *model.Artist, ann *model.Annotation) Entry {
2016-03-28 06:01:43 -08:00
e := Entry{}
e.Id = ar.ID
2016-03-28 06:01:43 -08:00
e.Title = ar.Name
e.AlbumCount = ar.AlbumCount
e.IsDir = true
2020-01-21 19:01:43 -09:00
if ann != nil {
e.Starred = ann.StarredAt
}
2016-03-28 06:01:43 -08:00
return e
}
2020-01-21 19:01:43 -09:00
func FromAlbum(al *model.Album, ann *model.Annotation) 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.Created = al.CreatedAt
e.AlbumId = al.ID
e.ArtistId = al.ArtistID
e.Duration = al.Duration
2016-03-27 16:13:00 -08:00
e.SongCount = al.SongCount
2020-01-21 19:01:43 -09:00
if ann != nil {
e.Starred = ann.StarredAt
e.PlayCount = int32(ann.PlayCount)
e.UserRating = ann.Rating
}
return e
2016-03-11 05:10:40 -09:00
}
2020-01-21 19:01:43 -09:00
func FromMediaFile(mf *model.MediaFile, ann *model.Annotation) 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
2016-03-11 05:10:40 -09:00
if mf.HasCoverArt {
e.CoverArt = mf.ID
2016-03-11 05:10:40 -09:00
}
e.ContentType = mf.ContentType()
e.AbsolutePath = mf.Path
// Creates a "pseudo" Path, to avoid sending absolute paths to the client
2016-03-23 16:53:28 -08: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" // TODO Hardcoded for now
2020-01-21 19:01:43 -09:00
if ann != nil {
e.PlayCount = int32(ann.PlayCount)
e.Starred = ann.StarredAt
e.UserRating = ann.Rating
}
return e
2016-03-11 05:10:40 -09:00
}
2020-01-14 18:22:34 -09:00
func realArtistName(mf *model.MediaFile) string {
2016-03-23 16:53:28 -08:00
switch {
case mf.Compilation:
return "Various Artists"
case mf.AlbumArtist != "":
return mf.AlbumArtist
}
return mf.Artist
}
2020-01-21 19:01:43 -09:00
func FromAlbums(albums model.Albums, annMap model.AnnotationMap) Entries {
entries := make(Entries, len(albums))
for i, al := range albums {
2020-01-21 19:01:43 -09:00
ann := annMap[al.ID]
entries[i] = FromAlbum(&al, &ann)
}
return entries
}
2016-03-23 15:02:58 -08:00
2020-01-21 19:01:43 -09:00
func FromMediaFiles(mfs model.MediaFiles, annMap model.AnnotationMap) Entries {
2016-03-23 15:02:58 -08:00
entries := make(Entries, len(mfs))
for i, mf := range mfs {
2020-01-21 19:01:43 -09:00
ann := annMap[mf.ID]
entries[i] = FromMediaFile(&mf, &ann)
2016-03-23 15:02:58 -08:00
}
return entries
}
2020-01-21 19:01:43 -09:00
func FromArtists(ars model.Artists, annMap model.AnnotationMap) Entries {
entries := make(Entries, len(ars))
for i, ar := range ars {
2020-01-21 19:01:43 -09:00
ann := annMap[ar.ID]
entries[i] = FromArtist(&ar, &ann)
}
return entries
}
2020-01-21 19:01:43 -09:00
func getUserID(ctx context.Context) string {
user, ok := ctx.Value("user").(*model.User)
if ok {
return user.ID
}
return ""
}