ApiService.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.IndexNumber = item.IndexNumber;
  38. dto.IsFolder = item is Folder;
  39. dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
  40. dto.Name = item.Name;
  41. dto.OfficialRating = item.OfficialRating;
  42. dto.Overview = item.Overview;
  43. // If there are no backdrops, indicate what parent has them in case the UI wants to allow inheritance
  44. if (dto.BackdropCount == 0)
  45. {
  46. int backdropCount;
  47. dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  48. dto.ParentBackdropCount = backdropCount;
  49. }
  50. if (item.Parent != null)
  51. {
  52. dto.ParentId = item.Parent.Id;
  53. }
  54. // If there is no logo, indicate what parent has one in case the UI wants to allow inheritance
  55. if (!dto.HasLogo)
  56. {
  57. dto.ParentLogoItemId = GetParentLogoItemId(item);
  58. }
  59. dto.Path = item.Path;
  60. dto.PremiereDate = item.PremiereDate;
  61. dto.ProductionYear = item.ProductionYear;
  62. dto.ProviderIds = item.ProviderIds;
  63. dto.RunTimeTicks = item.RunTimeTicks;
  64. dto.SortName = item.SortName;
  65. dto.Taglines = item.Taglines;
  66. dto.TrailerUrl = item.TrailerUrl;
  67. dto.Type = item.GetType().Name;
  68. dto.UserRating = item.UserRating;
  69. dto.UserData = item.GetUserData(user);
  70. AttachStudios(dto, item);
  71. if (includeChildren)
  72. {
  73. AttachChildren(dto, item, user);
  74. }
  75. if (includePeople)
  76. {
  77. AttachPeople(dto, item);
  78. }
  79. Folder folder = item as Folder;
  80. if (folder != null)
  81. {
  82. dto.SpecialCounts = folder.GetSpecialCounts(user);
  83. dto.IsRoot = folder.IsRoot;
  84. dto.IsVirtualFolder = folder.IsVirtualFolder;
  85. }
  86. return dto;
  87. }
  88. private static void AttachStudios(DTOBaseItem dto, BaseItem item)
  89. {
  90. // Attach Studios by transforming them into BaseItemStudio (DTO)
  91. if (item.Studios != null)
  92. {
  93. dto.Studios = item.Studios.Select(s =>
  94. {
  95. BaseItemStudio baseItemStudio = new BaseItemStudio();
  96. baseItemStudio.Name = s;
  97. Studio ibnObject = Kernel.Instance.ItemController.GetStudio(s);
  98. if (ibnObject != null)
  99. {
  100. baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  101. }
  102. return baseItemStudio;
  103. });
  104. }
  105. }
  106. private static void AttachChildren(DTOBaseItem dto, BaseItem item, User user)
  107. {
  108. var folder = item as Folder;
  109. if (folder != null)
  110. {
  111. dto.Children = folder.GetParentalAllowedChildren(user).Select(c => GetDTOBaseItem(c, user, false, false));
  112. }
  113. dto.LocalTrailers = item.LocalTrailers;
  114. }
  115. private static void AttachPeople(DTOBaseItem dto, BaseItem item)
  116. {
  117. // Attach People by transforming them into BaseItemPerson (DTO)
  118. if (item.People != null)
  119. {
  120. dto.People = item.People.Select(p =>
  121. {
  122. BaseItemPerson baseItemPerson = new BaseItemPerson();
  123. baseItemPerson.PersonInfo = p;
  124. Person ibnObject = Kernel.Instance.ItemController.GetPerson(p.Name);
  125. if (ibnObject != null)
  126. {
  127. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  128. }
  129. return baseItemPerson;
  130. });
  131. }
  132. }
  133. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  134. {
  135. backdropCount = 0;
  136. var parent = item.Parent;
  137. while (parent != null)
  138. {
  139. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  140. {
  141. backdropCount = parent.BackdropImagePaths.Count();
  142. return parent.Id;
  143. }
  144. parent = parent.Parent;
  145. }
  146. return null;
  147. }
  148. private static Guid? GetParentLogoItemId(BaseItem item)
  149. {
  150. var parent = item.Parent;
  151. while (parent != null)
  152. {
  153. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  154. {
  155. return parent.Id;
  156. }
  157. parent = parent.Parent;
  158. }
  159. return null;
  160. }
  161. private static string _FFMpegDirectory = null;
  162. /// <summary>
  163. /// Gets the folder path to ffmpeg
  164. /// </summary>
  165. public static string FFMpegDirectory
  166. {
  167. get
  168. {
  169. if (_FFMpegDirectory == null)
  170. {
  171. _FFMpegDirectory = System.IO.Path.Combine(ApplicationPaths.ProgramDataPath, "ffmpeg");
  172. if (!Directory.Exists(_FFMpegDirectory))
  173. {
  174. Directory.CreateDirectory(_FFMpegDirectory);
  175. }
  176. }
  177. return _FFMpegDirectory;
  178. }
  179. }
  180. private static string _FFMpegPath = null;
  181. /// <summary>
  182. /// Gets the path to ffmpeg.exe
  183. /// </summary>
  184. public static string FFMpegPath
  185. {
  186. get
  187. {
  188. if (_FFMpegPath == null)
  189. {
  190. string filename = "ffmpeg.exe";
  191. _FFMpegPath = Path.Combine(FFMpegDirectory, filename);
  192. // Always re-extract the first time to handle new versions
  193. if (File.Exists(_FFMpegPath))
  194. {
  195. File.Delete(_FFMpegPath);
  196. }
  197. // Extract ffprobe
  198. using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Api.FFMpeg." + filename))
  199. {
  200. using (FileStream fileStream = new FileStream(_FFMpegPath, FileMode.Create))
  201. {
  202. stream.CopyTo(fileStream);
  203. }
  204. }
  205. }
  206. return _FFMpegPath;
  207. }
  208. }
  209. }
  210. }