AggregateFolder.cs 4.7 KB

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