ApiService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Language = item.Language;
  40. dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
  41. dto.Name = item.Name;
  42. dto.OfficialRating = item.OfficialRating;
  43. dto.Overview = item.Overview;
  44. // If there are no backdrops, indicate what parent has them in case the UI wants to allow inheritance
  45. if (dto.BackdropCount == 0)
  46. {
  47. int backdropCount;
  48. dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  49. dto.ParentBackdropCount = backdropCount;
  50. }
  51. if (item.Parent != null)
  52. {
  53. dto.ParentId = item.Parent.Id;
  54. }
  55. dto.ParentIndexNumber = item.ParentIndexNumber;
  56. // If there is no logo, indicate what parent has one in case the UI wants to allow inheritance
  57. if (!dto.HasLogo)
  58. {
  59. dto.ParentLogoItemId = GetParentLogoItemId(item);
  60. }
  61. dto.Path = item.Path;
  62. dto.PremiereDate = item.PremiereDate;
  63. dto.ProductionYear = item.ProductionYear;
  64. dto.ProviderIds = item.ProviderIds;
  65. dto.RunTimeTicks = item.RunTimeTicks;
  66. dto.SortName = item.SortName;
  67. dto.Taglines = item.Taglines;
  68. dto.TrailerUrl = item.TrailerUrl;
  69. dto.Type = item.GetType().Name;
  70. dto.UserRating = item.UserRating;
  71. dto.UserData = item.GetUserData(user);
  72. await AttachStudios(dto, item);
  73. if (includeChildren)
  74. {
  75. await AttachChildren(dto, item, user);
  76. }
  77. if (includePeople)
  78. {
  79. await AttachPeople(dto, item);
  80. }
  81. Folder folder = item as Folder;
  82. if (folder != null)
  83. {
  84. dto.SpecialCounts = folder.GetSpecialCounts(user);
  85. dto.IsRoot = folder.IsRoot;
  86. dto.IsVirtualFolder = folder is VirtualFolder;
  87. }
  88. return dto;
  89. }
  90. private static async Task AttachStudios(DTOBaseItem dto, BaseItem item)
  91. {
  92. // Attach Studios by transforming them into BaseItemStudio (DTO)
  93. if (item.Studios != null)
  94. {
  95. IEnumerable<Studio> entities = await Task.WhenAll<Studio>(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c)));
  96. dto.Studios = item.Studios.Select(s =>
  97. {
  98. BaseItemStudio baseItemStudio = new BaseItemStudio();
  99. baseItemStudio.Name = s;
  100. Studio ibnObject = entities.First(i => i.Name.Equals(s, StringComparison.OrdinalIgnoreCase));
  101. if (ibnObject != null)
  102. {
  103. baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  104. }
  105. return baseItemStudio;
  106. });
  107. }
  108. }
  109. private static async Task AttachChildren(DTOBaseItem dto, BaseItem item, User user)
  110. {
  111. var folder = item as Folder;
  112. if (folder != null)
  113. {
  114. IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
  115. dto.Children = await Task.WhenAll<DTOBaseItem>(children.Select(c => GetDTOBaseItem(c, user, false, false)));
  116. }
  117. if (item.LocalTrailers != null && item.LocalTrailers.Any())
  118. {
  119. dto.LocalTrailers = await Task.WhenAll<DTOBaseItem>(item.LocalTrailers.Select(c => GetDTOBaseItem(c, user, false, false)));
  120. }
  121. }
  122. private static async Task AttachPeople(DTOBaseItem dto, BaseItem item)
  123. {
  124. // Attach People by transforming them into BaseItemPerson (DTO)
  125. if (item.People != null)
  126. {
  127. IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Name)));
  128. dto.People = item.People.Select(p =>
  129. {
  130. BaseItemPerson baseItemPerson = new BaseItemPerson();
  131. baseItemPerson.PersonInfo = p;
  132. Person ibnObject = entities.First(i => i.Name.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
  133. if (ibnObject != null)
  134. {
  135. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  136. }
  137. return baseItemPerson;
  138. });
  139. }
  140. }
  141. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  142. {
  143. backdropCount = 0;
  144. var parent = item.Parent;
  145. while (parent != null)
  146. {
  147. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  148. {
  149. backdropCount = parent.BackdropImagePaths.Count();
  150. return parent.Id;
  151. }
  152. parent = parent.Parent;
  153. }
  154. return null;
  155. }
  156. private static Guid? GetParentLogoItemId(BaseItem item)
  157. {
  158. var parent = item.Parent;
  159. while (parent != null)
  160. {
  161. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  162. {
  163. return parent.Id;
  164. }
  165. parent = parent.Parent;
  166. }
  167. return null;
  168. }
  169. }
  170. }