AggregateFolder.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.IO;
  7. using System.Linq;
  8. using System.Runtime.Serialization;
  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. /// <summary>
  33. /// The _virtual children
  34. /// </summary>
  35. private readonly ConcurrentBag<BaseItem> _virtualChildren = new ConcurrentBag<BaseItem>();
  36. /// <summary>
  37. /// Gets the virtual children.
  38. /// </summary>
  39. /// <value>The virtual children.</value>
  40. public ConcurrentBag<BaseItem> VirtualChildren
  41. {
  42. get { return _virtualChildren; }
  43. }
  44. [IgnoreDataMember]
  45. public override IEnumerable<string> PhysicalLocations
  46. {
  47. get
  48. {
  49. return PhysicalLocationsList;
  50. }
  51. }
  52. public List<string> PhysicalLocationsList { get; set; }
  53. protected override IEnumerable<FileSystemInfo> GetFileSystemChildren(IDirectoryService directoryService)
  54. {
  55. return CreateResolveArgs(directoryService).FileSystemChildren;
  56. }
  57. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService)
  58. {
  59. var path = ContainingFolderPath;
  60. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, LibraryManager, directoryService)
  61. {
  62. FileInfo = new DirectoryInfo(path),
  63. Path = path,
  64. Parent = Parent
  65. };
  66. // Gather child folder and files
  67. if (args.IsDirectory)
  68. {
  69. var isPhysicalRoot = args.IsPhysicalRoot;
  70. // When resolving the root, we need it's grandchildren (children of user views)
  71. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  72. var fileSystemDictionary = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  73. // Need to remove subpaths that may have been resolved from shortcuts
  74. // Example: if \\server\movies exists, then strip out \\server\movies\action
  75. if (isPhysicalRoot)
  76. {
  77. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Keys);
  78. fileSystemDictionary = paths.Select(i => (FileSystemInfo)new DirectoryInfo(i)).ToDictionary(i => i.FullName);
  79. }
  80. args.FileSystemDictionary = fileSystemDictionary;
  81. }
  82. PhysicalLocationsList = args.PhysicalLocations.ToList();
  83. return args;
  84. }
  85. /// <summary>
  86. /// Adds the virtual child.
  87. /// </summary>
  88. /// <param name="child">The child.</param>
  89. /// <exception cref="System.ArgumentNullException"></exception>
  90. public void AddVirtualChild(BaseItem child)
  91. {
  92. if (child == null)
  93. {
  94. throw new ArgumentNullException();
  95. }
  96. _virtualChildren.Add(child);
  97. }
  98. /// <summary>
  99. /// Get the children of this folder from the actual file system
  100. /// </summary>
  101. /// <returns>IEnumerable{BaseItem}.</returns>
  102. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  103. {
  104. return base.GetNonCachedChildren(directoryService).Concat(_virtualChildren);
  105. }
  106. /// <summary>
  107. /// Finds the virtual child.
  108. /// </summary>
  109. /// <param name="id">The id.</param>
  110. /// <returns>BaseItem.</returns>
  111. /// <exception cref="System.ArgumentNullException">id</exception>
  112. public BaseItem FindVirtualChild(Guid id)
  113. {
  114. if (id == Guid.Empty)
  115. {
  116. throw new ArgumentNullException("id");
  117. }
  118. return _virtualChildren.FirstOrDefault(i => i.Id == id);
  119. }
  120. }
  121. }