From 8a84237f138ad81f33bd6be634f530a4238821a1 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Tue, 3 Feb 2026 23:56:40 -0500 Subject: [PATCH] Fix MediaSources field appending to query string - Properly parse and modify Fields parameter instead of appending to end - Fixes 400 BadRequest errors from malformed URLs - Now correctly adds MediaSources to Fields parameter value --- allstarr/Controllers/JellyfinController.cs | 49 ++++++++++++++++++---- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/allstarr/Controllers/JellyfinController.cs b/allstarr/Controllers/JellyfinController.cs index ff64aee..b4d9a66 100644 --- a/allstarr/Controllers/JellyfinController.cs +++ b/allstarr/Controllers/JellyfinController.cs @@ -126,17 +126,50 @@ public class JellyfinController : ControllerBase // Ensure MediaSources is included in Fields parameter for bitrate info var queryString = Request.QueryString.Value ?? ""; - if (!queryString.Contains("Fields=", StringComparison.OrdinalIgnoreCase)) + + if (!string.IsNullOrEmpty(queryString)) { - // No Fields parameter, add MediaSources - queryString = string.IsNullOrEmpty(queryString) - ? "?Fields=MediaSources" - : $"{queryString}&Fields=MediaSources"; + // Parse query string to modify Fields parameter + var queryParams = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(queryString); + + if (queryParams.ContainsKey("Fields")) + { + var fieldsValue = queryParams["Fields"].ToString(); + if (!fieldsValue.Contains("MediaSources", StringComparison.OrdinalIgnoreCase)) + { + // Append MediaSources to existing Fields + var newFields = string.IsNullOrEmpty(fieldsValue) + ? "MediaSources" + : $"{fieldsValue},MediaSources"; + + // Rebuild query string with updated Fields + var newQueryParams = new Dictionary(); + foreach (var kvp in queryParams) + { + if (kvp.Key == "Fields") + { + newQueryParams[kvp.Key] = newFields; + } + else + { + newQueryParams[kvp.Key] = kvp.Value.ToString(); + } + } + + queryString = "?" + string.Join("&", newQueryParams.Select(kvp => + $"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}")); + } + } + else + { + // No Fields parameter, add it + queryString = $"{queryString}&Fields=MediaSources"; + } } - else if (!queryString.Contains("MediaSources", StringComparison.OrdinalIgnoreCase)) + else { - // Fields parameter exists but doesn't include MediaSources, append it - queryString = $"{queryString},MediaSources"; + // No query string at all + queryString = "?Fields=MediaSources"; } endpoint = $"{endpoint}{queryString}";