AggregateFolder.cs 6.7 KB

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