Kernel.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Json;
  9. using MediaBrowser.Common.Logging;
  10. using MediaBrowser.Common.Net;
  11. using MediaBrowser.Common.Plugins;
  12. using MediaBrowser.Controller.Events;
  13. using MediaBrowser.Controller.IO;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Controller.Resolvers;
  16. using MediaBrowser.Model.Configuration;
  17. using MediaBrowser.Model.Entities;
  18. using MediaBrowser.Model.Users;
  19. namespace MediaBrowser.Controller
  20. {
  21. public class Kernel
  22. {
  23. public static Kernel Instance { get; private set; }
  24. public string DataPath { get; private set; }
  25. public HttpServer HttpServer { get; private set; }
  26. public ItemController ItemController { get; private set; }
  27. public UserController UserController { get; private set; }
  28. public PluginController PluginController { get; private set; }
  29. public Configuration Configuration { get; private set; }
  30. public IEnumerable<IPlugin> Plugins { get; private set; }
  31. public IEnumerable<User> Users { get; private set; }
  32. public Folder RootFolder { get; private set; }
  33. private DirectoryWatchers DirectoryWatchers { get; set; }
  34. private string MediaRootFolderPath
  35. {
  36. get
  37. {
  38. return Path.Combine(DataPath, "Root");
  39. }
  40. }
  41. /// <summary>
  42. /// Creates a kernal based on a Data path, which is akin to our current programdata path
  43. /// </summary>
  44. public Kernel(string dataPath)
  45. {
  46. Instance = this;
  47. DataPath = dataPath;
  48. Logger.LoggerInstance = new FileLogger(Path.Combine(DataPath, "Logs"));
  49. ItemController = new ItemController();
  50. UserController = new UserController(Path.Combine(DataPath, "Users"));
  51. PluginController = new PluginController(Path.Combine(DataPath, "Plugins"));
  52. DirectoryWatchers = new DirectoryWatchers();
  53. ItemController.PreBeginResolvePath += ItemController_PreBeginResolvePath;
  54. ItemController.BeginResolvePath += ItemController_BeginResolvePath;
  55. // Add support for core media types - audio, video, etc
  56. AddBaseItemType<Folder, FolderResolver>();
  57. AddBaseItemType<Audio, AudioResolver>();
  58. AddBaseItemType<Video, VideoResolver>();
  59. }
  60. /// <summary>
  61. /// Tells the kernel to start spinning up
  62. /// </summary>
  63. public void Init()
  64. {
  65. ReloadConfiguration();
  66. ReloadHttpServer();
  67. ReloadPlugins();
  68. // Get users from users folder
  69. // Load root media folder
  70. Parallel.Invoke(ReloadUsers, ReloadRoot);
  71. var b = true;
  72. }
  73. private void ReloadConfiguration()
  74. {
  75. // Deserialize config
  76. Configuration = GetConfiguration(DataPath);
  77. Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity;
  78. }
  79. private void ReloadPlugins()
  80. {
  81. // Find plugins
  82. Plugins = PluginController.GetAllPlugins();
  83. Parallel.For(0, Plugins.Count(), i =>
  84. {
  85. Plugins.ElementAt(i).Init();
  86. });
  87. }
  88. private void ReloadHttpServer()
  89. {
  90. if (HttpServer != null)
  91. {
  92. HttpServer.Dispose();
  93. }
  94. HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/");
  95. }
  96. /// <summary>
  97. /// Registers a new BaseItem subclass
  98. /// </summary>
  99. public void AddBaseItemType<TBaseItemType, TResolverType>()
  100. where TBaseItemType : BaseItem, new()
  101. where TResolverType : BaseItemResolver<TBaseItemType>, new()
  102. {
  103. ItemController.AddResovler<TBaseItemType, TResolverType>();
  104. }
  105. /// <summary>
  106. /// Fires when a path is about to be resolved, but before child folders and files
  107. /// have been collected from the file system.
  108. /// This gives us a chance to cancel it if needed, resulting in the path being ignored
  109. /// </summary>
  110. void ItemController_PreBeginResolvePath(object sender, PreBeginResolveEventArgs e)
  111. {
  112. if (e.IsHidden || e.IsSystemFile)
  113. {
  114. // Ignore hidden files and folders
  115. e.Cancel = true;
  116. }
  117. else if (Path.GetFileName(e.Path).Equals("trailers", StringComparison.OrdinalIgnoreCase))
  118. {
  119. // Ignore any folders named "trailers"
  120. e.Cancel = true;
  121. }
  122. }
  123. /// <summary>
  124. /// Fires when a path is about to be resolved, but after child folders and files
  125. /// This gives us a chance to cancel it if needed, resulting in the path being ignored
  126. /// </summary>
  127. void ItemController_BeginResolvePath(object sender, ItemResolveEventArgs e)
  128. {
  129. if (e.IsFolder)
  130. {
  131. if (e.ContainsFile(".ignore"))
  132. {
  133. // Ignore any folders containing a file called .ignore
  134. e.Cancel = true;
  135. }
  136. }
  137. }
  138. private void ReloadUsers()
  139. {
  140. Users = UserController.GetAllUsers();
  141. }
  142. /// <summary>
  143. /// Reloads the root media folder
  144. /// </summary>
  145. public void ReloadRoot()
  146. {
  147. if (!Directory.Exists(MediaRootFolderPath))
  148. {
  149. Directory.CreateDirectory(MediaRootFolderPath);
  150. }
  151. DirectoryWatchers.Stop();
  152. RootFolder = ItemController.GetItem(MediaRootFolderPath) as Folder;
  153. DirectoryWatchers.Start();
  154. }
  155. private static MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
  156. public static Guid GetMD5(string str)
  157. {
  158. lock (md5Provider)
  159. {
  160. return new Guid(md5Provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  161. }
  162. }
  163. private static Configuration GetConfiguration(string directory)
  164. {
  165. string file = Path.Combine(directory, "config.js");
  166. if (!File.Exists(file))
  167. {
  168. return new Configuration();
  169. }
  170. return JsonSerializer.Deserialize<Configuration>(file);
  171. }
  172. public void ReloadItem(BaseItem item)
  173. {
  174. Folder folder = item as Folder;
  175. if (folder != null && folder.IsRoot)
  176. {
  177. ReloadRoot();
  178. }
  179. else
  180. {
  181. if (!Directory.Exists(item.Path) && !File.Exists(item.Path))
  182. {
  183. ReloadItem(item.Parent);
  184. return;
  185. }
  186. BaseItem newItem = ItemController.GetItem(item.Parent, item.Path);
  187. List<BaseItem> children = item.Parent.Children.ToList();
  188. int index = children.IndexOf(item);
  189. children.RemoveAt(index);
  190. children.Insert(index, newItem);
  191. item.Parent.Children = children.ToArray();
  192. }
  193. }
  194. }
  195. }