2
0

LibraryManager.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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 Task CreateItem(BaseItem item, CancellationToken cancellationToken)
  872. {
  873. return CreateItems(new[] { item }, cancellationToken);
  874. }
  875. /// <summary>
  876. /// Creates the items.
  877. /// </summary>
  878. /// <param name="items">The items.</param>
  879. /// <param name="cancellationToken">The cancellation token.</param>
  880. /// <returns>Task.</returns>
  881. public async Task CreateItems(IEnumerable<BaseItem> items, CancellationToken cancellationToken)
  882. {
  883. var list = items.ToList();
  884. await ItemRepository.SaveItems(list, cancellationToken).ConfigureAwait(false);
  885. foreach (var item in list)
  886. {
  887. UpdateItemInLibraryCache(item);
  888. }
  889. if (ItemAdded != null)
  890. {
  891. foreach (var item in list)
  892. {
  893. try
  894. {
  895. ItemAdded(this, new ItemChangeEventArgs { Item = item });
  896. }
  897. catch (Exception ex)
  898. {
  899. _logger.ErrorException("Error in ItemUpdated event handler", ex);
  900. }
  901. }
  902. }
  903. }
  904. /// <summary>
  905. /// Updates the item.
  906. /// </summary>
  907. /// <param name="item">The item.</param>
  908. /// <param name="cancellationToken">The cancellation token.</param>
  909. /// <returns>Task.</returns>
  910. public async Task UpdateItem(BaseItem item, CancellationToken cancellationToken)
  911. {
  912. await ItemRepository.SaveItem(item, cancellationToken).ConfigureAwait(false);
  913. UpdateItemInLibraryCache(item);
  914. if (ItemUpdated != null)
  915. {
  916. try
  917. {
  918. ItemUpdated(this, new ItemChangeEventArgs { Item = item });
  919. }
  920. catch (Exception ex)
  921. {
  922. _logger.ErrorException("Error in ItemUpdated event handler", ex);
  923. }
  924. }
  925. }
  926. /// <summary>
  927. /// Reports the item removed.
  928. /// </summary>
  929. /// <param name="item">The item.</param>
  930. public void ReportItemRemoved(BaseItem item)
  931. {
  932. if (ItemRemoved != null)
  933. {
  934. try
  935. {
  936. ItemRemoved(this, new ItemChangeEventArgs { Item = item });
  937. }
  938. catch (Exception ex)
  939. {
  940. _logger.ErrorException("Error in ItemRemoved event handler", ex);
  941. }
  942. }
  943. }
  944. /// <summary>
  945. /// Retrieves the item.
  946. /// </summary>
  947. /// <param name="id">The id.</param>
  948. /// <returns>Task{BaseItem}.</returns>
  949. public BaseItem RetrieveItem(Guid id)
  950. {
  951. return ItemRepository.GetItem(id);
  952. }
  953. /// <summary>
  954. /// Saves the children.
  955. /// </summary>
  956. /// <param name="id">The id.</param>
  957. /// <param name="children">The children.</param>
  958. /// <param name="cancellationToken">The cancellation token.</param>
  959. /// <returns>Task.</returns>
  960. public Task SaveChildren(Guid id, IEnumerable<BaseItem> children, CancellationToken cancellationToken)
  961. {
  962. return ItemRepository.SaveChildren(id, children, cancellationToken);
  963. }
  964. /// <summary>
  965. /// Retrieves the children.
  966. /// </summary>
  967. /// <param name="parent">The parent.</param>
  968. /// <returns>IEnumerable{BaseItem}.</returns>
  969. public IEnumerable<BaseItem> RetrieveChildren(Folder parent)
  970. {
  971. var children = ItemRepository.RetrieveChildren(parent).ToList();
  972. foreach (var child in children)
  973. {
  974. child.Parent = parent;
  975. }
  976. return children;
  977. }
  978. }
  979. }