2
0

ApiService.cs 8.4 KB

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