Kernel.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 list of available item repositories
  82. /// </summary>
  83. /// <value>The item repositories.</value>
  84. public IEnumerable<IItemRepository> ItemRepositories { get; set; }
  85. /// <summary>
  86. /// Gets the active item repository
  87. /// </summary>
  88. /// <value>The item repository.</value>
  89. public IItemRepository ItemRepository { get; set; }
  90. /// <summary>
  91. /// Gets the list of available item repositories
  92. /// </summary>
  93. /// <value>The user data repositories.</value>
  94. public IEnumerable<IUserDataRepository> UserDataRepositories { get; set; }
  95. /// <summary>
  96. /// Gets the active user data repository
  97. /// </summary>
  98. /// <value>The user data repository.</value>
  99. public IUserDataRepository UserDataRepository { get; set; }
  100. private readonly IServerConfigurationManager _configurationManager;
  101. /// <summary>
  102. /// Creates a kernel based on a Data path, which is akin to our current programdata path
  103. /// </summary>
  104. /// <param name="configurationManager">The configuration manager.</param>
  105. public Kernel(IServerConfigurationManager configurationManager)
  106. {
  107. Instance = this;
  108. _configurationManager = configurationManager;
  109. }
  110. /// <summary>
  111. /// Called when [composable parts loaded].
  112. /// </summary>
  113. /// <returns>Task.</returns>
  114. public Task LoadRepositories(IServerConfigurationManager configurationManager)
  115. {
  116. // Get the current item repository
  117. ItemRepository = GetRepository(ItemRepositories, configurationManager.Configuration.ItemRepository);
  118. var itemRepoTask = ItemRepository.Initialize();
  119. // Get the current user repository
  120. UserRepository = GetRepository(UserRepositories, configurationManager.Configuration.UserRepository);
  121. var userRepoTask = UserRepository.Initialize();
  122. // Get the current item repository
  123. UserDataRepository = GetRepository(UserDataRepositories, configurationManager.Configuration.UserDataRepository);
  124. var userDataRepoTask = UserDataRepository.Initialize();
  125. return Task.WhenAll(itemRepoTask, userRepoTask, userDataRepoTask);
  126. }
  127. /// <summary>
  128. /// Gets a repository by name from a list, and returns the default if not found
  129. /// </summary>
  130. /// <typeparam name="T"></typeparam>
  131. /// <param name="repositories">The repositories.</param>
  132. /// <param name="name">The name.</param>
  133. /// <returns>``0.</returns>
  134. private T GetRepository<T>(IEnumerable<T> repositories, string name)
  135. where T : class, IRepository
  136. {
  137. var enumerable = repositories as T[] ?? repositories.ToArray();
  138. return enumerable.FirstOrDefault(r => string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)) ??
  139. enumerable.FirstOrDefault();
  140. }
  141. }
  142. }