AggregateFolder.cs 7.1 KB

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