LibraryManager.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Progress;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Entities.Audio;
  7. using MediaBrowser.Controller.IO;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Persistence;
  10. using MediaBrowser.Controller.Resolvers;
  11. using MediaBrowser.Controller.Sorting;
  12. using MediaBrowser.Model.Configuration;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Logging;
  15. using MediaBrowser.Server.Implementations.ScheduledTasks;
  16. using MoreLinq;
  17. using System;
  18. using System.Collections.Concurrent;
  19. using System.Collections.Generic;
  20. using System.Globalization;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. using SortOrder = MediaBrowser.Model.Entities.SortOrder;
  26. namespace MediaBrowser.Server.Implementations.Library
  27. {
  28. /// <summary>
  29. /// Class LibraryManager
  30. /// </summary>
  31. public class LibraryManager : ILibraryManager
  32. {
  33. private IEnumerable<ILibraryPrescanTask> PrescanTasks { get; set; }
  34. /// <summary>
  35. /// Gets the intro providers.
  36. /// </summary>
  37. /// <value>The intro providers.</value>
  38. private IEnumerable<IIntroProvider> IntroProviders { get; set; }
  39. /// <summary>
  40. /// Gets the list of entity resolution ignore rules
  41. /// </summary>
  42. /// <value>The entity resolution ignore rules.</value>
  43. private IEnumerable<IResolverIgnoreRule> EntityResolutionIgnoreRules { get; set; }
  44. /// <summary>
  45. /// Gets the list of BasePluginFolders added by plugins
  46. /// </summary>
  47. /// <value>The plugin folders.</value>
  48. private IEnumerable<IVirtualFolderCreator> PluginFolderCreators { get; set; }
  49. /// <summary>
  50. /// Gets the list of currently registered entity resolvers
  51. /// </summary>
  52. /// <value>The entity resolvers enumerable.</value>
  53. private IEnumerable<IItemResolver> EntityResolvers { get; set; }
  54. /// <summary>
  55. /// Gets or sets the comparers.
  56. /// </summary>
  57. /// <value>The comparers.</value>
  58. private IEnumerable<IBaseItemComparer> Comparers { get; set; }
  59. /// <summary>
  60. /// Gets the active item repository
  61. /// </summary>
  62. /// <value>The item repository.</value>
  63. public IItemRepository ItemRepository { get; set; }
  64. /// <summary>
  65. /// Occurs when [item added].
  66. /// </summary>
  67. public event EventHandler<ItemChangeEventArgs> ItemAdded;
  68. /// <summary>
  69. /// Occurs when [item updated].
  70. /// </summary>
  71. public event EventHandler<ItemChangeEventArgs> ItemUpdated;
  72. /// <summary>
  73. /// Occurs when [item removed].
  74. /// </summary>
  75. public event EventHandler<ItemChangeEventArgs> ItemRemoved;
  76. /// <summary>
  77. /// The _logger
  78. /// </summary>
  79. private readonly ILogger _logger;
  80. /// <summary>
  81. /// The _task manager
  82. /// </summary>
  83. private readonly ITaskManager _taskManager;
  84. /// <summary>
  85. /// The _user manager
  86. /// </summary>
  87. private readonly IUserManager _userManager;
  88. private readonly IUserDataRepository _userDataRepository;
  89. /// <summary>
  90. /// Gets or sets the configuration manager.
  91. /// </summary>
  92. /// <value>The configuration manager.</value>
  93. private IServerConfigurationManager ConfigurationManager { get; set; }
  94. /// <summary>
  95. /// A collection of items that may be referenced from multiple physical places in the library
  96. /// (typically, multiple user roots). We store them here and be sure they all reference a
  97. /// single instance.
  98. /// </summary>
  99. private ConcurrentDictionary<Guid, BaseItem> ByReferenceItems { get; set; }
  100. private ConcurrentDictionary<Guid, BaseItem> _libraryItemsCache;
  101. private object _libraryItemsCacheSyncLock = new object();
  102. private bool _libraryItemsCacheInitialized;
  103. private ConcurrentDictionary<Guid, BaseItem> LibraryItemsCache
  104. {
  105. get
  106. {
  107. LazyInitializer.EnsureInitialized(ref _libraryItemsCache, ref _libraryItemsCacheInitialized, ref _libraryItemsCacheSyncLock, CreateLibraryItemsCache);
  108. return _libraryItemsCache;
  109. }
  110. }
  111. private readonly ConcurrentDictionary<string, UserRootFolder> _userRootFolders =
  112. new ConcurrentDictionary<string, UserRootFolder>();
  113. /// <summary>
  114. /// Initializes a new instance of the <see cref="LibraryManager" /> class.
  115. /// </summary>
  116. /// <param name="logger">The logger.</param>
  117. /// <param name="taskManager">The task manager.</param>
  118. /// <param name="userManager">The user manager.</param>
  119. /// <param name="configurationManager">The configuration manager.</param>
  120. /// <param name="userDataRepository">The user data repository.</param>
  121. public LibraryManager(ILogger logger, ITaskManager taskManager, IUserManager userManager, IServerConfigurationManager configurationManager, IUserDataRepository userDataRepository)
  122. {
  123. _logger = logger;
  124. _taskManager = taskManager;
  125. _userManager = userManager;
  126. ConfigurationManager = configurationManager;
  127. _userDataRepository = userDataRepository;
  128. ByReferenceItems = new ConcurrentDictionary<Guid, BaseItem>();
  129. ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated;
  130. RecordConfigurationValues(configurationManager.Configuration);
  131. }
  132. /// <summary>
  133. /// Adds the parts.
  134. /// </summary>
  135. /// <param name="rules">The rules.</param>
  136. /// <param name="pluginFolders">The plugin folders.</param>
  137. /// <param name="resolvers">The resolvers.</param>
  138. /// <param name="introProviders">The intro providers.</param>
  139. /// <param name="itemComparers">The item comparers.</param>
  140. public void AddParts(IEnumerable<IResolverIgnoreRule> rules,
  141. IEnumerable<IVirtualFolderCreator> pluginFolders,
  142. IEnumerable<IItemResolver> resolvers,
  143. IEnumerable<IIntroProvider> introProviders,
  144. IEnumerable<IBaseItemComparer> itemComparers,
  145. IEnumerable<ILibraryPrescanTask> prescanTasks)
  146. {
  147. EntityResolutionIgnoreRules = rules;
  148. PluginFolderCreators = pluginFolders;
  149. EntityResolvers = resolvers.OrderBy(i => i.Priority).ToArray();
  150. IntroProviders = introProviders;
  151. Comparers = itemComparers;
  152. PrescanTasks = prescanTasks;
  153. }
  154. /// <summary>
  155. /// The _root folder
  156. /// </summary>
  157. private AggregateFolder _rootFolder;
  158. /// <summary>
  159. /// The _root folder sync lock
  160. /// </summary>
  161. private object _rootFolderSyncLock = new object();
  162. /// <summary>
  163. /// The _root folder initialized
  164. /// </summary>
  165. private bool _rootFolderInitialized;
  166. /// <summary>
  167. /// Gets the root folder.
  168. /// </summary>
  169. /// <value>The root folder.</value>
  170. public AggregateFolder RootFolder
  171. {
  172. get
  173. {
  174. LazyInitializer.EnsureInitialized(ref _rootFolder, ref _rootFolderInitialized, ref _rootFolderSyncLock, CreateRootFolder);
  175. return _rootFolder;
  176. }
  177. private set
  178. {
  179. _rootFolder = value;
  180. if (value == null)
  181. {
  182. _rootFolderInitialized = false;
  183. }
  184. }
  185. }
  186. private bool _internetProvidersEnabled;
  187. private bool _peopleImageFetchingEnabled;
  188. private string _itemsByNamePath;
  189. private void RecordConfigurationValues(ServerConfiguration configuration)
  190. {
  191. _itemsByNamePath = ConfigurationManager.ApplicationPaths.ItemsByNamePath;
  192. _internetProvidersEnabled = configuration.EnableInternetProviders;
  193. _peopleImageFetchingEnabled = configuration.InternetProviderExcludeTypes == null || !configuration.InternetProviderExcludeTypes.Contains(typeof(Person).Name, StringComparer.OrdinalIgnoreCase);
  194. }
  195. /// <summary>
  196. /// Configurations the updated.
  197. /// </summary>
  198. /// <param name="sender">The sender.</param>
  199. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  200. void ConfigurationUpdated(object sender, EventArgs e)
  201. {
  202. var config = ConfigurationManager.Configuration;
  203. // Figure out whether or not we should refresh people after the update is finished
  204. var refreshPeopleAfterUpdate = !_internetProvidersEnabled && config.EnableInternetProviders;
  205. // This is true if internet providers has just been turned on, or if People have just been removed from InternetProviderExcludeTypes
  206. if (!refreshPeopleAfterUpdate)
  207. {
  208. var newConfigurationFetchesPeopleImages = config.InternetProviderExcludeTypes == null || !config.InternetProviderExcludeTypes.Contains(typeof(Person).Name, StringComparer.OrdinalIgnoreCase);
  209. refreshPeopleAfterUpdate = newConfigurationFetchesPeopleImages && !_peopleImageFetchingEnabled;
  210. }
  211. var ibnPathChanged = !string.Equals(_itemsByNamePath, ConfigurationManager.ApplicationPaths.ItemsByNamePath);
  212. if (ibnPathChanged)
  213. {
  214. _itemsByName.Clear();
  215. }
  216. RecordConfigurationValues(config);
  217. Task.Run(() =>
  218. {
  219. // Any number of configuration settings could change the way the library is refreshed, so do that now
  220. _taskManager.CancelIfRunningAndQueue<RefreshMediaLibraryTask>();
  221. if (refreshPeopleAfterUpdate)
  222. {
  223. _taskManager.CancelIfRunningAndQueue<PeopleValidationTask>();
  224. }
  225. });
  226. }
  227. /// <summary>
  228. /// Creates the library items cache.
  229. /// </summary>
  230. /// <returns>ConcurrentDictionary{GuidBaseItem}.</returns>
  231. private ConcurrentDictionary<Guid, BaseItem> CreateLibraryItemsCache()
  232. {
  233. var items = RootFolder.RecursiveChildren.ToList();
  234. items.Add(RootFolder);
  235. // Need to use DistinctBy Id because there could be multiple instances with the same id
  236. // due to sharing the default library
  237. var userRootFolders = _userManager.Users.Select(i => i.RootFolder)
  238. .Distinct()
  239. .ToList();
  240. items.AddRange(userRootFolders);
  241. // Get all user collection folders
  242. // Skip BasePluginFolders because we already got them from RootFolder.RecursiveChildren
  243. var userFolders =
  244. userRootFolders.SelectMany(i => i.Children)
  245. .Where(i => !(i is BasePluginFolder))
  246. .ToList();
  247. items.AddRange(userFolders);
  248. return new ConcurrentDictionary<Guid, BaseItem>(items.ToDictionary(i => i.Id));
  249. }
  250. /// <summary>
  251. /// Updates the item in library cache.
  252. /// </summary>
  253. /// <param name="item">The item.</param>
  254. private void UpdateItemInLibraryCache(BaseItem item)
  255. {
  256. LibraryItemsCache.AddOrUpdate(item.Id, item, delegate { return item; });
  257. }
  258. /// <summary>
  259. /// Resolves the item.
  260. /// </summary>
  261. /// <param name="args">The args.</param>
  262. /// <returns>BaseItem.</returns>
  263. public BaseItem ResolveItem(ItemResolveArgs args)
  264. {
  265. var item = EntityResolvers.Select(r =>
  266. {
  267. try
  268. {
  269. return r.ResolvePath(args);
  270. }
  271. catch (Exception ex)
  272. {
  273. _logger.ErrorException("Error in {0} resolving {1}", ex, r.GetType().Name, args.Path);
  274. return null;
  275. }
  276. }).FirstOrDefault(i => i != null);
  277. if (item != null)
  278. {
  279. ResolverHelper.SetInitialItemValues(item, args);
  280. // Now handle the issue with posibly having the same item referenced from multiple physical
  281. // places within the library. Be sure we always end up with just one instance.
  282. if (item is IByReferenceItem)
  283. {
  284. item = GetOrAddByReferenceItem(item);
  285. }
  286. }
  287. return item;
  288. }
  289. /// <summary>
  290. /// Ensure supplied item has only one instance throughout
  291. /// </summary>
  292. /// <param name="item"></param>
  293. /// <returns>The proper instance to the item</returns>
  294. public BaseItem GetOrAddByReferenceItem(BaseItem item)
  295. {
  296. // Add this item to our list if not there already
  297. if (!ByReferenceItems.TryAdd(item.Id, item))
  298. {
  299. // Already there - return the existing reference
  300. item = ByReferenceItems[item.Id];
  301. }
  302. return item;
  303. }
  304. /// <summary>
  305. /// Resolves a path into a BaseItem
  306. /// </summary>
  307. /// <param name="path">The path.</param>
  308. /// <param name="parent">The parent.</param>
  309. /// <param name="fileInfo">The file info.</param>
  310. /// <returns>BaseItem.</returns>
  311. /// <exception cref="System.ArgumentNullException"></exception>
  312. public BaseItem ResolvePath(string path, Folder parent = null, FileSystemInfo fileInfo = null)
  313. {
  314. if (string.IsNullOrEmpty(path))
  315. {
  316. throw new ArgumentNullException();
  317. }
  318. fileInfo = fileInfo ?? FileSystem.GetFileSystemInfo(path);
  319. if (!fileInfo.Exists)
  320. {
  321. return null;
  322. }
  323. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths)
  324. {
  325. Parent = parent,
  326. Path = path,
  327. FileInfo = fileInfo
  328. };
  329. // Return null if ignore rules deem that we should do so
  330. if (EntityResolutionIgnoreRules.Any(r => r.ShouldIgnore(args)))
  331. {
  332. return null;
  333. }
  334. // Gather child folder and files
  335. if (args.IsDirectory)
  336. {
  337. var isPhysicalRoot = args.IsPhysicalRoot;
  338. // When resolving the root, we need it's grandchildren (children of user views)
  339. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  340. args.FileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, _logger, flattenFolderDepth: flattenFolderDepth, args: args, resolveShortcuts: isPhysicalRoot || args.IsVf);
  341. }
  342. // Check to see if we should resolve based on our contents
  343. if (args.IsDirectory && !ShouldResolvePathContents(args))
  344. {
  345. return null;
  346. }
  347. return ResolveItem(args);
  348. }
  349. /// <summary>
  350. /// Determines whether a path should be ignored based on its contents - called after the contents have been read
  351. /// </summary>
  352. /// <param name="args">The args.</param>
  353. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  354. private static bool ShouldResolvePathContents(ItemResolveArgs args)
  355. {
  356. // Ignore any folders containing a file called .ignore
  357. return !args.ContainsFileSystemEntryByName(".ignore");
  358. }
  359. /// <summary>
  360. /// Resolves a set of files into a list of BaseItem
  361. /// </summary>
  362. /// <typeparam name="T"></typeparam>
  363. /// <param name="files">The files.</param>
  364. /// <param name="parent">The parent.</param>
  365. /// <returns>List{``0}.</returns>
  366. public List<T> ResolvePaths<T>(IEnumerable<FileSystemInfo> files, Folder parent)
  367. where T : BaseItem
  368. {
  369. var list = new List<T>();
  370. Parallel.ForEach(files, f =>
  371. {
  372. try
  373. {
  374. var item = ResolvePath(f.FullName, parent, f) as T;
  375. if (item != null)
  376. {
  377. lock (list)
  378. {
  379. list.Add(item);
  380. }
  381. }
  382. }
  383. catch (Exception ex)
  384. {
  385. _logger.ErrorException("Error resolving path {0}", ex, f.FullName);
  386. }
  387. });
  388. return list;
  389. }
  390. /// <summary>
  391. /// Creates the root media folder
  392. /// </summary>
  393. /// <returns>AggregateFolder.</returns>
  394. /// <exception cref="System.InvalidOperationException">Cannot create the root folder until plugins have loaded</exception>
  395. public AggregateFolder CreateRootFolder()
  396. {
  397. var rootFolderPath = ConfigurationManager.ApplicationPaths.RootFolderPath;
  398. var rootFolder = RetrieveItem(rootFolderPath.GetMBId(typeof(AggregateFolder))) as AggregateFolder ?? (AggregateFolder)ResolvePath(rootFolderPath);
  399. // Add in the plug-in folders
  400. foreach (var child in PluginFolderCreators)
  401. {
  402. rootFolder.AddVirtualChild(child.GetFolder());
  403. }
  404. return rootFolder;
  405. }
  406. /// <summary>
  407. /// Gets the user root folder.
  408. /// </summary>
  409. /// <param name="userRootPath">The user root path.</param>
  410. /// <returns>UserRootFolder.</returns>
  411. public UserRootFolder GetUserRootFolder(string userRootPath)
  412. {
  413. return _userRootFolders.GetOrAdd(userRootPath, key => RetrieveItem(userRootPath.GetMBId(typeof(UserRootFolder))) as UserRootFolder ?? (UserRootFolder)ResolvePath(userRootPath));
  414. }
  415. /// <summary>
  416. /// Gets a Person
  417. /// </summary>
  418. /// <param name="name">The name.</param>
  419. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  420. /// <returns>Task{Person}.</returns>
  421. public Task<Person> GetPerson(string name, bool allowSlowProviders = false)
  422. {
  423. return GetPerson(name, CancellationToken.None, allowSlowProviders);
  424. }
  425. /// <summary>
  426. /// Gets a Person
  427. /// </summary>
  428. /// <param name="name">The name.</param>
  429. /// <param name="cancellationToken">The cancellation token.</param>
  430. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  431. /// <param name="forceCreation">if set to <c>true</c> [force creation].</param>
  432. /// <returns>Task{Person}.</returns>
  433. private Task<Person> GetPerson(string name, CancellationToken cancellationToken, bool allowSlowProviders = false, bool forceCreation = false)
  434. {
  435. return GetItemByName<Person>(ConfigurationManager.ApplicationPaths.PeoplePath, name, cancellationToken, allowSlowProviders, forceCreation);
  436. }
  437. /// <summary>
  438. /// Gets a Studio
  439. /// </summary>
  440. /// <param name="name">The name.</param>
  441. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  442. /// <returns>Task{Studio}.</returns>
  443. public Task<Studio> GetStudio(string name, bool allowSlowProviders = false)
  444. {
  445. return GetItemByName<Studio>(ConfigurationManager.ApplicationPaths.StudioPath, name, CancellationToken.None, allowSlowProviders);
  446. }
  447. /// <summary>
  448. /// Gets a Genre
  449. /// </summary>
  450. /// <param name="name">The name.</param>
  451. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  452. /// <returns>Task{Genre}.</returns>
  453. public Task<Genre> GetGenre(string name, bool allowSlowProviders = false)
  454. {
  455. return GetItemByName<Genre>(ConfigurationManager.ApplicationPaths.GenrePath, name, CancellationToken.None, allowSlowProviders);
  456. }
  457. /// <summary>
  458. /// Gets a Genre
  459. /// </summary>
  460. /// <param name="name">The name.</param>
  461. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  462. /// <returns>Task{Genre}.</returns>
  463. public Task<Artist> GetArtist(string name, bool allowSlowProviders = false)
  464. {
  465. return GetArtist(name, CancellationToken.None, allowSlowProviders);
  466. }
  467. /// <summary>
  468. /// Gets the artist.
  469. /// </summary>
  470. /// <param name="name">The name.</param>
  471. /// <param name="cancellationToken">The cancellation token.</param>
  472. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  473. /// <param name="forceCreation">if set to <c>true</c> [force creation].</param>
  474. /// <returns>Task{Artist}.</returns>
  475. private Task<Artist> GetArtist(string name, CancellationToken cancellationToken, bool allowSlowProviders = false, bool forceCreation = false)
  476. {
  477. return GetItemByName<Artist>(ConfigurationManager.ApplicationPaths.ArtistsPath, name, cancellationToken, allowSlowProviders, forceCreation);
  478. }
  479. /// <summary>
  480. /// The us culture
  481. /// </summary>
  482. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  483. /// <summary>
  484. /// Gets a Year
  485. /// </summary>
  486. /// <param name="value">The value.</param>
  487. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  488. /// <returns>Task{Year}.</returns>
  489. /// <exception cref="System.ArgumentOutOfRangeException"></exception>
  490. public Task<Year> GetYear(int value, bool allowSlowProviders = false)
  491. {
  492. if (value <= 0)
  493. {
  494. throw new ArgumentOutOfRangeException();
  495. }
  496. return GetItemByName<Year>(ConfigurationManager.ApplicationPaths.YearPath, value.ToString(UsCulture), CancellationToken.None, allowSlowProviders);
  497. }
  498. /// <summary>
  499. /// The images by name item cache
  500. /// </summary>
  501. private readonly ConcurrentDictionary<string, BaseItem> _itemsByName = new ConcurrentDictionary<string, BaseItem>(StringComparer.OrdinalIgnoreCase);
  502. /// <summary>
  503. /// Generically retrieves an IBN item
  504. /// </summary>
  505. /// <typeparam name="T"></typeparam>
  506. /// <param name="path">The path.</param>
  507. /// <param name="name">The name.</param>
  508. /// <param name="cancellationToken">The cancellation token.</param>
  509. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  510. /// <param name="forceCreation">if set to <c>true</c> [force creation].</param>
  511. /// <returns>Task{``0}.</returns>
  512. /// <exception cref="System.ArgumentNullException">
  513. /// </exception>
  514. private async Task<T> GetItemByName<T>(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true, bool forceCreation = false)
  515. where T : BaseItem, new()
  516. {
  517. if (string.IsNullOrEmpty(path))
  518. {
  519. throw new ArgumentNullException();
  520. }
  521. if (string.IsNullOrEmpty(name))
  522. {
  523. throw new ArgumentNullException();
  524. }
  525. var key = Path.Combine(path, FileSystem.GetValidFilename(name));
  526. BaseItem obj;
  527. if (forceCreation || !_itemsByName.TryGetValue(key, out obj))
  528. {
  529. obj = await CreateItemByName<T>(path, name, cancellationToken, allowSlowProviders).ConfigureAwait(false);
  530. _itemsByName.AddOrUpdate(key, obj, (keyName, oldValue) => obj);
  531. }
  532. return obj as T;
  533. }
  534. /// <summary>
  535. /// Creates an IBN item based on a given path
  536. /// </summary>
  537. /// <typeparam name="T"></typeparam>
  538. /// <param name="path">The path.</param>
  539. /// <param name="name">The name.</param>
  540. /// <param name="cancellationToken">The cancellation token.</param>
  541. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  542. /// <returns>Task{``0}.</returns>
  543. /// <exception cref="System.IO.IOException">Path not created: + path</exception>
  544. private async Task<T> CreateItemByName<T>(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true)
  545. where T : BaseItem, new()
  546. {
  547. cancellationToken.ThrowIfCancellationRequested();
  548. path = Path.Combine(path, FileSystem.GetValidFilename(name));
  549. var fileInfo = new DirectoryInfo(path);
  550. var isNew = false;
  551. if (!fileInfo.Exists)
  552. {
  553. Directory.CreateDirectory(path);
  554. fileInfo = new DirectoryInfo(path);
  555. if (!fileInfo.Exists)
  556. {
  557. throw new IOException("Path not created: " + path);
  558. }
  559. isNew = true;
  560. }
  561. cancellationToken.ThrowIfCancellationRequested();
  562. var id = path.GetMBId(typeof(T));
  563. var item = RetrieveItem(id) as T;
  564. if (item == null)
  565. {
  566. item = new T
  567. {
  568. Name = name,
  569. Id = id,
  570. DateCreated = fileInfo.CreationTimeUtc,
  571. DateModified = fileInfo.LastWriteTimeUtc,
  572. Path = path
  573. };
  574. isNew = true;
  575. }
  576. cancellationToken.ThrowIfCancellationRequested();
  577. // Set this now so we don't cause additional file system access during provider executions
  578. item.ResetResolveArgs(fileInfo);
  579. await item.RefreshMetadata(cancellationToken, isNew, allowSlowProviders: allowSlowProviders).ConfigureAwait(false);
  580. cancellationToken.ThrowIfCancellationRequested();
  581. return item;
  582. }
  583. /// <summary>
  584. /// Validate and refresh the People sub-set of the IBN.
  585. /// The items are stored in the db but not loaded into memory until actually requested by an operation.
  586. /// </summary>
  587. /// <param name="cancellationToken">The cancellation token.</param>
  588. /// <param name="progress">The progress.</param>
  589. /// <returns>Task.</returns>
  590. public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
  591. {
  592. const int maxTasks = 25;
  593. var tasks = new List<Task>();
  594. var includedPersonTypes = new[] { PersonType.Actor, PersonType.Director, PersonType.GuestStar, PersonType.Writer, PersonType.Director, PersonType.Producer };
  595. var people = RootFolder.RecursiveChildren
  596. .Where(c => c.People != null)
  597. .SelectMany(c => c.People.Where(p => includedPersonTypes.Contains(p.Type)))
  598. .DistinctBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
  599. .ToList();
  600. var numComplete = 0;
  601. foreach (var person in people)
  602. {
  603. if (tasks.Count > maxTasks)
  604. {
  605. await Task.WhenAll(tasks).ConfigureAwait(false);
  606. tasks.Clear();
  607. // Safe cancellation point, when there are no pending tasks
  608. cancellationToken.ThrowIfCancellationRequested();
  609. }
  610. // Avoid accessing the foreach variable within the closure
  611. var currentPerson = person;
  612. tasks.Add(Task.Run(async () =>
  613. {
  614. cancellationToken.ThrowIfCancellationRequested();
  615. try
  616. {
  617. await GetPerson(currentPerson.Name, cancellationToken, true, true).ConfigureAwait(false);
  618. }
  619. catch (IOException ex)
  620. {
  621. _logger.ErrorException("Error validating IBN entry {0}", ex, currentPerson.Name);
  622. }
  623. // Update progress
  624. lock (progress)
  625. {
  626. numComplete++;
  627. double percent = numComplete;
  628. percent /= people.Count;
  629. progress.Report(100 * percent);
  630. }
  631. }));
  632. }
  633. await Task.WhenAll(tasks).ConfigureAwait(false);
  634. progress.Report(100);
  635. _logger.Info("People validation complete");
  636. }
  637. public async Task ValidateArtists(CancellationToken cancellationToken, IProgress<double> progress)
  638. {
  639. const int maxTasks = 25;
  640. var tasks = new List<Task>();
  641. var artists = RootFolder.RecursiveChildren
  642. .OfType<Audio>()
  643. .SelectMany(c =>
  644. {
  645. var list = new List<string>();
  646. if (!string.IsNullOrEmpty(c.AlbumArtist))
  647. {
  648. list.Add(c.AlbumArtist);
  649. }
  650. if (!string.IsNullOrEmpty(c.Artist))
  651. {
  652. list.Add(c.Artist);
  653. }
  654. return list;
  655. })
  656. .Distinct(StringComparer.OrdinalIgnoreCase)
  657. .ToList();
  658. var numComplete = 0;
  659. foreach (var artist in artists)
  660. {
  661. if (tasks.Count > maxTasks)
  662. {
  663. await Task.WhenAll(tasks).ConfigureAwait(false);
  664. tasks.Clear();
  665. // Safe cancellation point, when there are no pending tasks
  666. cancellationToken.ThrowIfCancellationRequested();
  667. }
  668. // Avoid accessing the foreach variable within the closure
  669. var currentArtist = artist;
  670. tasks.Add(Task.Run(async () =>
  671. {
  672. cancellationToken.ThrowIfCancellationRequested();
  673. try
  674. {
  675. await GetArtist(currentArtist, cancellationToken, true, true).ConfigureAwait(false);
  676. }
  677. catch (IOException ex)
  678. {
  679. _logger.ErrorException("Error validating Artist {0}", ex, currentArtist);
  680. }
  681. // Update progress
  682. lock (progress)
  683. {
  684. numComplete++;
  685. double percent = numComplete;
  686. percent /= artists.Count;
  687. progress.Report(100 * percent);
  688. }
  689. }));
  690. }
  691. await Task.WhenAll(tasks).ConfigureAwait(false);
  692. progress.Report(100);
  693. _logger.Info("Artist validation complete");
  694. }
  695. /// <summary>
  696. /// Reloads the root media folder
  697. /// </summary>
  698. /// <param name="progress">The progress.</param>
  699. /// <param name="cancellationToken">The cancellation token.</param>
  700. /// <returns>Task.</returns>
  701. public Task ValidateMediaLibrary(IProgress<double> progress, CancellationToken cancellationToken)
  702. {
  703. // Just run the scheduled task so that the user can see it
  704. return Task.Run(() => _taskManager.CancelIfRunningAndQueue<RefreshMediaLibraryTask>());
  705. }
  706. /// <summary>
  707. /// Validates the media library internal.
  708. /// </summary>
  709. /// <param name="progress">The progress.</param>
  710. /// <param name="cancellationToken">The cancellation token.</param>
  711. /// <returns>Task.</returns>
  712. public async Task ValidateMediaLibraryInternal(IProgress<double> progress, CancellationToken cancellationToken)
  713. {
  714. _logger.Info("Validating media library");
  715. await RootFolder.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  716. // Start by just validating the children of the root, but go no further
  717. await RootFolder.ValidateChildren(new Progress<double>(), cancellationToken, recursive: false);
  718. foreach (var folder in _userManager.Users.Select(u => u.RootFolder).Distinct())
  719. {
  720. await ValidateCollectionFolders(folder, cancellationToken).ConfigureAwait(false);
  721. }
  722. // Run prescan tasks
  723. foreach (var task in PrescanTasks)
  724. {
  725. try
  726. {
  727. await task.Run(new Progress<double>(), cancellationToken);
  728. }
  729. catch (Exception ex)
  730. {
  731. _logger.ErrorException("Error running prescan task", ex);
  732. }
  733. }
  734. var innerProgress = new ActionableProgress<double>();
  735. innerProgress.RegisterAction(pct => progress.Report(pct * .8));
  736. // Now validate the entire media library
  737. await RootFolder.ValidateChildren(innerProgress, cancellationToken, recursive: true).ConfigureAwait(false);
  738. innerProgress = new ActionableProgress<double>();
  739. innerProgress.RegisterAction(pct => progress.Report(80 + pct * .2));
  740. await ValidateArtists(cancellationToken, innerProgress);
  741. progress.Report(100);
  742. }
  743. /// <summary>
  744. /// Validates only the collection folders for a User and goes no further
  745. /// </summary>
  746. /// <param name="userRootFolder">The user root folder.</param>
  747. /// <param name="cancellationToken">The cancellation token.</param>
  748. /// <returns>Task.</returns>
  749. private async Task ValidateCollectionFolders(UserRootFolder userRootFolder, CancellationToken cancellationToken)
  750. {
  751. _logger.Info("Validating collection folders within {0}", userRootFolder.Path);
  752. await userRootFolder.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  753. cancellationToken.ThrowIfCancellationRequested();
  754. await userRootFolder.ValidateChildren(new Progress<double>(), cancellationToken, recursive: false).ConfigureAwait(false);
  755. }
  756. /// <summary>
  757. /// Gets the default view.
  758. /// </summary>
  759. /// <returns>IEnumerable{VirtualFolderInfo}.</returns>
  760. public IEnumerable<VirtualFolderInfo> GetDefaultVirtualFolders()
  761. {
  762. return GetView(ConfigurationManager.ApplicationPaths.DefaultUserViewsPath);
  763. }
  764. /// <summary>
  765. /// Gets the view.
  766. /// </summary>
  767. /// <param name="user">The user.</param>
  768. /// <returns>IEnumerable{VirtualFolderInfo}.</returns>
  769. public IEnumerable<VirtualFolderInfo> GetVirtualFolders(User user)
  770. {
  771. return GetView(user.RootFolderPath);
  772. }
  773. /// <summary>
  774. /// Gets the view.
  775. /// </summary>
  776. /// <param name="path">The path.</param>
  777. /// <returns>IEnumerable{VirtualFolderInfo}.</returns>
  778. private IEnumerable<VirtualFolderInfo> GetView(string path)
  779. {
  780. return Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly)
  781. .Select(dir => new VirtualFolderInfo
  782. {
  783. Name = Path.GetFileName(dir),
  784. Locations = Directory.EnumerateFiles(dir, "*.lnk", SearchOption.TopDirectoryOnly).Select(FileSystem.ResolveShortcut).OrderBy(i => i).ToList()
  785. });
  786. }
  787. /// <summary>
  788. /// Gets the item by id.
  789. /// </summary>
  790. /// <param name="id">The id.</param>
  791. /// <returns>BaseItem.</returns>
  792. /// <exception cref="System.ArgumentNullException">id</exception>
  793. public BaseItem GetItemById(Guid id)
  794. {
  795. if (id == Guid.Empty)
  796. {
  797. throw new ArgumentNullException("id");
  798. }
  799. BaseItem item;
  800. if (LibraryItemsCache.TryGetValue(id, out item))
  801. {
  802. return item;
  803. }
  804. return ItemRepository.GetItem(id);
  805. }
  806. /// <summary>
  807. /// Gets the intros.
  808. /// </summary>
  809. /// <param name="item">The item.</param>
  810. /// <param name="user">The user.</param>
  811. /// <returns>IEnumerable{System.String}.</returns>
  812. public IEnumerable<string> GetIntros(BaseItem item, User user)
  813. {
  814. return IntroProviders.SelectMany(i => i.GetIntros(item, user));
  815. }
  816. /// <summary>
  817. /// Sorts the specified sort by.
  818. /// </summary>
  819. /// <param name="items">The items.</param>
  820. /// <param name="user">The user.</param>
  821. /// <param name="sortBy">The sort by.</param>
  822. /// <param name="sortOrder">The sort order.</param>
  823. /// <returns>IEnumerable{BaseItem}.</returns>
  824. public IEnumerable<BaseItem> Sort(IEnumerable<BaseItem> items, User user, IEnumerable<string> sortBy, SortOrder sortOrder)
  825. {
  826. var isFirst = true;
  827. IOrderedEnumerable<BaseItem> orderedItems = null;
  828. foreach (var orderBy in sortBy.Select(o => GetComparer(o, user)).Where(c => c != null))
  829. {
  830. if (isFirst)
  831. {
  832. orderedItems = sortOrder == SortOrder.Descending ? items.OrderByDescending(i => i, orderBy) : items.OrderBy(i => i, orderBy);
  833. }
  834. else
  835. {
  836. orderedItems = sortOrder == SortOrder.Descending ? orderedItems.ThenByDescending(i => i, orderBy) : orderedItems.ThenBy(i => i, orderBy);
  837. }
  838. isFirst = false;
  839. }
  840. return orderedItems ?? items;
  841. }
  842. /// <summary>
  843. /// Gets the comparer.
  844. /// </summary>
  845. /// <param name="name">The name.</param>
  846. /// <param name="user">The user.</param>
  847. /// <returns>IBaseItemComparer.</returns>
  848. private IBaseItemComparer GetComparer(string name, User user)
  849. {
  850. var comparer = Comparers.FirstOrDefault(c => string.Equals(name, c.Name, StringComparison.OrdinalIgnoreCase));
  851. if (comparer != null)
  852. {
  853. // If it requires a user, create a new one, and assign the user
  854. if (comparer is IUserBaseItemComparer)
  855. {
  856. var userComparer = (IUserBaseItemComparer)Activator.CreateInstance(comparer.GetType());
  857. userComparer.User = user;
  858. userComparer.UserManager = _userManager;
  859. userComparer.UserDataRepository = _userDataRepository;
  860. return userComparer;
  861. }
  862. }
  863. return comparer;
  864. }
  865. /// <summary>
  866. /// Creates the item.
  867. /// </summary>
  868. /// <param name="item">The item.</param>
  869. /// <param name="cancellationToken">The cancellation token.</param>
  870. /// <returns>Task.</returns>
  871. public async Task CreateItem(BaseItem item, CancellationToken cancellationToken)
  872. {
  873. await SaveItem(item, cancellationToken).ConfigureAwait(false);
  874. UpdateItemInLibraryCache(item);
  875. if (ItemAdded != null)
  876. {
  877. try
  878. {
  879. ItemAdded(this, new ItemChangeEventArgs { Item = item });
  880. }
  881. catch (Exception ex)
  882. {
  883. _logger.ErrorException("Error in ItemUpdated event handler", ex);
  884. }
  885. }
  886. }
  887. /// <summary>
  888. /// Updates the item.
  889. /// </summary>
  890. /// <param name="item">The item.</param>
  891. /// <param name="cancellationToken">The cancellation token.</param>
  892. /// <returns>Task.</returns>
  893. public async Task UpdateItem(BaseItem item, CancellationToken cancellationToken)
  894. {
  895. await SaveItem(item, cancellationToken).ConfigureAwait(false);
  896. UpdateItemInLibraryCache(item);
  897. if (ItemUpdated != null)
  898. {
  899. try
  900. {
  901. ItemUpdated(this, new ItemChangeEventArgs { Item = item });
  902. }
  903. catch (Exception ex)
  904. {
  905. _logger.ErrorException("Error in ItemUpdated event handler", ex);
  906. }
  907. }
  908. }
  909. /// <summary>
  910. /// Reports the item removed.
  911. /// </summary>
  912. /// <param name="item">The item.</param>
  913. public void ReportItemRemoved(BaseItem item)
  914. {
  915. if (ItemRemoved != null)
  916. {
  917. try
  918. {
  919. ItemRemoved(this, new ItemChangeEventArgs { Item = item });
  920. }
  921. catch (Exception ex)
  922. {
  923. _logger.ErrorException("Error in ItemRemoved event handler", ex);
  924. }
  925. }
  926. }
  927. /// <summary>
  928. /// Saves the item.
  929. /// </summary>
  930. /// <param name="item">The item.</param>
  931. /// <param name="cancellationToken">The cancellation token.</param>
  932. /// <returns>Task.</returns>
  933. private Task SaveItem(BaseItem item, CancellationToken cancellationToken)
  934. {
  935. return ItemRepository.SaveItem(item, cancellationToken);
  936. }
  937. /// <summary>
  938. /// Retrieves the item.
  939. /// </summary>
  940. /// <param name="id">The id.</param>
  941. /// <returns>Task{BaseItem}.</returns>
  942. public BaseItem RetrieveItem(Guid id)
  943. {
  944. return ItemRepository.GetItem(id);
  945. }
  946. /// <summary>
  947. /// Saves the children.
  948. /// </summary>
  949. /// <param name="id">The id.</param>
  950. /// <param name="children">The children.</param>
  951. /// <param name="cancellationToken">The cancellation token.</param>
  952. /// <returns>Task.</returns>
  953. public Task SaveChildren(Guid id, IEnumerable<BaseItem> children, CancellationToken cancellationToken)
  954. {
  955. return ItemRepository.SaveChildren(id, children, cancellationToken);
  956. }
  957. /// <summary>
  958. /// Retrieves the children.
  959. /// </summary>
  960. /// <param name="parent">The parent.</param>
  961. /// <returns>IEnumerable{BaseItem}.</returns>
  962. public IEnumerable<BaseItem> RetrieveChildren(Folder parent)
  963. {
  964. var children = ItemRepository.RetrieveChildren(parent).ToList();
  965. foreach (var child in children)
  966. {
  967. child.Parent = parent;
  968. }
  969. return children;
  970. }
  971. }
  972. }