AggregateFolder.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. namespace MediaBrowser.Controller.Entities
  10. {
  11. /// <summary>
  12. /// Specialized folder that can have items added to it's children by external entities.
  13. /// Used for our RootFolder so plug-ins can add items.
  14. /// </summary>
  15. public class AggregateFolder : Folder
  16. {
  17. public AggregateFolder()
  18. {
  19. PhysicalLocationsList = new List<string>();
  20. }
  21. /// <summary>
  22. /// We don't support manual shortcuts
  23. /// </summary>
  24. protected override bool SupportsShortcutChildren
  25. {
  26. get
  27. {
  28. return false;
  29. }
  30. }
  31. /// <summary>
  32. /// The _virtual children
  33. /// </summary>
  34. private readonly ConcurrentBag<BaseItem> _virtualChildren = new ConcurrentBag<BaseItem>();
  35. /// <summary>
  36. /// Gets the virtual children.
  37. /// </summary>
  38. /// <value>The virtual children.</value>
  39. public ConcurrentBag<BaseItem> VirtualChildren
  40. {
  41. get { return _virtualChildren; }
  42. }
  43. [IgnoreDataMember]
  44. public override IEnumerable<string> PhysicalLocations
  45. {
  46. get
  47. {
  48. return PhysicalLocationsList;
  49. }
  50. }
  51. public List<string> PhysicalLocationsList { get; set; }
  52. protected override IEnumerable<FileSystemInfo> GetFileSystemChildren()
  53. {
  54. return CreateResolveArgs().FileSystemChildren;
  55. }
  56. private ItemResolveArgs CreateResolveArgs()
  57. {
  58. var path = ContainingFolderPath;
  59. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, LibraryManager)
  60. {
  61. FileInfo = new DirectoryInfo(path),
  62. Path = path,
  63. Parent = Parent
  64. };
  65. // Gather child folder and files
  66. if (args.IsDirectory)
  67. {
  68. var isPhysicalRoot = args.IsPhysicalRoot;
  69. // When resolving the root, we need it's grandchildren (children of user views)
  70. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  71. var fileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  72. // Need to remove subpaths that may have been resolved from shortcuts
  73. // Example: if \\server\movies exists, then strip out \\server\movies\action
  74. if (isPhysicalRoot)
  75. {
  76. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Keys);
  77. fileSystemDictionary = paths.Select(i => (FileSystemInfo)new DirectoryInfo(i)).ToDictionary(i => i.FullName);
  78. }
  79. args.FileSystemDictionary = fileSystemDictionary;
  80. }
  81. PhysicalLocationsList = args.PhysicalLocations.ToList();
  82. return args;
  83. }
  84. /// <summary>
  85. /// Adds the virtual child.
  86. /// </summary>
  87. /// <param name="child">The child.</param>
  88. /// <exception cref="System.ArgumentNullException"></exception>
  89. public void AddVirtualChild(BaseItem child)
  90. {
  91. if (child == null)
  92. {
  93. throw new ArgumentNullException();
  94. }
  95. _virtualChildren.Add(child);
  96. }
  97. /// <summary>
  98. /// Get the children of this folder from the actual file system
  99. /// </summary>
  100. /// <returns>IEnumerable{BaseItem}.</returns>
  101. protected override IEnumerable<BaseItem> GetNonCachedChildren()
  102. {
  103. return base.GetNonCachedChildren().Concat(_virtualChildren);
  104. }
  105. /// <summary>
  106. /// Finds the virtual child.
  107. /// </summary>
  108. /// <param name="id">The id.</param>
  109. /// <returns>BaseItem.</returns>
  110. /// <exception cref="System.ArgumentNullException">id</exception>
  111. public BaseItem FindVirtualChild(Guid id)
  112. {
  113. if (id == Guid.Empty)
  114. {
  115. throw new ArgumentNullException("id");
  116. }
  117. return _virtualChildren.FirstOrDefault(i => i.Id == id);
  118. }
  119. }
  120. }