From e120b7f2dd986d7f07f6814b6d987bafd46baab8 Mon Sep 17 00:00:00 2001 From: vavallee Date: Fri, 7 Aug 2026 12:33:37 -0300 Subject: Stop image endpoints from upscaling beyond the source resolution ImageHelper.GetNewImageSize passed the caller-supplied width/height straight through to SkiaEncoder.EncodeImage, which allocates an SKImageInfo of exactly that size. Nothing bounded those values against the source image, so a request like Items//Images/Primary?width=23100&height=23100 made the server allocate and resample a 23100x23100 surface from, say, a 600x336 poster: the reporter measured 100% of a core for 10-15 minutes and 6-12 GB resident per request. The item images endpoints do not require authentication, so any caller who knows an item id can trigger this, and varying the size by one pixel misses the cache every time. Add DrawingUtils.ScaleDownToFit, which scales a size down uniformly until it fits inside a bounding box and returns it unchanged if it already does, and apply it in GetNewImageSize against the original image dimensions. Requests that ask for more pixels than the source now get the source resolution back, scaled to the requested aspect ratio. Downscaling paths are untouched, and DrawingUtils.Resize keeps its existing behaviour for the transcoding callers in EncodingJobInfo and StreamInfo, which legitimately size video output. ResizeFill already refused to upscale; this makes width/height consistent with fillWidth/fillHeight. Fixes #17056. --- MediaBrowser.Controller/Drawing/ImageHelper.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'MediaBrowser.Controller') diff --git a/MediaBrowser.Controller/Drawing/ImageHelper.cs b/MediaBrowser.Controller/Drawing/ImageHelper.cs index 9ef92bc981..c1d0203897 100644 --- a/MediaBrowser.Controller/Drawing/ImageHelper.cs +++ b/MediaBrowser.Controller/Drawing/ImageHelper.cs @@ -11,7 +11,11 @@ namespace MediaBrowser.Controller.Drawing // Determine the output size based on incoming parameters var newSize = DrawingUtils.Resize(originalImageSize, options.Width ?? 0, options.Height ?? 0, options.MaxWidth ?? 0, options.MaxHeight ?? 0); newSize = DrawingUtils.ResizeFill(newSize, options.FillWidth, options.FillHeight); - return newSize; + + // Never encode larger than the source. Upscaling adds no detail, and the requested + // width/height are caller-controlled, so without this an unauthenticated request can + // pin a CPU and allocate several GB encoding a single image. + return DrawingUtils.ScaleDownToFit(newSize, originalImageSize); } } } -- cgit v1.2.3 From c091ffdc6b2d8d4dd6f439d056c561bae7bd9a18 Mon Sep 17 00:00:00 2001 From: brandon Date: Fri, 7 Aug 2026 22:51:45 -0400 Subject: Batch alternate version detection in DtoService to remove MediaSourceCount N+1 Browsing a page of videos with the MediaSourceCount field ran one alternate version query per item, each opening a fresh DbContext. On a large library that turned a single page into hundreds of sequential round trips and made the Items endpoint take tens of seconds while holding a request thread the whole time. Detect which videos own alternate versions once per page with a single query, mirroring the existing people batch. Videos absent from that set have a single media source, so the per item lookups are skipped for the common case. Behavior is unchanged: a video with no alternates already resolved to a count of one. Adds a regression test asserting the count resolves from the batch and the per item lookups are never called. --- Emby.Server.Implementations/Dto/DtoService.cs | 49 ++++++++++++++++------ .../Library/LibraryManager.cs | 6 +++ .../Item/LinkedChildrenService.cs | 21 ++++++++++ MediaBrowser.Controller/Library/ILibraryManager.cs | 8 ++++ .../Persistence/ILinkedChildrenService.cs | 9 ++++ .../Dto/DtoServiceImageInheritanceTests.cs | 42 +++++++++++++++++++ 6 files changed, 123 insertions(+), 12 deletions(-) (limited to 'MediaBrowser.Controller') diff --git a/Emby.Server.Implementations/Dto/DtoService.cs b/Emby.Server.Implementations/Dto/DtoService.cs index da0c52df5b..062c19a1d4 100644 --- a/Emby.Server.Implementations/Dto/DtoService.cs +++ b/Emby.Server.Implementations/Dto/DtoService.cs @@ -253,6 +253,18 @@ namespace Emby.Server.Implementations.Dto } } + // Batch-detect which videos own alternate versions to avoid the per-item alternate-version + // queries in MediaSourceCount. Videos absent from this set have a single media source. + IReadOnlySet? alternateVersionItemIds = null; + if (options.ContainsField(ItemFields.MediaSourceCount)) + { + var versionItemIds = accessibleItems.OfType