AggregateFolder.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.Json.Serialization;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Controller.IO;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Model.IO;
  13. namespace MediaBrowser.Controller.Entities
  14. {
  15. /// <summary>
  16. /// Specialized folder that can have items added to it's children by external entities.
  17. /// Used for our RootFolder so plug-ins can add items.
  18. /// </summary>
  19. public class AggregateFolder : Folder
  20. {
  21. public AggregateFolder()
  22. {
  23. PhysicalLocationsList = Array.Empty<string>();
  24. }
  25. [JsonIgnore]
  26. public override bool IsPhysicalRoot => true;
  27. public override bool CanDelete()
  28. {
  29. return false;
  30. }
  31. [JsonIgnore]
  32. public override bool SupportsPlayedStatus => false;
  33. /// <summary>
  34. /// The _virtual children.
  35. /// </summary>
  36. private readonly ConcurrentBag<BaseItem> _virtualChildren = new ConcurrentBag<BaseItem>();
  37. /// <summary>
  38. /// Gets the virtual children.
  39. /// </summary>
  40. /// <value>The virtual children.</value>
  41. public ConcurrentBag<BaseItem> VirtualChildren => _virtualChildren;
  42. [JsonIgnore]
  43. public override string[] PhysicalLocations => PhysicalLocationsList;
  44. public string[] PhysicalLocationsList { get; set; }
  45. protected override FileSystemMetadata[] GetFileSystemChildren(IDirectoryService directoryService)
  46. {
  47. return CreateResolveArgs(directoryService, true).FileSystemChildren;
  48. }
  49. private Guid[] _childrenIds = null;
  50. private readonly object _childIdsLock = new object();
  51. protected override List<BaseItem> LoadChildren()
  52. {
  53. lock (_childIdsLock)
  54. {
  55. if (_childrenIds == null || _childrenIds.Length == 0)
  56. {
  57. var list = base.LoadChildren();
  58. _childrenIds = list.Select(i => i.Id).ToArray();
  59. return list;
  60. }
  61. return _childrenIds.Select(LibraryManager.GetItemById).Where(i => i != null).ToList();
  62. }
  63. }
  64. private void ClearCache()
  65. {
  66. lock (_childIdsLock)
  67. {
  68. _childrenIds = null;
  69. }
  70. }
  71. private bool _requiresRefresh;
  72. public override bool RequiresRefresh()
  73. {
  74. var changed = base.RequiresRefresh() || _requiresRefresh;
  75. if (!changed)
  76. {
  77. var locations = PhysicalLocations;
  78. var newLocations = CreateResolveArgs(new DirectoryService(FileSystem), false).PhysicalLocations;
  79. if (!locations.SequenceEqual(newLocations))
  80. {
  81. changed = true;
  82. }
  83. }
  84. return changed;
  85. }
  86. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  87. {
  88. ClearCache();
  89. var changed = base.BeforeMetadataRefresh(replaceAllMetdata) || _requiresRefresh;
  90. _requiresRefresh = false;
  91. return changed;
  92. }
  93. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService, bool setPhysicalLocations)
  94. {
  95. ClearCache();
  96. var path = ContainingFolderPath;
  97. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
  98. {
  99. FileInfo = FileSystem.GetDirectoryInfo(path)
  100. };
  101. // Gather child folder and files
  102. if (args.IsDirectory)
  103. {
  104. // When resolving the root, we need it's grandchildren (children of user views)
  105. var flattenFolderDepth = 2;
  106. var files = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, CollectionFolder.ApplicationHost, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: true);
  107. // Need to remove subpaths that may have been resolved from shortcuts
  108. // Example: if \\server\movies exists, then strip out \\server\movies\action
  109. files = LibraryManager.NormalizeRootPathList(files).ToArray();
  110. args.FileSystemChildren = files;
  111. }
  112. _requiresRefresh = _requiresRefresh || !args.PhysicalLocations.SequenceEqual(PhysicalLocations);
  113. if (setPhysicalLocations)
  114. {
  115. PhysicalLocationsList = args.PhysicalLocations;
  116. }
  117. return args;
  118. }
  119. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  120. {
  121. return base.GetNonCachedChildren(directoryService).Concat(_virtualChildren);
  122. }
  123. protected override async Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  124. {
  125. ClearCache();
  126. await base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService)
  127. .ConfigureAwait(false);
  128. ClearCache();
  129. }
  130. /// <summary>
  131. /// Adds the virtual child.
  132. /// </summary>
  133. /// <param name="child">The child.</param>
  134. /// <exception cref="ArgumentNullException"></exception>
  135. public void AddVirtualChild(BaseItem child)
  136. {
  137. if (child == null)
  138. {
  139. throw new ArgumentNullException(nameof(child));
  140. }
  141. _virtualChildren.Add(child);
  142. }
  143. /// <summary>
  144. /// Finds the virtual child.
  145. /// </summary>
  146. /// <param name="id">The id.</param>
  147. /// <returns>BaseItem.</returns>
  148. /// <exception cref="ArgumentNullException">id</exception>
  149. public BaseItem FindVirtualChild(Guid id)
  150. {
  151. if (id.Equals(Guid.Empty))
  152. {
  153. throw new ArgumentNullException(nameof(id));
  154. }
  155. foreach (var child in _virtualChildren)
  156. {
  157. if (child.Id == id)
  158. {
  159. return child;
  160. }
  161. }
  162. return null;
  163. }
  164. }
  165. }