AggregateFolder.cs 7.0 KB

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