ApiService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. List<Task> tasks = new List<Task>();
  26. tasks.Add(AttachStudios(dto, item));
  27. if (includeChildren)
  28. {
  29. tasks.Add(AttachChildren(dto, item, user));
  30. tasks.Add(AttachLocalTrailers(dto, item, user));
  31. }
  32. if (includePeople)
  33. {
  34. tasks.Add(AttachPeople(dto, item));
  35. }
  36. // Make sure all the tasks we kicked off have completed.
  37. if (tasks.Count > 0)
  38. {
  39. await Task.WhenAll(tasks).ConfigureAwait(false);
  40. }
  41. AttachBasicFields(dto, item, user);
  42. return dto;
  43. }
  44. private static void AttachBasicFields(DTOBaseItem dto, BaseItem item, User user)
  45. {
  46. dto.AspectRatio = item.AspectRatio;
  47. dto.BackdropCount = item.BackdropImagePaths == null ? 0 : item.BackdropImagePaths.Count();
  48. dto.DateCreated = item.DateCreated;
  49. dto.DisplayMediaType = item.DisplayMediaType;
  50. dto.Genres = item.Genres;
  51. dto.HasArt = !string.IsNullOrEmpty(item.ArtImagePath);
  52. dto.HasBanner = !string.IsNullOrEmpty(item.BannerImagePath);
  53. dto.HasLogo = !string.IsNullOrEmpty(item.LogoImagePath);
  54. dto.HasPrimaryImage = !string.IsNullOrEmpty(item.LogoImagePath);
  55. dto.HasThumb = !string.IsNullOrEmpty(item.ThumbnailImagePath);
  56. dto.Id = item.Id;
  57. dto.IsNew = item.IsRecentlyAdded(user);
  58. dto.IndexNumber = item.IndexNumber;
  59. dto.IsFolder = item.IsFolder;
  60. dto.Language = item.Language;
  61. dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
  62. dto.Name = item.Name;
  63. dto.OfficialRating = item.OfficialRating;
  64. dto.Overview = item.Overview;
  65. // If there are no backdrops, indicate what parent has them in case the UI wants to allow inheritance
  66. if (dto.BackdropCount == 0)
  67. {
  68. int backdropCount;
  69. dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  70. dto.ParentBackdropCount = backdropCount;
  71. }
  72. if (item.Parent != null)
  73. {
  74. dto.ParentId = item.Parent.Id;
  75. }
  76. dto.ParentIndexNumber = item.ParentIndexNumber;
  77. // If there is no logo, indicate what parent has one in case the UI wants to allow inheritance
  78. if (!dto.HasLogo)
  79. {
  80. dto.ParentLogoItemId = GetParentLogoItemId(item);
  81. }
  82. dto.Path = item.Path;
  83. dto.PremiereDate = item.PremiereDate;
  84. dto.ProductionYear = item.ProductionYear;
  85. dto.ProviderIds = item.ProviderIds;
  86. dto.RunTimeTicks = item.RunTimeTicks;
  87. dto.SortName = item.SortName;
  88. dto.Taglines = item.Taglines;
  89. dto.TrailerUrl = item.TrailerUrl;
  90. dto.Type = item.GetType().Name;
  91. dto.UserRating = item.UserRating;
  92. VirtualFolder virtualFolder = item.VirtualFolder;
  93. if (virtualFolder != null)
  94. {
  95. dto.VfType = virtualFolder.CollectionType;
  96. }
  97. dto.UserData = item.GetUserData(user);
  98. Folder folder = item as Folder;
  99. if (folder != null)
  100. {
  101. dto.SpecialCounts = folder.GetSpecialCounts(user);
  102. dto.IsRoot = folder.IsRoot;
  103. dto.IsVirtualFolder = folder is VirtualFolder;
  104. }
  105. Audio audio = item as Audio;
  106. if (audio != null)
  107. {
  108. dto.AudioInfo = new AudioInfo()
  109. {
  110. Album = audio.Album,
  111. AlbumArtist = audio.AlbumArtist,
  112. Artist = audio.Artist,
  113. BitRate = audio.BitRate,
  114. Channels = audio.Channels
  115. };
  116. }
  117. Video video = item as Video;
  118. if (video != null)
  119. {
  120. dto.VideoInfo = new VideoInfo()
  121. {
  122. Height = video.Height,
  123. Width = video.Width,
  124. Codec = video.Codec,
  125. VideoType = video.VideoType,
  126. AudioStreams = video.AudioStreams,
  127. Subtitles = video.Subtitles,
  128. ScanType = video.ScanType
  129. };
  130. }
  131. }
  132. private static async Task AttachStudios(DTOBaseItem dto, BaseItem item)
  133. {
  134. // Attach Studios by transforming them into BaseItemStudio (DTO)
  135. if (item.Studios != null)
  136. {
  137. IEnumerable<Studio> entities = await Task.WhenAll<Studio>(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c))).ConfigureAwait(false);
  138. dto.Studios = item.Studios.Select(s =>
  139. {
  140. BaseItemStudio baseItemStudio = new BaseItemStudio();
  141. baseItemStudio.Name = s;
  142. Studio ibnObject = entities.First(i => i.Name.Equals(s, StringComparison.OrdinalIgnoreCase));
  143. if (ibnObject != null)
  144. {
  145. baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  146. }
  147. return baseItemStudio;
  148. });
  149. }
  150. }
  151. private static async Task AttachChildren(DTOBaseItem dto, BaseItem item, User user)
  152. {
  153. var folder = item as Folder;
  154. if (folder != null)
  155. {
  156. IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
  157. dto.Children = await Task.WhenAll<DTOBaseItem>(children.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
  158. }
  159. }
  160. private static async Task AttachLocalTrailers(DTOBaseItem dto, BaseItem item, User user)
  161. {
  162. if (item.LocalTrailers != null && item.LocalTrailers.Any())
  163. {
  164. dto.LocalTrailers = await Task.WhenAll<DTOBaseItem>(item.LocalTrailers.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
  165. }
  166. }
  167. private static async Task AttachPeople(DTOBaseItem dto, BaseItem item)
  168. {
  169. // Attach People by transforming them into BaseItemPerson (DTO)
  170. if (item.People != null)
  171. {
  172. IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Key))).ConfigureAwait(false);
  173. dto.People = item.People.Select(p =>
  174. {
  175. BaseItemPerson baseItemPerson = new BaseItemPerson();
  176. baseItemPerson.Name = p.Key;
  177. baseItemPerson.Overview = p.Value.Overview;
  178. baseItemPerson.Type = p.Value.Type;
  179. Person ibnObject = entities.First(i => i.Name.Equals(p.Key, StringComparison.OrdinalIgnoreCase));
  180. if (ibnObject != null)
  181. {
  182. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  183. }
  184. return baseItemPerson;
  185. });
  186. }
  187. }
  188. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  189. {
  190. backdropCount = 0;
  191. var parent = item.Parent;
  192. while (parent != null)
  193. {
  194. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  195. {
  196. backdropCount = parent.BackdropImagePaths.Count();
  197. return parent.Id;
  198. }
  199. parent = parent.Parent;
  200. }
  201. return null;
  202. }
  203. private static Guid? GetParentLogoItemId(BaseItem item)
  204. {
  205. var parent = item.Parent;
  206. while (parent != null)
  207. {
  208. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  209. {
  210. return parent.Id;
  211. }
  212. parent = parent.Parent;
  213. }
  214. return null;
  215. }
  216. public static IBNItem GetIBNItem(BaseEntity entity, int itemCount)
  217. {
  218. return new IBNItem()
  219. {
  220. Id = entity.Id,
  221. BaseItemCount = itemCount,
  222. HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath),
  223. Name = entity.Name
  224. };
  225. }
  226. }
  227. }