ApiService.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. dto.UserData = item.GetUserData(user);
  93. Folder folder = item as Folder;
  94. if (folder != null)
  95. {
  96. dto.SpecialCounts = folder.GetSpecialCounts(user);
  97. dto.IsRoot = folder.IsRoot;
  98. dto.IsVirtualFolder = folder.IsVirtualFolder;
  99. }
  100. Audio audio = item as Audio;
  101. if (audio != null)
  102. {
  103. dto.AudioInfo = new AudioInfo()
  104. {
  105. Album = audio.Album,
  106. AlbumArtist = audio.AlbumArtist,
  107. Artist = audio.Artist,
  108. BitRate = audio.BitRate,
  109. Channels = audio.Channels
  110. };
  111. }
  112. Video video = item as Video;
  113. if (video != null)
  114. {
  115. dto.VideoInfo = new VideoInfo()
  116. {
  117. Height = video.Height,
  118. Width = video.Width,
  119. Codec = video.Codec,
  120. VideoType = video.VideoType,
  121. AudioStreams = video.AudioStreams,
  122. Subtitles = video.Subtitles,
  123. ScanType = video.ScanType
  124. };
  125. }
  126. }
  127. private static async Task AttachStudios(DTOBaseItem dto, BaseItem item)
  128. {
  129. // Attach Studios by transforming them into BaseItemStudio (DTO)
  130. if (item.Studios != null)
  131. {
  132. IEnumerable<Studio> entities = await Task.WhenAll<Studio>(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c))).ConfigureAwait(false);
  133. dto.Studios = item.Studios.Select(s =>
  134. {
  135. BaseItemStudio baseItemStudio = new BaseItemStudio();
  136. baseItemStudio.Name = s;
  137. Studio ibnObject = entities.First(i => i.Name.Equals(s, StringComparison.OrdinalIgnoreCase));
  138. if (ibnObject != null)
  139. {
  140. baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  141. }
  142. return baseItemStudio;
  143. }).ToArray();
  144. }
  145. }
  146. private static async Task AttachChildren(DTOBaseItem dto, BaseItem item, User user)
  147. {
  148. var folder = item as Folder;
  149. if (folder != null)
  150. {
  151. IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
  152. dto.Children = await Task.WhenAll<DTOBaseItem>(children.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
  153. }
  154. }
  155. private static async Task AttachLocalTrailers(DTOBaseItem dto, BaseItem item, User user)
  156. {
  157. if (item.LocalTrailers != null && item.LocalTrailers.Any())
  158. {
  159. dto.LocalTrailers = await Task.WhenAll<DTOBaseItem>(item.LocalTrailers.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
  160. }
  161. }
  162. private static async Task AttachPeople(DTOBaseItem dto, BaseItem item)
  163. {
  164. // Attach People by transforming them into BaseItemPerson (DTO)
  165. if (item.People != null)
  166. {
  167. IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Key))).ConfigureAwait(false);
  168. dto.People = item.People.Select(p =>
  169. {
  170. BaseItemPerson baseItemPerson = new BaseItemPerson();
  171. baseItemPerson.Name = p.Key;
  172. baseItemPerson.Overview = p.Value.Overview;
  173. baseItemPerson.Type = p.Value.Type;
  174. Person ibnObject = entities.First(i => i.Name.Equals(p.Key, StringComparison.OrdinalIgnoreCase));
  175. if (ibnObject != null)
  176. {
  177. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  178. }
  179. return baseItemPerson;
  180. }).ToArray();
  181. }
  182. }
  183. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  184. {
  185. backdropCount = 0;
  186. var parent = item.Parent;
  187. while (parent != null)
  188. {
  189. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  190. {
  191. backdropCount = parent.BackdropImagePaths.Count();
  192. return parent.Id;
  193. }
  194. parent = parent.Parent;
  195. }
  196. return null;
  197. }
  198. private static Guid? GetParentLogoItemId(BaseItem item)
  199. {
  200. var parent = item.Parent;
  201. while (parent != null)
  202. {
  203. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  204. {
  205. return parent.Id;
  206. }
  207. parent = parent.Parent;
  208. }
  209. return null;
  210. }
  211. public static IBNItem GetIBNItem(BaseEntity entity, int itemCount)
  212. {
  213. return new IBNItem()
  214. {
  215. Id = entity.Id,
  216. BaseItemCount = itemCount,
  217. HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath),
  218. Name = entity.Name
  219. };
  220. }
  221. public static DTOUser GetDTOUser(User user)
  222. {
  223. return new DTOUser()
  224. {
  225. Id = user.Id,
  226. Name = user.Name,
  227. HasImage = !string.IsNullOrEmpty(user.PrimaryImagePath)
  228. };
  229. }
  230. }
  231. }