LibraryManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.IO;
  5. using MediaBrowser.Controller.Resolvers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MoreLinq;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace MediaBrowser.Controller.Library
  18. {
  19. /// <summary>
  20. /// Class LibraryManager
  21. /// </summary>
  22. public class LibraryManager : BaseManager<Kernel>
  23. {
  24. #region LibraryChanged Event
  25. /// <summary>
  26. /// Fires whenever any validation routine adds or removes items. The added and removed items are properties of the args.
  27. /// *** Will fire asynchronously. ***
  28. /// </summary>
  29. public event EventHandler<ChildrenChangedEventArgs> LibraryChanged;
  30. /// <summary>
  31. /// Raises the <see cref="E:LibraryChanged" /> event.
  32. /// </summary>
  33. /// <param name="args">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
  34. internal void OnLibraryChanged(ChildrenChangedEventArgs args)
  35. {
  36. EventHelper.QueueEventIfNotNull(LibraryChanged, this, args, _logger);
  37. // Had to put this in a separate method to avoid an implicitly captured closure
  38. SendLibraryChangedWebSocketMessage(args);
  39. }
  40. /// <summary>
  41. /// Sends the library changed web socket message.
  42. /// </summary>
  43. /// <param name="args">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
  44. private void SendLibraryChangedWebSocketMessage(ChildrenChangedEventArgs args)
  45. {
  46. // Notify connected ui's
  47. Kernel.TcpManager.SendWebSocketMessage("LibraryChanged", () => DtoBuilder.GetLibraryUpdateInfo(args));
  48. }
  49. #endregion
  50. /// <summary>
  51. /// The _logger
  52. /// </summary>
  53. private readonly ILogger _logger;
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="LibraryManager" /> class.
  56. /// </summary>
  57. /// <param name="kernel">The kernel.</param>
  58. /// <param name="logger">The logger.</param>
  59. public LibraryManager(Kernel kernel, ILogger logger)
  60. : base(kernel)
  61. {
  62. _logger = logger;
  63. }
  64. /// <summary>
  65. /// Resolves the item.
  66. /// </summary>
  67. /// <param name="args">The args.</param>
  68. /// <returns>BaseItem.</returns>
  69. public BaseItem ResolveItem(ItemResolveArgs args)
  70. {
  71. return Kernel.EntityResolvers.Select(r => r.ResolvePath(args)).FirstOrDefault(i => i != null);
  72. }
  73. /// <summary>
  74. /// Resolves a path into a BaseItem
  75. /// </summary>
  76. /// <param name="path">The path.</param>
  77. /// <param name="parent">The parent.</param>
  78. /// <param name="fileInfo">The file info.</param>
  79. /// <returns>BaseItem.</returns>
  80. /// <exception cref="System.ArgumentNullException"></exception>
  81. public BaseItem GetItem(string path, Folder parent = null, WIN32_FIND_DATA? fileInfo = null)
  82. {
  83. if (string.IsNullOrEmpty(path))
  84. {
  85. throw new ArgumentNullException();
  86. }
  87. fileInfo = fileInfo ?? FileSystem.GetFileData(path);
  88. if (!fileInfo.HasValue)
  89. {
  90. return null;
  91. }
  92. var args = new ItemResolveArgs
  93. {
  94. Parent = parent,
  95. Path = path,
  96. FileInfo = fileInfo.Value
  97. };
  98. // Return null if ignore rules deem that we should do so
  99. if (Kernel.EntityResolutionIgnoreRules.Any(r => r.ShouldIgnore(args)))
  100. {
  101. return null;
  102. }
  103. // Gather child folder and files
  104. if (args.IsDirectory)
  105. {
  106. // When resolving the root, we need it's grandchildren (children of user views)
  107. var flattenFolderDepth = args.IsPhysicalRoot ? 2 : 0;
  108. args.FileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, _logger, flattenFolderDepth: flattenFolderDepth, args: args);
  109. }
  110. // Check to see if we should resolve based on our contents
  111. if (args.IsDirectory && !EntityResolutionHelper.ShouldResolvePathContents(args))
  112. {
  113. return null;
  114. }
  115. return ResolveItem(args);
  116. }
  117. /// <summary>
  118. /// Resolves a set of files into a list of BaseItem
  119. /// </summary>
  120. /// <typeparam name="T"></typeparam>
  121. /// <param name="files">The files.</param>
  122. /// <param name="parent">The parent.</param>
  123. /// <returns>List{``0}.</returns>
  124. public List<T> GetItems<T>(IEnumerable<WIN32_FIND_DATA> files, Folder parent)
  125. where T : BaseItem
  126. {
  127. var list = new List<T>();
  128. Parallel.ForEach(files, f =>
  129. {
  130. try
  131. {
  132. var item = GetItem(f.Path, parent, f) as T;
  133. if (item != null)
  134. {
  135. lock (list)
  136. {
  137. list.Add(item);
  138. }
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. _logger.ErrorException("Error resolving path {0}", ex, f.Path);
  144. }
  145. });
  146. return list;
  147. }
  148. /// <summary>
  149. /// Creates the root media folder
  150. /// </summary>
  151. /// <returns>AggregateFolder.</returns>
  152. /// <exception cref="System.InvalidOperationException">Cannot create the root folder until plugins have loaded</exception>
  153. internal AggregateFolder CreateRootFolder()
  154. {
  155. if (Kernel.Plugins == null)
  156. {
  157. throw new InvalidOperationException("Cannot create the root folder until plugins have loaded");
  158. }
  159. var rootFolderPath = Kernel.ApplicationPaths.RootFolderPath;
  160. var rootFolder = Kernel.ItemRepository.RetrieveItem(rootFolderPath.GetMBId(typeof(AggregateFolder))) as AggregateFolder ?? (AggregateFolder)GetItem(rootFolderPath);
  161. // Add in the plug-in folders
  162. foreach (var child in Kernel.PluginFolderCreators)
  163. {
  164. rootFolder.AddVirtualChild(child.GetFolder());
  165. }
  166. return rootFolder;
  167. }
  168. /// <summary>
  169. /// Gets a Person
  170. /// </summary>
  171. /// <param name="name">The name.</param>
  172. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  173. /// <returns>Task{Person}.</returns>
  174. public Task<Person> GetPerson(string name, bool allowSlowProviders = false)
  175. {
  176. return GetPerson(name, CancellationToken.None, allowSlowProviders);
  177. }
  178. /// <summary>
  179. /// Gets a Person
  180. /// </summary>
  181. /// <param name="name">The name.</param>
  182. /// <param name="cancellationToken">The cancellation token.</param>
  183. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  184. /// <returns>Task{Person}.</returns>
  185. private Task<Person> GetPerson(string name, CancellationToken cancellationToken, bool allowSlowProviders = false)
  186. {
  187. return GetImagesByNameItem<Person>(Kernel.ApplicationPaths.PeoplePath, name, cancellationToken, allowSlowProviders);
  188. }
  189. /// <summary>
  190. /// Gets a Studio
  191. /// </summary>
  192. /// <param name="name">The name.</param>
  193. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  194. /// <returns>Task{Studio}.</returns>
  195. public Task<Studio> GetStudio(string name, bool allowSlowProviders = false)
  196. {
  197. return GetImagesByNameItem<Studio>(Kernel.ApplicationPaths.StudioPath, name, CancellationToken.None, allowSlowProviders);
  198. }
  199. /// <summary>
  200. /// Gets a Genre
  201. /// </summary>
  202. /// <param name="name">The name.</param>
  203. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  204. /// <returns>Task{Genre}.</returns>
  205. public Task<Genre> GetGenre(string name, bool allowSlowProviders = false)
  206. {
  207. return GetImagesByNameItem<Genre>(Kernel.ApplicationPaths.GenrePath, name, CancellationToken.None, allowSlowProviders);
  208. }
  209. /// <summary>
  210. /// The us culture
  211. /// </summary>
  212. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  213. /// <summary>
  214. /// Gets a Year
  215. /// </summary>
  216. /// <param name="value">The value.</param>
  217. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  218. /// <returns>Task{Year}.</returns>
  219. /// <exception cref="System.ArgumentOutOfRangeException"></exception>
  220. public Task<Year> GetYear(int value, bool allowSlowProviders = false)
  221. {
  222. if (value <= 0)
  223. {
  224. throw new ArgumentOutOfRangeException();
  225. }
  226. return GetImagesByNameItem<Year>(Kernel.ApplicationPaths.YearPath, value.ToString(UsCulture), CancellationToken.None, allowSlowProviders);
  227. }
  228. /// <summary>
  229. /// The images by name item cache
  230. /// </summary>
  231. private readonly ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  232. /// <summary>
  233. /// Generically retrieves an IBN item
  234. /// </summary>
  235. /// <typeparam name="T"></typeparam>
  236. /// <param name="path">The path.</param>
  237. /// <param name="name">The name.</param>
  238. /// <param name="cancellationToken">The cancellation token.</param>
  239. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  240. /// <returns>Task{``0}.</returns>
  241. /// <exception cref="System.ArgumentNullException"></exception>
  242. private Task<T> GetImagesByNameItem<T>(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true)
  243. where T : BaseItem, new()
  244. {
  245. if (string.IsNullOrEmpty(path))
  246. {
  247. throw new ArgumentNullException();
  248. }
  249. if (string.IsNullOrEmpty(name))
  250. {
  251. throw new ArgumentNullException();
  252. }
  253. var key = Path.Combine(path, FileSystem.GetValidFilename(name));
  254. var obj = ImagesByNameItemCache.GetOrAdd(key, keyname => CreateImagesByNameItem<T>(path, name, cancellationToken, allowSlowProviders));
  255. return obj as Task<T>;
  256. }
  257. /// <summary>
  258. /// Creates an IBN item based on a given path
  259. /// </summary>
  260. /// <typeparam name="T"></typeparam>
  261. /// <param name="path">The path.</param>
  262. /// <param name="name">The name.</param>
  263. /// <param name="cancellationToken">The cancellation token.</param>
  264. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  265. /// <returns>Task{``0}.</returns>
  266. /// <exception cref="System.IO.IOException">Path not created: + path</exception>
  267. private async Task<T> CreateImagesByNameItem<T>(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true)
  268. where T : BaseItem, new()
  269. {
  270. cancellationToken.ThrowIfCancellationRequested();
  271. _logger.Debug("Creating {0}: {1}", typeof(T).Name, name);
  272. path = Path.Combine(path, FileSystem.GetValidFilename(name));
  273. var fileInfo = FileSystem.GetFileData(path);
  274. var isNew = false;
  275. if (!fileInfo.HasValue)
  276. {
  277. Directory.CreateDirectory(path);
  278. fileInfo = FileSystem.GetFileData(path);
  279. if (!fileInfo.HasValue)
  280. {
  281. throw new IOException("Path not created: " + path);
  282. }
  283. isNew = true;
  284. }
  285. cancellationToken.ThrowIfCancellationRequested();
  286. var id = path.GetMBId(typeof(T));
  287. var item = Kernel.ItemRepository.RetrieveItem(id) as T;
  288. if (item == null)
  289. {
  290. item = new T
  291. {
  292. Name = name,
  293. Id = id,
  294. DateCreated = fileInfo.Value.CreationTimeUtc,
  295. DateModified = fileInfo.Value.LastWriteTimeUtc,
  296. Path = path
  297. };
  298. isNew = true;
  299. }
  300. cancellationToken.ThrowIfCancellationRequested();
  301. // Set this now so we don't cause additional file system access during provider executions
  302. item.ResetResolveArgs(fileInfo);
  303. await item.RefreshMetadata(cancellationToken, isNew, allowSlowProviders: allowSlowProviders).ConfigureAwait(false);
  304. cancellationToken.ThrowIfCancellationRequested();
  305. return item;
  306. }
  307. /// <summary>
  308. /// Validate and refresh the People sub-set of the IBN.
  309. /// The items are stored in the db but not loaded into memory until actually requested by an operation.
  310. /// </summary>
  311. /// <param name="cancellationToken">The cancellation token.</param>
  312. /// <param name="progress">The progress.</param>
  313. /// <returns>Task.</returns>
  314. internal async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
  315. {
  316. // Clear the IBN cache
  317. ImagesByNameItemCache.Clear();
  318. const int maxTasks = 250;
  319. var tasks = new List<Task>();
  320. var includedPersonTypes = new[] { PersonType.Actor, PersonType.Director };
  321. var people = Kernel.RootFolder.RecursiveChildren
  322. .Where(c => c.People != null)
  323. .SelectMany(c => c.People.Where(p => includedPersonTypes.Contains(p.Type)))
  324. .DistinctBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
  325. .ToList();
  326. var numComplete = 0;
  327. foreach (var person in people)
  328. {
  329. if (tasks.Count > maxTasks)
  330. {
  331. await Task.WhenAll(tasks).ConfigureAwait(false);
  332. tasks.Clear();
  333. // Safe cancellation point, when there are no pending tasks
  334. cancellationToken.ThrowIfCancellationRequested();
  335. }
  336. // Avoid accessing the foreach variable within the closure
  337. var currentPerson = person;
  338. tasks.Add(Task.Run(async () =>
  339. {
  340. cancellationToken.ThrowIfCancellationRequested();
  341. try
  342. {
  343. await GetPerson(currentPerson.Name, cancellationToken, allowSlowProviders: true).ConfigureAwait(false);
  344. }
  345. catch (IOException ex)
  346. {
  347. _logger.ErrorException("Error validating IBN entry {0}", ex, currentPerson.Name);
  348. }
  349. // Update progress
  350. lock (progress)
  351. {
  352. numComplete++;
  353. double percent = numComplete;
  354. percent /= people.Count;
  355. progress.Report(100 * percent);
  356. }
  357. }));
  358. }
  359. await Task.WhenAll(tasks).ConfigureAwait(false);
  360. progress.Report(100);
  361. _logger.Info("People validation complete");
  362. }
  363. /// <summary>
  364. /// Reloads the root media folder
  365. /// </summary>
  366. /// <param name="progress">The progress.</param>
  367. /// <param name="cancellationToken">The cancellation token.</param>
  368. /// <returns>Task.</returns>
  369. internal async Task ValidateMediaLibrary(IProgress<double> progress, CancellationToken cancellationToken)
  370. {
  371. _logger.Info("Validating media library");
  372. await Kernel.RootFolder.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  373. // Start by just validating the children of the root, but go no further
  374. await Kernel.RootFolder.ValidateChildren(new Progress<double> { }, cancellationToken, recursive: false);
  375. // Validate only the collection folders for each user, just to make them available as quickly as possible
  376. var userCollectionFolderTasks = Kernel.Users.AsParallel().Select(user => user.ValidateCollectionFolders(new Progress<double> { }, cancellationToken));
  377. await Task.WhenAll(userCollectionFolderTasks).ConfigureAwait(false);
  378. // Now validate the entire media library
  379. await Kernel.RootFolder.ValidateChildren(progress, cancellationToken, recursive: true).ConfigureAwait(false);
  380. foreach (var user in Kernel.Users)
  381. {
  382. await user.ValidateMediaLibrary(new Progress<double> { }, cancellationToken).ConfigureAwait(false);
  383. }
  384. }
  385. /// <summary>
  386. /// Saves display preferences for a Folder
  387. /// </summary>
  388. /// <param name="user">The user.</param>
  389. /// <param name="folder">The folder.</param>
  390. /// <param name="data">The data.</param>
  391. /// <returns>Task.</returns>
  392. public Task SaveDisplayPreferencesForFolder(User user, Folder folder, DisplayPreferences data)
  393. {
  394. // Need to update all items with the same DisplayPrefsId
  395. foreach (var child in Kernel.RootFolder.GetRecursiveChildren(user)
  396. .OfType<Folder>()
  397. .Where(i => i.DisplayPrefsId == folder.DisplayPrefsId))
  398. {
  399. child.AddOrUpdateDisplayPrefs(user, data);
  400. }
  401. return Kernel.DisplayPreferencesRepository.SaveDisplayPrefs(folder, CancellationToken.None);
  402. }
  403. /// <summary>
  404. /// Gets the default view.
  405. /// </summary>
  406. /// <returns>IEnumerable{VirtualFolderInfo}.</returns>
  407. public IEnumerable<VirtualFolderInfo> GetDefaultVirtualFolders()
  408. {
  409. return GetView(Kernel.ApplicationPaths.DefaultUserViewsPath);
  410. }
  411. /// <summary>
  412. /// Gets the view.
  413. /// </summary>
  414. /// <param name="user">The user.</param>
  415. /// <returns>IEnumerable{VirtualFolderInfo}.</returns>
  416. public IEnumerable<VirtualFolderInfo> GetVirtualFolders(User user)
  417. {
  418. return GetView(user.RootFolderPath);
  419. }
  420. /// <summary>
  421. /// Gets the view.
  422. /// </summary>
  423. /// <param name="path">The path.</param>
  424. /// <returns>IEnumerable{VirtualFolderInfo}.</returns>
  425. private IEnumerable<VirtualFolderInfo> GetView(string path)
  426. {
  427. return Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly)
  428. .Select(dir => new VirtualFolderInfo
  429. {
  430. Name = Path.GetFileName(dir),
  431. Locations = Directory.EnumerateFiles(dir, "*.lnk", SearchOption.TopDirectoryOnly).Select(FileSystem.ResolveShortcut).ToList()
  432. });
  433. }
  434. }
  435. }