AggregateFolder.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = EmptyStringArray;
  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()
  104. {
  105. ClearCache();
  106. var changed = base.BeforeMetadataRefresh() || _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. Parent = GetParent() as Folder
  119. };
  120. // Gather child folder and files
  121. if (args.IsDirectory)
  122. {
  123. var isPhysicalRoot = args.IsPhysicalRoot;
  124. // When resolving the root, we need it's grandchildren (children of user views)
  125. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  126. var files = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  127. // Need to remove subpaths that may have been resolved from shortcuts
  128. // Example: if \\server\movies exists, then strip out \\server\movies\action
  129. if (isPhysicalRoot)
  130. {
  131. files = LibraryManager.NormalizeRootPathList(files).ToArray();
  132. }
  133. args.FileSystemChildren = files;
  134. }
  135. _requiresRefresh = _requiresRefresh || !args.PhysicalLocations.SequenceEqual(PhysicalLocations);
  136. if (setPhysicalLocations)
  137. {
  138. PhysicalLocationsList = args.PhysicalLocations;
  139. }
  140. return args;
  141. }
  142. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  143. {
  144. return base.GetNonCachedChildren(directoryService).Concat(_virtualChildren);
  145. }
  146. protected override async Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  147. {
  148. ClearCache();
  149. await base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService)
  150. .ConfigureAwait(false);
  151. ClearCache();
  152. }
  153. /// <summary>
  154. /// Adds the virtual child.
  155. /// </summary>
  156. /// <param name="child">The child.</param>
  157. /// <exception cref="System.ArgumentNullException"></exception>
  158. public void AddVirtualChild(BaseItem child)
  159. {
  160. if (child == null)
  161. {
  162. throw new ArgumentNullException();
  163. }
  164. _virtualChildren.Add(child);
  165. }
  166. /// <summary>
  167. /// Finds the virtual child.
  168. /// </summary>
  169. /// <param name="id">The id.</param>
  170. /// <returns>BaseItem.</returns>
  171. /// <exception cref="System.ArgumentNullException">id</exception>
  172. public BaseItem FindVirtualChild(Guid id)
  173. {
  174. if (id == Guid.Empty)
  175. {
  176. throw new ArgumentNullException("id");
  177. }
  178. foreach (var child in _virtualChildren)
  179. {
  180. if (child.Id == id)
  181. {
  182. return child;
  183. }
  184. }
  185. return null;
  186. }
  187. }
  188. }