AggregateFolder.cs 6.9 KB

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