RequestHelpers.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using Jellyfin.Data.Enums;
  6. using MediaBrowser.Common.Extensions;
  7. using MediaBrowser.Controller.Net;
  8. using MediaBrowser.Controller.Session;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Querying;
  11. using Microsoft.AspNetCore.Http;
  12. namespace Jellyfin.Api.Helpers
  13. {
  14. /// <summary>
  15. /// Request Extensions.
  16. /// </summary>
  17. public static class RequestHelpers
  18. {
  19. /// <summary>
  20. /// Get Order By.
  21. /// </summary>
  22. /// <param name="sortBy">Sort By. Comma delimited string.</param>
  23. /// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
  24. /// <returns>Order By.</returns>
  25. public static ValueTuple<string, SortOrder>[] GetOrderBy(string? sortBy, string? requestedSortOrder)
  26. {
  27. var val = sortBy;
  28. if (string.IsNullOrEmpty(val))
  29. {
  30. return Array.Empty<ValueTuple<string, SortOrder>>();
  31. }
  32. var vals = val.Split(',');
  33. if (string.IsNullOrWhiteSpace(requestedSortOrder))
  34. {
  35. requestedSortOrder = "Ascending";
  36. }
  37. var sortOrders = requestedSortOrder.Split(',');
  38. var result = new ValueTuple<string, SortOrder>[vals.Length];
  39. for (var i = 0; i < vals.Length; i++)
  40. {
  41. var sortOrderIndex = sortOrders.Length > i ? i : 0;
  42. var sortOrderValue = sortOrders.Length > sortOrderIndex ? sortOrders[sortOrderIndex] : null;
  43. var sortOrder = string.Equals(sortOrderValue, "Descending", StringComparison.OrdinalIgnoreCase)
  44. ? SortOrder.Descending
  45. : SortOrder.Ascending;
  46. result[i] = new ValueTuple<string, SortOrder>(vals[i], sortOrder);
  47. }
  48. return result;
  49. }
  50. /// <summary>
  51. /// Get parsed filters.
  52. /// </summary>
  53. /// <param name="filters">The filters.</param>
  54. /// <returns>Item filters.</returns>
  55. public static IEnumerable<ItemFilter> GetFilters(string? filters)
  56. {
  57. return string.IsNullOrEmpty(filters)
  58. ? Array.Empty<ItemFilter>()
  59. : filters.Split(',').Select(v => Enum.Parse<ItemFilter>(v, true));
  60. }
  61. /// <summary>
  62. /// Splits a string at a separating character into an array of substrings.
  63. /// </summary>
  64. /// <param name="value">The string to split.</param>
  65. /// <param name="separator">The char that separates the substrings.</param>
  66. /// <param name="removeEmpty">Option to remove empty substrings from the array.</param>
  67. /// <returns>An array of the substrings.</returns>
  68. internal static string[] Split(string? value, char separator, bool removeEmpty)
  69. {
  70. if (string.IsNullOrWhiteSpace(value))
  71. {
  72. return Array.Empty<string>();
  73. }
  74. return removeEmpty
  75. ? value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries)
  76. : value.Split(separator);
  77. }
  78. /// <summary>
  79. /// Checks if the user can update an entry.
  80. /// </summary>
  81. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  82. /// <param name="requestContext">The <see cref="HttpRequest"/>.</param>
  83. /// <param name="userId">The user id.</param>
  84. /// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
  85. /// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
  86. internal static bool AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
  87. {
  88. var auth = authContext.GetAuthorizationInfo(requestContext);
  89. var authenticatedUser = auth.User;
  90. // If they're going to update the record of another user, they must be an administrator
  91. if ((!userId.Equals(auth.UserId) && !authenticatedUser.HasPermission(PermissionKind.IsAdministrator))
  92. || (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess))
  93. {
  94. return false;
  95. }
  96. return true;
  97. }
  98. internal static SessionInfo GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
  99. {
  100. var authorization = authContext.GetAuthorizationInfo(request);
  101. var user = authorization.User;
  102. var session = sessionManager.LogSessionActivity(
  103. authorization.Client,
  104. authorization.Version,
  105. authorization.DeviceId,
  106. authorization.Device,
  107. request.HttpContext.GetNormalizedRemoteIp(),
  108. user);
  109. if (session == null)
  110. {
  111. throw new ArgumentException("Session not found.");
  112. }
  113. return session;
  114. }
  115. /// <summary>
  116. /// Get Guid array from string.
  117. /// </summary>
  118. /// <param name="value">String value.</param>
  119. /// <returns>Guid array.</returns>
  120. internal static Guid[] GetGuids(string? value)
  121. {
  122. if (value == null)
  123. {
  124. return Array.Empty<Guid>();
  125. }
  126. return Split(value, ',', true)
  127. .Select(i => new Guid(i))
  128. .ToArray();
  129. }
  130. /// <summary>
  131. /// Gets the item fields.
  132. /// </summary>
  133. /// <param name="fields">The fields string.</param>
  134. /// <returns>IEnumerable{ItemFields}.</returns>
  135. internal static ItemFields[] GetItemFields(string? fields)
  136. {
  137. if (string.IsNullOrEmpty(fields))
  138. {
  139. return Array.Empty<ItemFields>();
  140. }
  141. return Split(fields, ',', true)
  142. .Select(v =>
  143. {
  144. if (Enum.TryParse(v, true, out ItemFields value))
  145. {
  146. return (ItemFields?)value;
  147. }
  148. return null;
  149. }).Where(i => i.HasValue)
  150. .Select(i => i!.Value)
  151. .ToArray();
  152. }
  153. /// <summary>
  154. /// Gets the item fields.
  155. /// </summary>
  156. /// <param name="imageTypes">The image types string.</param>
  157. /// <returns>IEnumerable{ItemFields}.</returns>
  158. internal static ImageType[] GetImageTypes(string? imageTypes)
  159. {
  160. if (string.IsNullOrEmpty(imageTypes))
  161. {
  162. return Array.Empty<ImageType>();
  163. }
  164. return Split(imageTypes, ',', true)
  165. .Select(v =>
  166. {
  167. if (Enum.TryParse(v, true, out ImageType value))
  168. {
  169. return (ImageType?)value;
  170. }
  171. return null;
  172. }).Where(i => i.HasValue)
  173. .Select(i => i!.Value)
  174. .ToArray();
  175. }
  176. }
  177. }