AggregateFolder.cs 7.2 KB

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