Kernel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Drawing;
  3. using MediaBrowser.Controller.Localization;
  4. using MediaBrowser.Controller.MediaInfo;
  5. using MediaBrowser.Controller.Persistence;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Controller.Weather;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Controller
  13. {
  14. /// <summary>
  15. /// Class Kernel
  16. /// </summary>
  17. public class Kernel
  18. {
  19. /// <summary>
  20. /// Gets the instance.
  21. /// </summary>
  22. /// <value>The instance.</value>
  23. public static Kernel Instance { get; private set; }
  24. /// <summary>
  25. /// Gets the image manager.
  26. /// </summary>
  27. /// <value>The image manager.</value>
  28. public ImageManager ImageManager { get; set; }
  29. /// <summary>
  30. /// Gets the FFMPEG controller.
  31. /// </summary>
  32. /// <value>The FFMPEG controller.</value>
  33. public FFMpegManager FFMpegManager { get; set; }
  34. /// <summary>
  35. /// Gets the name of the web application that can be used for url building.
  36. /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
  37. /// </summary>
  38. /// <value>The name of the web application.</value>
  39. public string WebApplicationName
  40. {
  41. get { return "mediabrowser"; }
  42. }
  43. /// <summary>
  44. /// Gets the HTTP server URL prefix.
  45. /// </summary>
  46. /// <value>The HTTP server URL prefix.</value>
  47. public virtual string HttpServerUrlPrefix
  48. {
  49. get
  50. {
  51. return "http://+:" + _configurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/";
  52. }
  53. }
  54. /// <summary>
  55. /// Gets the list of Localized string files
  56. /// </summary>
  57. /// <value>The string files.</value>
  58. public IEnumerable<LocalizedStringData> StringFiles { get; set; }
  59. /// <summary>
  60. /// Gets the list of currently registered weather prvoiders
  61. /// </summary>
  62. /// <value>The weather providers.</value>
  63. public IEnumerable<IWeatherProvider> WeatherProviders { get; set; }
  64. /// <summary>
  65. /// Gets the list of currently registered image processors
  66. /// Image processors are specialized metadata providers that run after the normal ones
  67. /// </summary>
  68. /// <value>The image enhancers.</value>
  69. public IEnumerable<IImageEnhancer> ImageEnhancers { get; set; }
  70. /// <summary>
  71. /// Gets the list of available user repositories
  72. /// </summary>
  73. /// <value>The user repositories.</value>
  74. public IEnumerable<IUserRepository> UserRepositories { get; set; }
  75. /// <summary>
  76. /// Gets the active user repository
  77. /// </summary>
  78. /// <value>The user repository.</value>
  79. public IUserRepository UserRepository { get; set; }
  80. /// <summary>
  81. /// Gets the active user repository
  82. /// </summary>
  83. /// <value>The display preferences repository.</value>
  84. public IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
  85. /// <summary>
  86. /// Gets the list of available item repositories
  87. /// </summary>
  88. /// <value>The item repositories.</value>
  89. public IEnumerable<IItemRepository> ItemRepositories { get; set; }
  90. /// <summary>
  91. /// Gets the active item repository
  92. /// </summary>
  93. /// <value>The item repository.</value>
  94. public IItemRepository ItemRepository { get; set; }
  95. /// <summary>
  96. /// Gets the list of available DisplayPreferencesRepositories
  97. /// </summary>
  98. /// <value>The display preferences repositories.</value>
  99. public IEnumerable<IDisplayPreferencesRepository> DisplayPreferencesRepositories { get; set; }
  100. /// <summary>
  101. /// Gets the list of available item repositories
  102. /// </summary>
  103. /// <value>The user data repositories.</value>
  104. public IEnumerable<IUserDataRepository> UserDataRepositories { get; set; }
  105. /// <summary>
  106. /// Gets the active user data repository
  107. /// </summary>
  108. /// <value>The user data repository.</value>
  109. public IUserDataRepository UserDataRepository { get; set; }
  110. private readonly IServerConfigurationManager _configurationManager;
  111. /// <summary>
  112. /// Creates a kernel based on a Data path, which is akin to our current programdata path
  113. /// </summary>
  114. /// <param name="configurationManager">The configuration manager.</param>
  115. public Kernel(IServerConfigurationManager configurationManager)
  116. {
  117. Instance = this;
  118. _configurationManager = configurationManager;
  119. }
  120. /// <summary>
  121. /// Called when [composable parts loaded].
  122. /// </summary>
  123. /// <returns>Task.</returns>
  124. public Task LoadRepositories(IServerConfigurationManager configurationManager)
  125. {
  126. // Get the current item repository
  127. ItemRepository = GetRepository(ItemRepositories, configurationManager.Configuration.ItemRepository);
  128. var itemRepoTask = ItemRepository.Initialize();
  129. // Get the current user repository
  130. UserRepository = GetRepository(UserRepositories, configurationManager.Configuration.UserRepository);
  131. var userRepoTask = UserRepository.Initialize();
  132. // Get the current item repository
  133. UserDataRepository = GetRepository(UserDataRepositories, configurationManager.Configuration.UserDataRepository);
  134. var userDataRepoTask = UserDataRepository.Initialize();
  135. // Get the current display preferences repository
  136. DisplayPreferencesRepository = GetRepository(DisplayPreferencesRepositories, configurationManager.Configuration.DisplayPreferencesRepository);
  137. var displayPreferencesRepoTask = DisplayPreferencesRepository.Initialize();
  138. return Task.WhenAll(itemRepoTask, userRepoTask, userDataRepoTask, displayPreferencesRepoTask);
  139. }
  140. /// <summary>
  141. /// Gets a repository by name from a list, and returns the default if not found
  142. /// </summary>
  143. /// <typeparam name="T"></typeparam>
  144. /// <param name="repositories">The repositories.</param>
  145. /// <param name="name">The name.</param>
  146. /// <returns>``0.</returns>
  147. private T GetRepository<T>(IEnumerable<T> repositories, string name)
  148. where T : class, IRepository
  149. {
  150. var enumerable = repositories as T[] ?? repositories.ToArray();
  151. return enumerable.FirstOrDefault(r => string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)) ??
  152. enumerable.FirstOrDefault();
  153. }
  154. }
  155. }