ApiService.cs 6.3 KB

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