ApiService.cs 4.8 KB

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