ApiService.cs 4.9 KB

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