ApiService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 BaseItemWrapper<BaseItem> GetSerializationObject(BaseItem item, bool includeChildren, Guid userId)
  25. {
  26. BaseItemWrapper<BaseItem> wrapper = new BaseItemWrapper<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. wrapper.People = item.People;
  55. }
  56. return wrapper;
  57. }
  58. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  59. {
  60. backdropCount = 0;
  61. var parent = item.Parent;
  62. while (parent != null)
  63. {
  64. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  65. {
  66. backdropCount = parent.BackdropImagePaths.Count();
  67. return parent.Id;
  68. }
  69. parent = parent.Parent;
  70. }
  71. return null;
  72. }
  73. private static Guid? GetParentLogoItemId(BaseItem item)
  74. {
  75. var parent = item.Parent;
  76. while (parent != null)
  77. {
  78. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  79. {
  80. return parent.Id;
  81. }
  82. parent = parent.Parent;
  83. }
  84. return null;
  85. }
  86. private static string _FFMpegDirectory = null;
  87. /// <summary>
  88. /// Gets the folder path to ffmpeg
  89. /// </summary>
  90. public static string FFMpegDirectory
  91. {
  92. get
  93. {
  94. if (_FFMpegDirectory == null)
  95. {
  96. _FFMpegDirectory = System.IO.Path.Combine(ApplicationPaths.ProgramDataPath, "ffmpeg");
  97. if (!Directory.Exists(_FFMpegDirectory))
  98. {
  99. Directory.CreateDirectory(_FFMpegDirectory);
  100. }
  101. }
  102. return _FFMpegDirectory;
  103. }
  104. }
  105. private static string _FFMpegPath = null;
  106. /// <summary>
  107. /// Gets the path to ffmpeg.exe
  108. /// </summary>
  109. public static string FFMpegPath
  110. {
  111. get
  112. {
  113. if (_FFMpegPath == null)
  114. {
  115. string filename = "ffmpeg.exe";
  116. _FFMpegPath = Path.Combine(FFMpegDirectory, filename);
  117. // Always re-extract the first time to handle new versions
  118. if (File.Exists(_FFMpegPath))
  119. {
  120. File.Delete(_FFMpegPath);
  121. }
  122. // Extract ffprobe
  123. using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Api.FFMpeg." + filename))
  124. {
  125. using (FileStream fileStream = new FileStream(_FFMpegPath, FileMode.Create))
  126. {
  127. stream.CopyTo(fileStream);
  128. }
  129. }
  130. }
  131. return _FFMpegPath;
  132. }
  133. }
  134. }
  135. }