using System; using System.Collections.Generic; using System.Linq; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Querying; namespace Jellyfin.Api.Helpers { /// /// Request Extensions. /// public static class RequestHelpers { /// /// Get Order By. /// /// Sort By. Comma delimited string. /// Sort Order. Comma delimited string. /// Order By. public static ValueTuple[] GetOrderBy(string sortBy, string requestedSortOrder) { var val = sortBy; if (string.IsNullOrEmpty(val)) { return Array.Empty>(); } var vals = val.Split(','); if (string.IsNullOrWhiteSpace(requestedSortOrder)) { requestedSortOrder = "Ascending"; } var sortOrders = requestedSortOrder.Split(','); var result = new ValueTuple[vals.Length]; for (var i = 0; i < vals.Length; i++) { var sortOrderIndex = sortOrders.Length > i ? i : 0; var sortOrderValue = sortOrders.Length > sortOrderIndex ? sortOrders[sortOrderIndex] : null; var sortOrder = string.Equals(sortOrderValue, "Descending", StringComparison.OrdinalIgnoreCase) ? SortOrder.Descending : SortOrder.Ascending; result[i] = new ValueTuple(vals[i], sortOrder); } return result; } /// /// Gets the item fields. /// /// The fields. /// IEnumerable{ItemFields}. public static ItemFields[] GetItemFields(string fields) { if (string.IsNullOrEmpty(fields)) { return Array.Empty(); } return fields.Split(',').Select(v => { if (Enum.TryParse(v, true, out ItemFields value)) { return (ItemFields?)value; } return null; }).Where(i => i.HasValue).Select(i => i!.Value).ToArray(); } /// /// Get parsed filters. /// /// The filters. /// Item filters. public static IEnumerable GetFilters(string filters) { return string.IsNullOrEmpty(filters) ? Array.Empty() : filters.Split(',').Select(v => Enum.Parse(v, true)); } /// /// Splits a string at a separating character into an array of substrings. /// /// The string to split. /// The char that separates the substrings. /// Option to remove empty substrings from the array. /// An array of the substrings. internal static string[] Split(string value, char separator, bool removeEmpty) { if (string.IsNullOrWhiteSpace(value)) { return Array.Empty(); } return removeEmpty ? value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries) : value.Split(separator); } } }