AggregateFolder.cs 6.5 KB

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