AggregateFolder.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 = new List<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 IEnumerable<string> PhysicalLocations
  55. {
  56. get
  57. {
  58. return PhysicalLocationsList;
  59. }
  60. }
  61. public List<string> PhysicalLocationsList { get; set; }
  62. protected override FileSystemMetadata[] GetFileSystemChildren(IDirectoryService directoryService)
  63. {
  64. return CreateResolveArgs(directoryService, true).FileSystemChildren.ToArray();
  65. }
  66. private List<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.Count == 0)
  73. {
  74. var list = base.LoadChildren().ToList();
  75. _childrenIds = list.Select(i => i.Id).ToList();
  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.ToList();
  95. var newLocations = CreateResolveArgs(new DirectoryService(Logger, FileSystem), false).PhysicalLocations.ToList();
  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 = Parent
  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 fileSystemDictionary = 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. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Values);
  132. fileSystemDictionary = paths.ToDictionary(i => i.FullName);
  133. }
  134. args.FileSystemDictionary = fileSystemDictionary;
  135. }
  136. _requiresRefresh = _requiresRefresh || !args.PhysicalLocations.SequenceEqual(PhysicalLocations);
  137. if (setPhysicalLocations)
  138. {
  139. PhysicalLocationsList = args.PhysicalLocations.ToList();
  140. }
  141. return args;
  142. }
  143. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  144. {
  145. return base.GetNonCachedChildren(directoryService).Concat(_virtualChildren);
  146. }
  147. protected override async Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  148. {
  149. ClearCache();
  150. await base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService)
  151. .ConfigureAwait(false);
  152. ClearCache();
  153. }
  154. /// <summary>
  155. /// Adds the virtual child.
  156. /// </summary>
  157. /// <param name="child">The child.</param>
  158. /// <exception cref="System.ArgumentNullException"></exception>
  159. public void AddVirtualChild(BaseItem child)
  160. {
  161. if (child == null)
  162. {
  163. throw new ArgumentNullException();
  164. }
  165. _virtualChildren.Add(child);
  166. }
  167. /// <summary>
  168. /// Finds the virtual child.
  169. /// </summary>
  170. /// <param name="id">The id.</param>
  171. /// <returns>BaseItem.</returns>
  172. /// <exception cref="System.ArgumentNullException">id</exception>
  173. public BaseItem FindVirtualChild(Guid id)
  174. {
  175. if (id == Guid.Empty)
  176. {
  177. throw new ArgumentNullException("id");
  178. }
  179. return _virtualChildren.FirstOrDefault(i => i.Id == id);
  180. }
  181. }
  182. }