ApiService.cs 7.8 KB

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