AggregateFolder.cs 6.5 KB

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