ApiService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. using MediaBrowser.Model.Users;
  10. namespace MediaBrowser.Api
  11. {
  12. /// <summary>
  13. /// Contains some helpers for the api
  14. /// </summary>
  15. public static class ApiService
  16. {
  17. public static BaseItem GetItemById(string id)
  18. {
  19. Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
  20. return Kernel.Instance.GetItemById(guid);
  21. }
  22. /// <summary>
  23. /// Takes a BaseItem and returns the actual object that will be serialized by the api
  24. /// </summary>
  25. public static BaseItemContainer<BaseItem> GetSerializationObject(BaseItem item, bool includeChildren, Guid userId)
  26. {
  27. User user = Kernel.Instance.Users.First(u => u.Id == userId);
  28. BaseItemContainer<BaseItem> wrapper = new BaseItemContainer<BaseItem>()
  29. {
  30. Item = item,
  31. UserItemData = user.GetItemData(item.Id),
  32. Type = item.GetType().Name,
  33. IsFolder = (item is Folder)
  34. };
  35. if (string.IsNullOrEmpty(item.LogoImagePath))
  36. {
  37. wrapper.ParentLogoItemId = GetParentLogoItemId(item);
  38. }
  39. if (item.BackdropImagePaths == null || !item.BackdropImagePaths.Any())
  40. {
  41. int backdropCount;
  42. wrapper.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  43. wrapper.ParentBackdropCount = backdropCount;
  44. }
  45. if (item.Parent != null)
  46. {
  47. wrapper.ParentId = item.Parent.Id;
  48. }
  49. if (includeChildren)
  50. {
  51. var folder = item as Folder;
  52. if (folder != null)
  53. {
  54. wrapper.Children = folder.GetParentalAllowedChildren(user).Select(c => GetSerializationObject(c, false, userId));
  55. }
  56. // Attach People by transforming them into BaseItemPerson (DTO)
  57. if (item.People != null)
  58. {
  59. wrapper.People = item.People.Select(p =>
  60. {
  61. BaseItemPerson baseItemPerson = new BaseItemPerson();
  62. baseItemPerson.PersonInfo = p;
  63. Person ibnObject = Kernel.Instance.ItemController.GetPerson(p.Name);
  64. if (ibnObject != null)
  65. {
  66. baseItemPerson.PrimaryImagePath = ibnObject.PrimaryImagePath;
  67. }
  68. return baseItemPerson;
  69. });
  70. }
  71. }
  72. // Attach Studios by transforming them into BaseItemStudio (DTO)
  73. if (item.Studios != null)
  74. {
  75. wrapper.Studios = item.Studios.Select(s =>
  76. {
  77. BaseItemStudio baseItemStudio = new BaseItemStudio();
  78. baseItemStudio.Name = s;
  79. Studio ibnObject = Kernel.Instance.ItemController.GetStudio(s);
  80. if (ibnObject != null)
  81. {
  82. baseItemStudio.PrimaryImagePath = ibnObject.PrimaryImagePath;
  83. }
  84. return baseItemStudio;
  85. });
  86. }
  87. return wrapper;
  88. }
  89. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  90. {
  91. backdropCount = 0;
  92. var parent = item.Parent;
  93. while (parent != null)
  94. {
  95. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  96. {
  97. backdropCount = parent.BackdropImagePaths.Count();
  98. return parent.Id;
  99. }
  100. parent = parent.Parent;
  101. }
  102. return null;
  103. }
  104. private static Guid? GetParentLogoItemId(BaseItem item)
  105. {
  106. var parent = item.Parent;
  107. while (parent != null)
  108. {
  109. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  110. {
  111. return parent.Id;
  112. }
  113. parent = parent.Parent;
  114. }
  115. return null;
  116. }
  117. private static string _FFMpegDirectory = null;
  118. /// <summary>
  119. /// Gets the folder path to ffmpeg
  120. /// </summary>
  121. public static string FFMpegDirectory
  122. {
  123. get
  124. {
  125. if (_FFMpegDirectory == null)
  126. {
  127. _FFMpegDirectory = System.IO.Path.Combine(ApplicationPaths.ProgramDataPath, "ffmpeg");
  128. if (!Directory.Exists(_FFMpegDirectory))
  129. {
  130. Directory.CreateDirectory(_FFMpegDirectory);
  131. }
  132. }
  133. return _FFMpegDirectory;
  134. }
  135. }
  136. private static string _FFMpegPath = null;
  137. /// <summary>
  138. /// Gets the path to ffmpeg.exe
  139. /// </summary>
  140. public static string FFMpegPath
  141. {
  142. get
  143. {
  144. if (_FFMpegPath == null)
  145. {
  146. string filename = "ffmpeg.exe";
  147. _FFMpegPath = Path.Combine(FFMpegDirectory, filename);
  148. // Always re-extract the first time to handle new versions
  149. if (File.Exists(_FFMpegPath))
  150. {
  151. File.Delete(_FFMpegPath);
  152. }
  153. // Extract ffprobe
  154. using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Api.FFMpeg." + filename))
  155. {
  156. using (FileStream fileStream = new FileStream(_FFMpegPath, FileMode.Create))
  157. {
  158. stream.CopyTo(fileStream);
  159. }
  160. }
  161. }
  162. return _FFMpegPath;
  163. }
  164. }
  165. }
  166. }