2
0

Kernel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Common.Kernel;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Events;
  12. using MediaBrowser.Controller.IO;
  13. using MediaBrowser.Controller.Library;
  14. using MediaBrowser.Controller.Resolvers;
  15. using MediaBrowser.Model.DTO;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.Progress;
  18. using MediaBrowser.Model.Users;
  19. namespace MediaBrowser.Controller
  20. {
  21. public class Kernel : BaseKernel<ServerConfiguration>
  22. {
  23. public static Kernel Instance { get; private set; }
  24. public ItemController ItemController { get; private set; }
  25. public IEnumerable<User> Users { get; private set; }
  26. public Folder RootFolder { get; private set; }
  27. private DirectoryWatchers DirectoryWatchers { get; set; }
  28. private string MediaRootFolderPath
  29. {
  30. get
  31. {
  32. return ApplicationPaths.RootFolderPath;
  33. }
  34. }
  35. /// <summary>
  36. /// Gets the list of currently registered entity resolvers
  37. /// </summary>
  38. [ImportMany(typeof(IBaseItemResolver))]
  39. public IEnumerable<IBaseItemResolver> EntityResolvers { get; private set; }
  40. /// <summary>
  41. /// Creates a kernal based on a Data path, which is akin to our current programdata path
  42. /// </summary>
  43. public Kernel()
  44. : base()
  45. {
  46. Instance = this;
  47. ItemController = new ItemController();
  48. DirectoryWatchers = new DirectoryWatchers();
  49. ItemController.PreBeginResolvePath += ItemController_PreBeginResolvePath;
  50. ItemController.BeginResolvePath += ItemController_BeginResolvePath;
  51. }
  52. public override void Init(IProgress<TaskProgress> progress)
  53. {
  54. base.Init(progress);
  55. progress.Report(new TaskProgress() { Description = "Loading Users", PercentComplete = 15 });
  56. ReloadUsers();
  57. progress.Report(new TaskProgress() { Description = "Loading Media Library", PercentComplete = 20 });
  58. ReloadRoot();
  59. progress.Report(new TaskProgress() { Description = "Loading Complete", PercentComplete = 100 });
  60. }
  61. protected override void OnComposablePartsLoaded()
  62. {
  63. List<IBaseItemResolver> resolvers = EntityResolvers.ToList();
  64. // Add the internal resolvers
  65. resolvers.Add(new VideoResolver());
  66. resolvers.Add(new AudioResolver());
  67. resolvers.Add(new FolderResolver());
  68. EntityResolvers = resolvers;
  69. // The base class will start up all the plugins
  70. base.OnComposablePartsLoaded();
  71. }
  72. /// <summary>
  73. /// Fires when a path is about to be resolved, but before child folders and files
  74. /// have been collected from the file system.
  75. /// This gives us a chance to cancel it if needed, resulting in the path being ignored
  76. /// </summary>
  77. void ItemController_PreBeginResolvePath(object sender, PreBeginResolveEventArgs e)
  78. {
  79. if (e.IsHidden || e.IsSystemFile)
  80. {
  81. // Ignore hidden files and folders
  82. e.Cancel = true;
  83. }
  84. else if (Path.GetFileName(e.Path).Equals("trailers", StringComparison.OrdinalIgnoreCase))
  85. {
  86. // Ignore any folders named "trailers"
  87. e.Cancel = true;
  88. }
  89. }
  90. /// <summary>
  91. /// Fires when a path is about to be resolved, but after child folders and files
  92. /// This gives us a chance to cancel it if needed, resulting in the path being ignored
  93. /// </summary>
  94. void ItemController_BeginResolvePath(object sender, ItemResolveEventArgs e)
  95. {
  96. if (e.IsFolder)
  97. {
  98. if (e.ContainsFile(".ignore"))
  99. {
  100. // Ignore any folders containing a file called .ignore
  101. e.Cancel = true;
  102. }
  103. }
  104. }
  105. private void ReloadUsers()
  106. {
  107. Users = GetAllUsers();
  108. }
  109. /// <summary>
  110. /// Reloads the root media folder
  111. /// </summary>
  112. public void ReloadRoot()
  113. {
  114. if (!Directory.Exists(MediaRootFolderPath))
  115. {
  116. Directory.CreateDirectory(MediaRootFolderPath);
  117. }
  118. DirectoryWatchers.Stop();
  119. RootFolder = ItemController.GetItem(MediaRootFolderPath) as Folder;
  120. DirectoryWatchers.Start();
  121. }
  122. private static MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
  123. public static Guid GetMD5(string str)
  124. {
  125. lock (md5Provider)
  126. {
  127. return new Guid(md5Provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  128. }
  129. }
  130. public void ReloadItem(BaseItem item)
  131. {
  132. Folder folder = item as Folder;
  133. if (folder != null && folder.IsRoot)
  134. {
  135. ReloadRoot();
  136. }
  137. else
  138. {
  139. if (!Directory.Exists(item.Path) && !File.Exists(item.Path))
  140. {
  141. ReloadItem(item.Parent);
  142. return;
  143. }
  144. BaseItem newItem = ItemController.GetItem(item.Parent, item.Path);
  145. List<BaseItem> children = item.Parent.Children.ToList();
  146. int index = children.IndexOf(item);
  147. children.RemoveAt(index);
  148. children.Insert(index, newItem);
  149. item.Parent.Children = children.ToArray();
  150. }
  151. }
  152. /// <summary>
  153. /// Finds a library item by Id
  154. /// </summary>
  155. public BaseItem GetItemById(Guid id)
  156. {
  157. if (id == Guid.Empty)
  158. {
  159. return RootFolder;
  160. }
  161. return RootFolder.FindById(id);
  162. }
  163. /// <summary>
  164. /// Gets all years from all recursive children of a folder
  165. /// The CategoryInfo class is used to keep track of the number of times each year appears
  166. /// </summary>
  167. public IEnumerable<IBNItem<Year>> GetAllYears(Folder parent, User user)
  168. {
  169. Dictionary<int, int> data = new Dictionary<int, int>();
  170. // Get all the allowed recursive children
  171. IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
  172. foreach (var item in allItems)
  173. {
  174. // Add the year from the item to the data dictionary
  175. // If the year already exists, increment the count
  176. if (item.ProductionYear == null)
  177. {
  178. continue;
  179. }
  180. if (!data.ContainsKey(item.ProductionYear.Value))
  181. {
  182. data.Add(item.ProductionYear.Value, 1);
  183. }
  184. else
  185. {
  186. data[item.ProductionYear.Value]++;
  187. }
  188. }
  189. // Now go through the dictionary and create a Category for each studio
  190. List<IBNItem<Year>> list = new List<IBNItem<Year>>();
  191. foreach (int key in data.Keys)
  192. {
  193. // Get the original entity so that we can also supply the PrimaryImagePath
  194. Year entity = Kernel.Instance.ItemController.GetYear(key);
  195. if (entity != null)
  196. {
  197. list.Add(new IBNItem<Year>()
  198. {
  199. Item = entity,
  200. BaseItemCount = data[key]
  201. });
  202. }
  203. }
  204. return list;
  205. }
  206. /// <summary>
  207. /// Gets all studios from all recursive children of a folder
  208. /// The CategoryInfo class is used to keep track of the number of times each studio appears
  209. /// </summary>
  210. public IEnumerable<IBNItem<Studio>> GetAllStudios(Folder parent, User user)
  211. {
  212. Dictionary<string, int> data = new Dictionary<string, int>();
  213. // Get all the allowed recursive children
  214. IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
  215. foreach (var item in allItems)
  216. {
  217. // Add each studio from the item to the data dictionary
  218. // If the studio already exists, increment the count
  219. if (item.Studios == null)
  220. {
  221. continue;
  222. }
  223. foreach (string val in item.Studios)
  224. {
  225. if (!data.ContainsKey(val))
  226. {
  227. data.Add(val, 1);
  228. }
  229. else
  230. {
  231. data[val]++;
  232. }
  233. }
  234. }
  235. // Now go through the dictionary and create a Category for each studio
  236. List<IBNItem<Studio>> list = new List<IBNItem<Studio>>();
  237. foreach (string key in data.Keys)
  238. {
  239. // Get the original entity so that we can also supply the PrimaryImagePath
  240. Studio entity = Kernel.Instance.ItemController.GetStudio(key);
  241. if (entity != null)
  242. {
  243. list.Add(new IBNItem<Studio>()
  244. {
  245. Item = entity,
  246. BaseItemCount = data[key]
  247. });
  248. }
  249. }
  250. return list;
  251. }
  252. /// <summary>
  253. /// Gets all genres from all recursive children of a folder
  254. /// The CategoryInfo class is used to keep track of the number of times each genres appears
  255. /// </summary>
  256. public IEnumerable<IBNItem<Genre>> GetAllGenres(Folder parent, User user)
  257. {
  258. Dictionary<string, int> data = new Dictionary<string, int>();
  259. // Get all the allowed recursive children
  260. IEnumerable<BaseItem> allItems = parent.GetParentalAllowedRecursiveChildren(user);
  261. foreach (var item in allItems)
  262. {
  263. // Add each genre from the item to the data dictionary
  264. // If the genre already exists, increment the count
  265. if (item.Genres == null)
  266. {
  267. continue;
  268. }
  269. foreach (string val in item.Genres)
  270. {
  271. if (!data.ContainsKey(val))
  272. {
  273. data.Add(val, 1);
  274. }
  275. else
  276. {
  277. data[val]++;
  278. }
  279. }
  280. }
  281. // Now go through the dictionary and create a Category for each genre
  282. List<IBNItem<Genre>> list = new List<IBNItem<Genre>>();
  283. foreach (string key in data.Keys)
  284. {
  285. // Get the original entity so that we can also supply the PrimaryImagePath
  286. Genre entity = Kernel.Instance.ItemController.GetGenre(key);
  287. if (entity != null)
  288. {
  289. list.Add(new IBNItem<Genre>()
  290. {
  291. Item = entity,
  292. BaseItemCount = data[key]
  293. });
  294. }
  295. }
  296. return list;
  297. }
  298. /// <summary>
  299. /// Gets all users within the system
  300. /// </summary>
  301. private IEnumerable<User> GetAllUsers()
  302. {
  303. List<User> list = new List<User>();
  304. // Return a dummy user for now since all calls to get items requre a userId
  305. User user = new User();
  306. user.Name = "Default User";
  307. user.Id = Guid.Parse("5d1cf7fce25943b790d140095457a42b");
  308. list.Add(user);
  309. return list;
  310. }
  311. }
  312. }