RequestHelpers.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Jellyfin.Data.Entities;
  5. using Jellyfin.Data.Enums;
  6. using MediaBrowser.Common.Extensions;
  7. using MediaBrowser.Controller.Dto;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Net;
  10. using MediaBrowser.Controller.Session;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Querying;
  13. using Microsoft.AspNetCore.Http;
  14. namespace Jellyfin.Api.Helpers
  15. {
  16. /// <summary>
  17. /// Request Extensions.
  18. /// </summary>
  19. public static class RequestHelpers
  20. {
  21. /// <summary>
  22. /// Get Order By.
  23. /// </summary>
  24. /// <param name="sortBy">Sort By. Comma delimited string.</param>
  25. /// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
  26. /// <returns>Order By.</returns>
  27. public static (string, SortOrder)[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
  28. {
  29. if (sortBy.Count == 0)
  30. {
  31. return Array.Empty<ValueTuple<string, SortOrder>>();
  32. }
  33. var result = new (string, SortOrder)[sortBy.Count];
  34. var i = 0;
  35. // Add elements which have a SortOrder specified
  36. for (; i < requestedSortOrder.Count; i++)
  37. {
  38. result[i] = (sortBy[i], requestedSortOrder[i]);
  39. }
  40. // Add remaining elements with the first specified SortOrder
  41. // or the default one if no SortOrders are specified
  42. var order = requestedSortOrder.Count > 0 ? requestedSortOrder[0] : SortOrder.Ascending;
  43. for (; i < sortBy.Count; i++)
  44. {
  45. result[i] = (sortBy[i], order);
  46. }
  47. return result;
  48. }
  49. /// <summary>
  50. /// Splits a string at a separating character into an array of substrings.
  51. /// </summary>
  52. /// <param name="value">The string to split.</param>
  53. /// <param name="separator">The char that separates the substrings.</param>
  54. /// <param name="removeEmpty">Option to remove empty substrings from the array.</param>
  55. /// <returns>An array of the substrings.</returns>
  56. internal static string[] Split(string? value, char separator, bool removeEmpty)
  57. {
  58. if (string.IsNullOrWhiteSpace(value))
  59. {
  60. return Array.Empty<string>();
  61. }
  62. return removeEmpty
  63. ? value.Split(separator, StringSplitOptions.RemoveEmptyEntries)
  64. : value.Split(separator);
  65. }
  66. /// <summary>
  67. /// Checks if the user can update an entry.
  68. /// </summary>
  69. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  70. /// <param name="requestContext">The <see cref="HttpRequest"/>.</param>
  71. /// <param name="userId">The user id.</param>
  72. /// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
  73. /// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
  74. internal static bool AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
  75. {
  76. var auth = authContext.GetAuthorizationInfo(requestContext);
  77. var authenticatedUser = auth.User;
  78. // If they're going to update the record of another user, they must be an administrator
  79. if ((!userId.Equals(auth.UserId) && !authenticatedUser.HasPermission(PermissionKind.IsAdministrator))
  80. || (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess))
  81. {
  82. return false;
  83. }
  84. return true;
  85. }
  86. internal static SessionInfo GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
  87. {
  88. var authorization = authContext.GetAuthorizationInfo(request);
  89. var user = authorization.User;
  90. var session = sessionManager.LogSessionActivity(
  91. authorization.Client,
  92. authorization.Version,
  93. authorization.DeviceId,
  94. authorization.Device,
  95. request.HttpContext.GetNormalizedRemoteIp(),
  96. user);
  97. if (session == null)
  98. {
  99. throw new ArgumentException("Session not found.");
  100. }
  101. return session;
  102. }
  103. internal static QueryResult<BaseItemDto> CreateQueryResult(
  104. QueryResult<(BaseItem, ItemCounts)> result,
  105. DtoOptions dtoOptions,
  106. IDtoService dtoService,
  107. bool includeItemTypes,
  108. User? user)
  109. {
  110. var dtos = result.Items.Select(i =>
  111. {
  112. var (baseItem, counts) = i;
  113. var dto = dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  114. if (includeItemTypes)
  115. {
  116. dto.ChildCount = counts.ItemCount;
  117. dto.ProgramCount = counts.ProgramCount;
  118. dto.SeriesCount = counts.SeriesCount;
  119. dto.EpisodeCount = counts.EpisodeCount;
  120. dto.MovieCount = counts.MovieCount;
  121. dto.TrailerCount = counts.TrailerCount;
  122. dto.AlbumCount = counts.AlbumCount;
  123. dto.SongCount = counts.SongCount;
  124. dto.ArtistCount = counts.ArtistCount;
  125. }
  126. return dto;
  127. });
  128. return new QueryResult<BaseItemDto>
  129. {
  130. Items = dtos.ToArray(),
  131. TotalRecordCount = result.TotalRecordCount
  132. };
  133. }
  134. }
  135. }