ApiService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Controller;
  7. using MediaBrowser.Model.DTO;
  8. using MediaBrowser.Model.Entities;
  9. namespace MediaBrowser.Api
  10. {
  11. /// <summary>
  12. /// Contains some helpers for the api
  13. /// </summary>
  14. public static class ApiService
  15. {
  16. public static BaseItem GetItemById(string id)
  17. {
  18. Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
  19. return Kernel.Instance.GetItemById(guid);
  20. }
  21. public static DTOBaseItem GetDTOBaseItem(BaseItem item, User user,
  22. bool includeChildren = true,
  23. bool includePeople = true)
  24. {
  25. DTOBaseItem dto = new DTOBaseItem();
  26. dto.AspectRatio = item.AspectRatio;
  27. dto.BackdropCount = item.BackdropImagePaths == null ? 0 : item.BackdropImagePaths.Count();
  28. dto.DateCreated = item.DateCreated;
  29. dto.DisplayMediaType = item.DisplayMediaType;
  30. dto.Genres = item.Genres;
  31. dto.HasArt = !string.IsNullOrEmpty(item.ArtImagePath);
  32. dto.HasBanner = !string.IsNullOrEmpty(item.BannerImagePath);
  33. dto.HasLogo = !string.IsNullOrEmpty(item.LogoImagePath);
  34. dto.HasPrimaryImage = !string.IsNullOrEmpty(item.LogoImagePath);
  35. dto.HasThumb = !string.IsNullOrEmpty(item.ThumbnailImagePath);
  36. dto.Id = item.Id;
  37. dto.IsNew = item.IsRecentlyAdded(user);
  38. dto.IndexNumber = item.IndexNumber;
  39. dto.IsFolder = item is Folder;
  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. // If there is no logo, indicate what parent has one in case the UI wants to allow inheritance
  56. if (!dto.HasLogo)
  57. {
  58. dto.ParentLogoItemId = GetParentLogoItemId(item);
  59. }
  60. dto.Path = item.Path;
  61. dto.PremiereDate = item.PremiereDate;
  62. dto.ProductionYear = item.ProductionYear;
  63. dto.ProviderIds = item.ProviderIds;
  64. dto.RunTimeTicks = item.RunTimeTicks;
  65. dto.SortName = item.SortName;
  66. dto.Taglines = item.Taglines;
  67. dto.TrailerUrl = item.TrailerUrl;
  68. dto.Type = item.GetType().Name;
  69. dto.UserRating = item.UserRating;
  70. dto.UserData = item.GetUserData(user);
  71. AttachStudios(dto, item);
  72. if (includeChildren)
  73. {
  74. AttachChildren(dto, item, user);
  75. }
  76. if (includePeople)
  77. {
  78. AttachPeople(dto, item);
  79. }
  80. Folder folder = item as Folder;
  81. if (folder != null)
  82. {
  83. dto.SpecialCounts = folder.GetSpecialCounts(user);
  84. dto.IsRoot = folder.IsRoot;
  85. dto.IsVirtualFolder = folder is VirtualFolder;
  86. }
  87. return dto;
  88. }
  89. private static void AttachStudios(DTOBaseItem dto, BaseItem item)
  90. {
  91. // Attach Studios by transforming them into BaseItemStudio (DTO)
  92. if (item.Studios != null)
  93. {
  94. dto.Studios = item.Studios.Select(s =>
  95. {
  96. BaseItemStudio baseItemStudio = new BaseItemStudio();
  97. baseItemStudio.Name = s;
  98. Studio ibnObject = Kernel.Instance.ItemController.GetStudio(s);
  99. if (ibnObject != null)
  100. {
  101. baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  102. }
  103. return baseItemStudio;
  104. });
  105. }
  106. }
  107. private static void AttachChildren(DTOBaseItem dto, BaseItem item, User user)
  108. {
  109. var folder = item as Folder;
  110. if (folder != null)
  111. {
  112. dto.Children = folder.GetParentalAllowedChildren(user).Select(c => GetDTOBaseItem(c, user, false, false));
  113. }
  114. dto.LocalTrailers = item.LocalTrailers;
  115. }
  116. private static void AttachPeople(DTOBaseItem dto, BaseItem item)
  117. {
  118. // Attach People by transforming them into BaseItemPerson (DTO)
  119. if (item.People != null)
  120. {
  121. dto.People = item.People.Select(p =>
  122. {
  123. BaseItemPerson baseItemPerson = new BaseItemPerson();
  124. baseItemPerson.PersonInfo = p;
  125. Person ibnObject = Kernel.Instance.ItemController.GetPerson(p.Name);
  126. if (ibnObject != null)
  127. {
  128. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  129. }
  130. return baseItemPerson;
  131. });
  132. }
  133. }
  134. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  135. {
  136. backdropCount = 0;
  137. var parent = item.Parent;
  138. while (parent != null)
  139. {
  140. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  141. {
  142. backdropCount = parent.BackdropImagePaths.Count();
  143. return parent.Id;
  144. }
  145. parent = parent.Parent;
  146. }
  147. return null;
  148. }
  149. private static Guid? GetParentLogoItemId(BaseItem item)
  150. {
  151. var parent = item.Parent;
  152. while (parent != null)
  153. {
  154. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  155. {
  156. return parent.Id;
  157. }
  158. parent = parent.Parent;
  159. }
  160. return null;
  161. }
  162. }
  163. }