diff --git a/model/artwork.go b/model/artwork.go index dd0f45004..1efa4c1e9 100644 --- a/model/artwork.go +++ b/model/artwork.go @@ -27,6 +27,15 @@ type ItemImage struct { ImageHeight int `structs:"-" json:"imageHeight,omitempty"` } +// AspectRatio is the image's width/height, or nil when there is no image or its dimensions are +// unknown (an unresolved item). Never guesses: a wrong ratio mis-shapes a client's placeholder. +func (i ItemImage) AspectRatio() *float64 { + if i.ImageAbsent || i.ImageWidth <= 0 || i.ImageHeight <= 0 { + return nil + } + return new(float64(i.ImageWidth) / float64(i.ImageHeight)) +} + // ItemArtwork is an entity's resolved artwork state. Hash=="" means known absent. type ItemArtwork struct { ItemKind string `structs:"item_kind"` diff --git a/model/artwork_test.go b/model/artwork_test.go index 5beaca5a7..ecb89625b 100644 --- a/model/artwork_test.go +++ b/model/artwork_test.go @@ -52,6 +52,24 @@ var _ = Describe("ItemImage JSON", func() { Expect(out).ToNot(HaveKey("imageHeight")) }) + Describe("AspectRatio", func() { + It("returns width/height", func() { + img := model.ItemImage{ImageHash: "abc", ImageWidth: 1200, ImageHeight: 800} + Expect(*img.AspectRatio()).To(BeNumerically("~", 1.5, 0.0001)) + }) + + It("returns nil when a dimension is missing, so callers never guess a ratio", func() { + Expect(model.ItemImage{ImageHash: "abc"}.AspectRatio()).To(BeNil()) + Expect(model.ItemImage{ImageHash: "abc", ImageWidth: 1200}.AspectRatio()).To(BeNil()) + Expect(model.ItemImage{ImageHash: "abc", ImageHeight: 800}.AspectRatio()).To(BeNil()) + }) + + It("returns nil for a known-absent image, whatever the dimensions say", func() { + img := model.ItemImage{ImageAbsent: true, ImageWidth: 1200, ImageHeight: 800} + Expect(img.AspectRatio()).To(BeNil()) + }) + }) + It("exposes known-absent artwork so clients can skip the request", func() { ar := model.Artist{ID: "ar-1", Name: "Artist"} ar.ImageAbsent = true diff --git a/server/jellyfin/dto/mappers.go b/server/jellyfin/dto/mappers.go index 08f12760c..974a50bd0 100644 --- a/server/jellyfin/dto/mappers.go +++ b/server/jellyfin/dto/mappers.go @@ -232,9 +232,8 @@ func primaryImage(img model.ItemImage, fallback string, fields Fields) (tag stri if img.BlurHash != "" { blurs = map[string]map[string]string{"Primary": {tag: img.BlurHash}} } - // Dimensions are unknown while an item is unresolved; omit rather than guess a ratio. - if fields.Has("PrimaryImageAspectRatio") && img.ImageWidth > 0 && img.ImageHeight > 0 { - ratio = new(float64(img.ImageWidth) / float64(img.ImageHeight)) + if fields.Has("PrimaryImageAspectRatio") { + ratio = img.AspectRatio() } return tag, blurs, ratio }