ApiService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Model.DTO;
  7. using MediaBrowser.Model.Entities;
  8. namespace MediaBrowser.Api
  9. {
  10. /// <summary>
  11. /// Contains some helpers for the api
  12. /// </summary>
  13. public static class ApiService
  14. {
  15. public static BaseItem GetItemById(string id)
  16. {
  17. Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
  18. return Kernel.Instance.GetItemById(guid);
  19. }
  20. public async static Task<DTOBaseItem> GetDTOBaseItem(BaseItem item, User user,
  21. bool includeChildren = true,
  22. bool includePeople = true)
  23. {
  24. DTOBaseItem dto = new DTOBaseItem();
  25. dto.AspectRatio = item.AspectRatio;
  26. dto.BackdropCount = item.BackdropImagePaths == null ? 0 : item.BackdropImagePaths.Count();
  27. dto.DateCreated = item.DateCreated;
  28. dto.DisplayMediaType = item.DisplayMediaType;
  29. dto.Genres = item.Genres;
  30. dto.HasArt = !string.IsNullOrEmpty(item.ArtImagePath);
  31. dto.HasBanner = !string.IsNullOrEmpty(item.BannerImagePath);
  32. dto.HasLogo = !string.IsNullOrEmpty(item.LogoImagePath);
  33. dto.HasPrimaryImage = !string.IsNullOrEmpty(item.LogoImagePath);
  34. dto.HasThumb = !string.IsNullOrEmpty(item.ThumbnailImagePath);
  35. dto.Id = item.Id;
  36. dto.IsNew = item.IsRecentlyAdded(user);
  37. dto.IndexNumber = item.IndexNumber;
  38. dto.IsFolder = item.IsFolder;
  39. dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
  40. dto.Name = item.Name;
  41. dto.OfficialRating = item.OfficialRating;
  42. dto.Overview = item.Overview;
  43. // If there are no backdrops, indicate what parent has them in case the UI wants to allow inheritance
  44. if (dto.BackdropCount == 0)
  45. {
  46. int backdropCount;
  47. dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  48. dto.ParentBackdropCount = backdropCount;
  49. }
  50. if (item.Parent != null)
  51. {
  52. dto.ParentId = item.Parent.Id;
  53. }
  54. // If there is no logo, indicate what parent has one in case the UI wants to allow inheritance
  55. if (!dto.HasLogo)
  56. {
  57. dto.ParentLogoItemId = GetParentLogoItemId(item);
  58. }
  59. dto.Path = item.Path;
  60. dto.PremiereDate = item.PremiereDate;
  61. dto.ProductionYear = item.ProductionYear;
  62. dto.ProviderIds = item.ProviderIds;
  63. dto.RunTimeTicks = item.RunTimeTicks;
  64. dto.SortName = item.SortName;
  65. dto.Taglines = item.Taglines;
  66. dto.TrailerUrl = item.TrailerUrl;
  67. dto.Type = item.GetType().Name;
  68. dto.UserRating = item.UserRating;
  69. dto.UserData = item.GetUserData(user);
  70. await AttachStudios(dto, item);
  71. if (includeChildren)
  72. {
  73. await AttachChildren(dto, item, user);
  74. }
  75. if (includePeople)
  76. {
  77. await AttachPeople(dto, item);
  78. }
  79. Folder folder = item as Folder;
  80. if (folder != null)
  81. {
  82. dto.SpecialCounts = folder.GetSpecialCounts(user);
  83. dto.IsRoot = folder.IsRoot;
  84. dto.IsVirtualFolder = folder is VirtualFolder;
  85. }
  86. return dto;
  87. }
  88. private static async Task AttachStudios(DTOBaseItem dto, BaseItem item)
  89. {
  90. // Attach Studios by transforming them into BaseItemStudio (DTO)
  91. if (item.Studios != null)
  92. {
  93. IEnumerable<Studio> entities = await Task.WhenAll<Studio>(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c)));
  94. dto.Studios = item.Studios.Select(s =>
  95. {
  96. BaseItemStudio baseItemStudio = new BaseItemStudio();
  97. baseItemStudio.Name = s;
  98. Studio ibnObject = entities.First(i => i.Name.Equals(s, StringComparison.OrdinalIgnoreCase));
  99. if (ibnObject != null)
  100. {
  101. baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  102. }
  103. return baseItemStudio;
  104. });
  105. }
  106. }
  107. private static async Task AttachChildren(DTOBaseItem dto, BaseItem item, User user)
  108. {
  109. var folder = item as Folder;
  110. if (folder != null)
  111. {
  112. IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
  113. dto.Children = await Task.WhenAll<DTOBaseItem>(children.Select(c => GetDTOBaseItem(c, user, false, false)));
  114. }
  115. if (item.LocalTrailers != null && item.LocalTrailers.Any())
  116. {
  117. dto.LocalTrailers = await Task.WhenAll<DTOBaseItem>(item.LocalTrailers.Select(c => GetDTOBaseItem(c, user, false, false)));
  118. }
  119. }
  120. private static async Task AttachPeople(DTOBaseItem dto, BaseItem item)
  121. {
  122. // Attach People by transforming them into BaseItemPerson (DTO)
  123. if (item.People != null)
  124. {
  125. IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Name)));
  126. dto.People = item.People.Select(p =>
  127. {
  128. BaseItemPerson baseItemPerson = new BaseItemPerson();
  129. baseItemPerson.PersonInfo = p;
  130. Person ibnObject = entities.First(i => i.Name.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
  131. if (ibnObject != null)
  132. {
  133. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  134. }
  135. return baseItemPerson;
  136. });
  137. }
  138. }
  139. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  140. {
  141. backdropCount = 0;
  142. var parent = item.Parent;
  143. while (parent != null)
  144. {
  145. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  146. {
  147. backdropCount = parent.BackdropImagePaths.Count();
  148. return parent.Id;
  149. }
  150. parent = parent.Parent;
  151. }
  152. return null;
  153. }
  154. private static Guid? GetParentLogoItemId(BaseItem item)
  155. {
  156. var parent = item.Parent;
  157. while (parent != null)
  158. {
  159. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  160. {
  161. return parent.Id;
  162. }
  163. parent = parent.Parent;
  164. }
  165. return null;
  166. }
  167. }
  168. }