ApiService.cs 10 KB

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