ApiService.cs 4.4 KB

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