2
0

AggregateFolder.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.Json.Serialization;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Controller.IO;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.IO;
  12. namespace MediaBrowser.Controller.Entities
  13. {
  14. /// <summary>
  15. /// Specialized folder that can have items added to it's children by external entities.
  16. /// Used for our RootFolder so plug-ins can add items.
  17. /// </summary>
  18. public class AggregateFolder : Folder
  19. {
  20. public AggregateFolder()
  21. {
  22. PhysicalLocationsList = Array.Empty<string>();
  23. }
  24. [JsonIgnore]
  25. public override bool IsPhysicalRoot => true;
  26. public override bool CanDelete()
  27. {
  28. return false;
  29. }
  30. [JsonIgnore]
  31. public override bool SupportsPlayedStatus => false;
  32. /// <summary>
  33. /// The _virtual children.
  34. /// </summary>
  35. private readonly ConcurrentBag<BaseItem> _virtualChildren = new ConcurrentBag<BaseItem>();
  36. /// <summary>
  37. /// Gets the virtual children.
  38. /// </summary>
  39. /// <value>The virtual children.</value>
  40. public ConcurrentBag<BaseItem> VirtualChildren => _virtualChildren;
  41. [JsonIgnore]
  42. public override string[] PhysicalLocations => PhysicalLocationsList;
  43. public string[] PhysicalLocationsList { get; set; }
  44. protected override FileSystemMetadata[] GetFileSystemChildren(IDirectoryService directoryService)
  45. {
  46. return CreateResolveArgs(directoryService, true).FileSystemChildren;
  47. }
  48. private Guid[] _childrenIds = null;
  49. private readonly object _childIdsLock = new object();
  50. protected override List<BaseItem> LoadChildren()
  51. {
  52. lock (_childIdsLock)
  53. {
  54. if (_childrenIds == null || _childrenIds.Length == 0)
  55. {
  56. var list = base.LoadChildren();
  57. _childrenIds = list.Select(i => i.Id).ToArray();
  58. return list;
  59. }
  60. return _childrenIds.Select(LibraryManager.GetItemById).Where(i => i != null).ToList();
  61. }
  62. }
  63. private void ClearCache()
  64. {
  65. lock (_childIdsLock)
  66. {
  67. _childrenIds = null;
  68. }
  69. }
  70. private bool _requiresRefresh;
  71. public override bool RequiresRefresh()
  72. {
  73. var changed = base.RequiresRefresh() || _requiresRefresh;
  74. if (!changed)
  75. {
  76. var locations = PhysicalLocations;
  77. var newLocations = CreateResolveArgs(new DirectoryService(FileSystem), false).PhysicalLocations;
  78. if (!locations.SequenceEqual(newLocations))
  79. {
  80. changed = true;
  81. }
  82. }
  83. return changed;
  84. }
  85. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  86. {
  87. ClearCache();
  88. var changed = base.BeforeMetadataRefresh(replaceAllMetdata) || _requiresRefresh;
  89. _requiresRefresh = false;
  90. return changed;
  91. }
  92. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService, bool setPhysicalLocations)
  93. {
  94. ClearCache();
  95. var path = ContainingFolderPath;
  96. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
  97. {
  98. FileInfo = FileSystem.GetDirectoryInfo(path),
  99. Path = 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. }