@@ -2,7 +2,6 @@ package main
22
33import (
44 "context"
5- "encoding/hex"
65 "encoding/json"
76 "errors"
87 "fmt"
@@ -14,9 +13,9 @@ import (
1413 "sync"
1514 "time"
1615
17- "github.com/rs/cors"
18-
1916 librespot "github.com/devgianlu/go-librespot"
17+ metadatapb "github.com/devgianlu/go-librespot/proto/spotify/metadata"
18+ "github.com/rs/cors"
2019 "nhooyr.io/websocket"
2120 "nhooyr.io/websocket/wsjson"
2221)
@@ -139,15 +138,31 @@ type ApiResponseStatusTrack struct {
139138 Name string `json:"name"`
140139 ArtistNames []string `json:"artist_names"`
141140 AlbumName string `json:"album_name"`
142- AlbumCoverUrl string `json:"album_cover_url"`
141+ AlbumCoverUrl * string `json:"album_cover_url"`
143142 Position int64 `json:"position"`
144143 Duration int `json:"duration"`
145144 ReleaseDate string `json:"release_date"`
146145 TrackNumber int `json:"track_number"`
147146 DiscNumber int `json:"disc_number"`
148147}
149148
150- func NewApiResponseStatusTrack (media * librespot.Media , prodInfo * ProductInfo , position int64 ) * ApiResponseStatusTrack {
149+ func getBestImageIdForSize (images []* metadatapb.Image , size string ) []byte {
150+ if len (images ) == 0 {
151+ return nil
152+ }
153+
154+ imageSize := metadatapb .Image_Size (metadatapb .Image_Size_value [strings .ToUpper (size )])
155+
156+ for _ , img := range images {
157+ if img .Size != nil && * img .Size == imageSize {
158+ return img .FileId
159+ }
160+ }
161+
162+ return images [0 ].FileId
163+ }
164+
165+ func (p * AppPlayer ) newApiResponseStatusTrack (media * librespot.Media , position int64 ) * ApiResponseStatusTrack {
151166 if media .IsTrack () {
152167 track := media .Track ()
153168
@@ -156,19 +171,17 @@ func NewApiResponseStatusTrack(media *librespot.Media, prodInfo *ProductInfo, po
156171 artists = append (artists , * a .Name )
157172 }
158173
159- var albumCoverId string
160- if len (track .Album .Cover ) > 0 {
161- albumCoverId = hex .EncodeToString (track .Album .Cover [0 ].FileId )
162- } else if track .Album .CoverGroup != nil && len (track .Album .CoverGroup .Image ) > 0 {
163- albumCoverId = hex .EncodeToString (track .Album .CoverGroup .Image [0 ].FileId )
174+ albumCoverId := getBestImageIdForSize (track .Album .Cover , p .app .cfg .Server .ImageSize )
175+ if albumCoverId == nil && track .Album .CoverGroup != nil {
176+ albumCoverId = getBestImageIdForSize (track .Album .CoverGroup .Image , p .app .cfg .Server .ImageSize )
164177 }
165178
166179 return & ApiResponseStatusTrack {
167180 Uri : librespot .SpotifyIdFromGid (librespot .SpotifyIdTypeTrack , track .Gid ).Uri (),
168181 Name : * track .Name ,
169182 ArtistNames : artists ,
170183 AlbumName : * track .Album .Name ,
171- AlbumCoverUrl : prodInfo .ImageUrl (albumCoverId ),
184+ AlbumCoverUrl : p . prodInfo .ImageUrl (albumCoverId ),
172185 Position : position ,
173186 Duration : int (* track .Duration ),
174187 ReleaseDate : track .Album .Date .String (),
@@ -178,17 +191,14 @@ func NewApiResponseStatusTrack(media *librespot.Media, prodInfo *ProductInfo, po
178191 } else {
179192 episode := media .Episode ()
180193
181- var albumCoverId string
182- if len (episode .CoverImage .Image ) > 0 {
183- albumCoverId = hex .EncodeToString (episode .CoverImage .Image [0 ].FileId )
184- }
194+ albumCoverId := getBestImageIdForSize (episode .CoverImage .Image , p .app .cfg .Server .ImageSize )
185195
186196 return & ApiResponseStatusTrack {
187197 Uri : librespot .SpotifyIdFromGid (librespot .SpotifyIdTypeEpisode , episode .Gid ).Uri (),
188198 Name : * episode .Name ,
189199 ArtistNames : []string {* episode .Show .Name },
190200 AlbumName : * episode .Show .Name ,
191- AlbumCoverUrl : prodInfo .ImageUrl (albumCoverId ),
201+ AlbumCoverUrl : p . prodInfo .ImageUrl (albumCoverId ),
192202 Position : position ,
193203 Duration : int (* episode .Duration ),
194204 ReleaseDate : "" ,
0 commit comments