ApiService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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).ConfigureAwait(false);
  73. if (includeChildren)
  74. {
  75. await AttachChildren(dto, item, user).ConfigureAwait(false);
  76. }
  77. if (includePeople)
  78. {
  79. await AttachPeople(dto, item).ConfigureAwait(false);
  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. Audio audio = item as Audio;
  89. if (audio != null)
  90. {
  91. dto.AudioInfo = new AudioInfo()
  92. {
  93. Album = audio.Album,
  94. AlbumArtist = audio.AlbumArtist,
  95. Artist = audio.Artist,
  96. BitRate = audio.BitRate,
  97. Channels = audio.Channels
  98. };
  99. }
  100. return dto;
  101. }
  102. private static async Task AttachStudios(DTOBaseItem dto, BaseItem item)
  103. {
  104. // Attach Studios by transforming them into BaseItemStudio (DTO)
  105. if (item.Studios != null)
  106. {
  107. IEnumerable<Studio> entities = await Task.WhenAll<Studio>(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c))).ConfigureAwait(false);
  108. dto.Studios = item.Studios.Select(s =>
  109. {
  110. BaseItemStudio baseItemStudio = new BaseItemStudio();
  111. baseItemStudio.Name = s;
  112. Studio ibnObject = entities.First(i => i.Name.Equals(s, StringComparison.OrdinalIgnoreCase));
  113. if (ibnObject != null)
  114. {
  115. baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  116. }
  117. return baseItemStudio;
  118. });
  119. }
  120. }
  121. private static async Task AttachChildren(DTOBaseItem dto, BaseItem item, User user)
  122. {
  123. var folder = item as Folder;
  124. if (folder != null)
  125. {
  126. IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
  127. dto.Children = await Task.WhenAll<DTOBaseItem>(children.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
  128. }
  129. if (item.LocalTrailers != null && item.LocalTrailers.Any())
  130. {
  131. dto.LocalTrailers = await Task.WhenAll<DTOBaseItem>(item.LocalTrailers.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
  132. }
  133. }
  134. private static async Task AttachPeople(DTOBaseItem dto, BaseItem item)
  135. {
  136. // Attach People by transforming them into BaseItemPerson (DTO)
  137. if (item.People != null)
  138. {
  139. IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Name))).ConfigureAwait(false);
  140. dto.People = item.People.Select(p =>
  141. {
  142. BaseItemPerson baseItemPerson = new BaseItemPerson();
  143. baseItemPerson.PersonInfo = p;
  144. Person ibnObject = entities.First(i => i.Name.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
  145. if (ibnObject != null)
  146. {
  147. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  148. }
  149. return baseItemPerson;
  150. });
  151. }
  152. }
  153. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  154. {
  155. backdropCount = 0;
  156. var parent = item.Parent;
  157. while (parent != null)
  158. {
  159. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  160. {
  161. backdropCount = parent.BackdropImagePaths.Count();
  162. return parent.Id;
  163. }
  164. parent = parent.Parent;
  165. }
  166. return null;
  167. }
  168. private static Guid? GetParentLogoItemId(BaseItem item)
  169. {
  170. var parent = item.Parent;
  171. while (parent != null)
  172. {
  173. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  174. {
  175. return parent.Id;
  176. }
  177. parent = parent.Parent;
  178. }
  179. return null;
  180. }
  181. }
  182. }