CollectionFolder.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using MediaBrowser.Common.Extensions;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.Serialization;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.Entities
  11. {
  12. /// <summary>
  13. /// Specialized Folder class that points to a subset of the physical folders in the system.
  14. /// It is created from the user-specific folders within the system root
  15. /// </summary>
  16. public class CollectionFolder : Folder, ICollectionFolder
  17. {
  18. /// <summary>
  19. /// Gets a value indicating whether this instance is virtual folder.
  20. /// </summary>
  21. /// <value><c>true</c> if this instance is virtual folder; otherwise, <c>false</c>.</value>
  22. [IgnoreDataMember]
  23. public override bool IsVirtualFolder
  24. {
  25. get
  26. {
  27. return true;
  28. }
  29. }
  30. /// <summary>
  31. /// Allow different display preferences for each collection folder
  32. /// </summary>
  33. /// <value>The display prefs id.</value>
  34. protected override Guid DisplayPreferencesId
  35. {
  36. get
  37. {
  38. return Id;
  39. }
  40. }
  41. // Cache this since it will be used a lot
  42. /// <summary>
  43. /// The null task result
  44. /// </summary>
  45. private static readonly Task NullTaskResult = Task.FromResult<object>(null);
  46. /// <summary>
  47. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  48. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  49. /// </summary>
  50. /// <param name="progress">The progress.</param>
  51. /// <param name="cancellationToken">The cancellation token.</param>
  52. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  53. /// <param name="forceRefreshMetadata">if set to <c>true</c> [force refresh metadata].</param>
  54. /// <returns>Task.</returns>
  55. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null, bool forceRefreshMetadata = false)
  56. {
  57. //we don't directly validate our children
  58. //but we do need to clear out the index cache...
  59. IndexCache = new ConcurrentDictionary<string, List<BaseItem>>(StringComparer.OrdinalIgnoreCase);
  60. return NullTaskResult;
  61. }
  62. /// <summary>
  63. /// Our children are actually just references to the ones in the physical root...
  64. /// </summary>
  65. /// <value>The actual children.</value>
  66. protected override ConcurrentDictionary<Guid,BaseItem> ActualChildren
  67. {
  68. get
  69. {
  70. Dictionary<Guid,Guid> folderIds;
  71. try
  72. {
  73. // Accessing ResolveArgs could involve file system access
  74. folderIds = ResolveArgs.PhysicalLocations
  75. .Select(f => f.GetMBId(typeof(Folder)))
  76. .ToDictionary(i => i);
  77. }
  78. catch (IOException ex)
  79. {
  80. Logger.ErrorException("Error creating FolderIds for {0}", ex, Path);
  81. folderIds = new Dictionary<Guid, Guid>();
  82. }
  83. var ourChildren =
  84. LibraryManager.RootFolder.RecursiveChildren.OfType<Folder>()
  85. .Where(i => folderIds.ContainsKey(i.Id))
  86. .SelectMany(c => c.Children);
  87. return new ConcurrentDictionary<Guid,BaseItem>(ourChildren.ToDictionary(i => i.Id));
  88. }
  89. }
  90. }
  91. }