AggregateFolder.cs 4.7 KB

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