2016-03-09 14:28:11 -09:00
|
|
|
package engine
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-05 08:37:43 -08:00
|
|
|
"fmt"
|
2016-03-09 14:28:11 -09:00
|
|
|
"time"
|
2016-03-11 05:10:40 -09:00
|
|
|
|
2020-08-05 08:37:43 -08:00
|
|
|
"github.com/deluan/navidrome/consts"
|
2020-01-23 15:44:08 -09:00
|
|
|
"github.com/deluan/navidrome/model"
|
2016-03-09 14:28:11 -09:00
|
|
|
)
|
|
|
|
|
|
2016-03-11 05:10:40 -09:00
|
|
|
type Entry struct {
|
2020-08-01 08:17:06 -08: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 int64
|
|
|
|
|
Suffix string
|
|
|
|
|
BitRate int
|
|
|
|
|
ContentType string
|
|
|
|
|
Path string
|
|
|
|
|
PlayCount int32
|
|
|
|
|
DiscNumber int
|
|
|
|
|
Created time.Time
|
|
|
|
|
AlbumId string
|
|
|
|
|
ArtistId string
|
|
|
|
|
Type string
|
|
|
|
|
UserRating int
|
|
|
|
|
SongCount int
|
|
|
|
|
UserName string
|
|
|
|
|
MinutesAgo int
|
|
|
|
|
PlayerId int
|
|
|
|
|
PlayerName string
|
|
|
|
|
AlbumCount int
|
|
|
|
|
BookmarkPosition int64
|
2020-08-05 08:37:43 -08:00
|
|
|
AbsolutePath string
|
2016-03-09 14:28:11 -09:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 07:35:48 -08:00
|
|
|
type Entries []Entry
|
|
|
|
|
|
2020-01-30 18:07:02 -09:00
|
|
|
func FromMediaFile(mf *model.MediaFile) Entry {
|
2016-03-22 15:00:18 -08:00
|
|
|
e := Entry{}
|
2020-01-09 19:33:01 -09:00
|
|
|
e.Id = mf.ID
|
2016-03-22 15:00:18 -08:00
|
|
|
e.Title = mf.Title
|
|
|
|
|
e.IsDir = false
|
2020-01-09 19:33:01 -09:00
|
|
|
e.Parent = mf.AlbumID
|
2016-03-22 15:00:18 -08:00
|
|
|
e.Album = mf.Album
|
|
|
|
|
e.Year = mf.Year
|
|
|
|
|
e.Artist = mf.Artist
|
|
|
|
|
e.Genre = mf.Genre
|
|
|
|
|
e.Track = mf.TrackNumber
|
2020-02-20 13:00:56 -09:00
|
|
|
e.Duration = int(mf.Duration)
|
2016-03-22 15:00:18 -08:00
|
|
|
e.Size = mf.Size
|
|
|
|
|
e.Suffix = mf.Suffix
|
|
|
|
|
e.BitRate = mf.BitRate
|
2016-03-11 05:10:40 -09:00
|
|
|
if mf.HasCoverArt {
|
2020-01-09 19:33:01 -09:00
|
|
|
e.CoverArt = mf.ID
|
2020-07-16 13:08:52 -08:00
|
|
|
} else {
|
|
|
|
|
e.CoverArt = "al-" + mf.AlbumID
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|
2016-03-22 15:00:18 -08:00
|
|
|
e.ContentType = mf.ContentType()
|
2020-08-05 08:37:43 -08:00
|
|
|
e.AbsolutePath = mf.Path
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
2016-03-22 15:00:18 -08:00
|
|
|
e.DiscNumber = mf.DiscNumber
|
|
|
|
|
e.Created = mf.CreatedAt
|
2020-01-09 19:33:01 -09:00
|
|
|
e.AlbumId = mf.AlbumID
|
|
|
|
|
e.ArtistId = mf.ArtistID
|
2020-02-24 09:31:05 -09:00
|
|
|
e.Type = "music"
|
2020-01-28 04:22:17 -09:00
|
|
|
e.PlayCount = int32(mf.PlayCount)
|
2020-02-24 18:06:12 -09:00
|
|
|
if mf.Starred {
|
|
|
|
|
e.Starred = mf.StarredAt
|
|
|
|
|
}
|
2020-01-28 04:22:17 -09:00
|
|
|
e.UserRating = mf.Rating
|
2020-08-01 08:17:06 -08:00
|
|
|
e.BookmarkPosition = mf.BookmarkPosition
|
2016-03-22 15:00:18 -08:00
|
|
|
return e
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|
2016-03-21 07:37:56 -08:00
|
|
|
|
2020-08-05 08:37:43 -08:00
|
|
|
func realArtistName(mf *model.MediaFile) string {
|
|
|
|
|
switch {
|
|
|
|
|
case mf.Compilation:
|
|
|
|
|
return consts.VariousArtists
|
|
|
|
|
case mf.AlbumArtist != "":
|
|
|
|
|
return mf.AlbumArtist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mf.Artist
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 18:07:02 -09:00
|
|
|
func FromMediaFiles(mfs model.MediaFiles) Entries {
|
2016-03-23 15:02:58 -08:00
|
|
|
entries := make(Entries, len(mfs))
|
2020-05-13 11:32:42 -08:00
|
|
|
for i := range mfs {
|
|
|
|
|
mf := mfs[i]
|
2020-01-30 18:07:02 -09:00
|
|
|
entries[i] = FromMediaFile(&mf)
|
2016-03-23 15:02:58 -08:00
|
|
|
}
|
|
|
|
|
return entries
|
|
|
|
|
}
|