AggregateFolder.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 bool _requiresRefresh;
  62. public override bool RequiresRefresh()
  63. {
  64. var changed = base.RequiresRefresh() || _requiresRefresh;
  65. if (!changed)
  66. {
  67. var locations = PhysicalLocations.ToList();
  68. var newLocations = CreateResolveArgs(new DirectoryService(BaseItem.FileSystem), false).PhysicalLocations.ToList();
  69. if (!locations.SequenceEqual(newLocations))
  70. {
  71. changed = true;
  72. }
  73. }
  74. return changed;
  75. }
  76. public override bool BeforeMetadataRefresh()
  77. {
  78. var changed = base.BeforeMetadataRefresh() || _requiresRefresh;
  79. _requiresRefresh = false;
  80. return changed;
  81. }
  82. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService, bool setPhysicalLocations)
  83. {
  84. var path = ContainingFolderPath;
  85. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths , directoryService)
  86. {
  87. FileInfo = FileSystem.GetDirectoryInfo(path),
  88. Path = path,
  89. Parent = Parent
  90. };
  91. // Gather child folder and files
  92. if (args.IsDirectory)
  93. {
  94. var isPhysicalRoot = args.IsPhysicalRoot;
  95. // When resolving the root, we need it's grandchildren (children of user views)
  96. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  97. var fileSystemDictionary = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  98. // Need to remove subpaths that may have been resolved from shortcuts
  99. // Example: if \\server\movies exists, then strip out \\server\movies\action
  100. if (isPhysicalRoot)
  101. {
  102. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Values);
  103. fileSystemDictionary = paths.ToDictionary(i => i.FullName);
  104. }
  105. args.FileSystemDictionary = fileSystemDictionary;
  106. }
  107. _requiresRefresh = _requiresRefresh || !args.PhysicalLocations.SequenceEqual(PhysicalLocations);
  108. if (setPhysicalLocations)
  109. {
  110. PhysicalLocationsList = args.PhysicalLocations.ToList();
  111. }
  112. return args;
  113. }
  114. /// <summary>
  115. /// Adds the virtual child.
  116. /// </summary>
  117. /// <param name="child">The child.</param>
  118. /// <exception cref="System.ArgumentNullException"></exception>
  119. public void AddVirtualChild(BaseItem child)
  120. {
  121. if (child == null)
  122. {
  123. throw new ArgumentNullException();
  124. }
  125. _virtualChildren.Add(child);
  126. }
  127. /// <summary>
  128. /// Get the children of this folder from the actual file system
  129. /// </summary>
  130. /// <returns>IEnumerable{BaseItem}.</returns>
  131. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  132. {
  133. return base.GetNonCachedChildren(directoryService).Concat(_virtualChildren);
  134. }
  135. /// <summary>
  136. /// Finds the virtual child.
  137. /// </summary>
  138. /// <param name="id">The id.</param>
  139. /// <returns>BaseItem.</returns>
  140. /// <exception cref="System.ArgumentNullException">id</exception>
  141. public BaseItem FindVirtualChild(Guid id)
  142. {
  143. if (id == Guid.Empty)
  144. {
  145. throw new ArgumentNullException("id");
  146. }
  147. return _virtualChildren.FirstOrDefault(i => i.Id == id);
  148. }
  149. }
  150. }