CollectionFolder.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using MediaBrowser.Controller.IO;
  2. using MediaBrowser.Controller.Library;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.Serialization;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.Entities
  11. {
  12. /// <summary>
  13. /// Specialized Folder class that points to a subset of the physical folders in the system.
  14. /// It is created from the user-specific folders within the system root
  15. /// </summary>
  16. public class CollectionFolder : Folder, ICollectionFolder
  17. {
  18. public CollectionFolder()
  19. {
  20. PhysicalLocationsList = new List<string>();
  21. }
  22. /// <summary>
  23. /// Gets a value indicating whether this instance is virtual folder.
  24. /// </summary>
  25. /// <value><c>true</c> if this instance is virtual folder; otherwise, <c>false</c>.</value>
  26. [IgnoreDataMember]
  27. public override bool IsVirtualFolder
  28. {
  29. get
  30. {
  31. return true;
  32. }
  33. }
  34. public string CollectionType { get; set; }
  35. /// <summary>
  36. /// Allow different display preferences for each collection folder
  37. /// </summary>
  38. /// <value>The display prefs id.</value>
  39. [IgnoreDataMember]
  40. public override Guid DisplayPreferencesId
  41. {
  42. get
  43. {
  44. return Id;
  45. }
  46. }
  47. [IgnoreDataMember]
  48. public override IEnumerable<string> PhysicalLocations
  49. {
  50. get
  51. {
  52. return PhysicalLocationsList;
  53. }
  54. }
  55. public List<string> PhysicalLocationsList { get; set; }
  56. protected override IEnumerable<FileSystemInfo> GetFileSystemChildren()
  57. {
  58. return CreateResolveArgs().FileSystemChildren;
  59. }
  60. private ItemResolveArgs CreateResolveArgs()
  61. {
  62. var path = ContainingFolderPath;
  63. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, LibraryManager)
  64. {
  65. FileInfo = new DirectoryInfo(path),
  66. Path = path,
  67. Parent = Parent
  68. };
  69. // Gather child folder and files
  70. if (args.IsDirectory)
  71. {
  72. var isPhysicalRoot = args.IsPhysicalRoot;
  73. // When resolving the root, we need it's grandchildren (children of user views)
  74. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  75. var fileSystemDictionary = FileData.GetFilteredFileSystemEntries(args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  76. // Need to remove subpaths that may have been resolved from shortcuts
  77. // Example: if \\server\movies exists, then strip out \\server\movies\action
  78. if (isPhysicalRoot)
  79. {
  80. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Keys);
  81. fileSystemDictionary = paths.Select(i => (FileSystemInfo)new DirectoryInfo(i)).ToDictionary(i => i.FullName);
  82. }
  83. args.FileSystemDictionary = fileSystemDictionary;
  84. }
  85. PhysicalLocationsList = args.PhysicalLocations.ToList();
  86. return args;
  87. }
  88. // Cache this since it will be used a lot
  89. /// <summary>
  90. /// The null task result
  91. /// </summary>
  92. private static readonly Task NullTaskResult = Task.FromResult<object>(null);
  93. /// <summary>
  94. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  95. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  96. /// </summary>
  97. /// <param name="progress">The progress.</param>
  98. /// <param name="cancellationToken">The cancellation token.</param>
  99. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  100. /// <param name="forceRefreshMetadata">if set to <c>true</c> [force refresh metadata].</param>
  101. /// <returns>Task.</returns>
  102. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null, bool forceRefreshMetadata = false)
  103. {
  104. CreateResolveArgs();
  105. ResetDynamicChildren();
  106. return NullTaskResult;
  107. }
  108. private List<LinkedChild> _linkedChildren;
  109. /// <summary>
  110. /// Our children are actually just references to the ones in the physical root...
  111. /// </summary>
  112. /// <value>The linked children.</value>
  113. public override List<LinkedChild> LinkedChildren
  114. {
  115. get { return _linkedChildren ?? (_linkedChildren = GetLinkedChildrenInternal()); }
  116. set
  117. {
  118. base.LinkedChildren = value;
  119. }
  120. }
  121. private List<LinkedChild> GetLinkedChildrenInternal()
  122. {
  123. Dictionary<string, string> locationsDicionary;
  124. try
  125. {
  126. locationsDicionary = PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  127. }
  128. catch (IOException ex)
  129. {
  130. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  131. return new List<LinkedChild>();
  132. }
  133. return LibraryManager.RootFolder.Children
  134. .OfType<Folder>()
  135. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  136. .SelectMany(c => c.LinkedChildren)
  137. .ToList();
  138. }
  139. private IEnumerable<BaseItem> _actualChildren;
  140. /// <summary>
  141. /// Our children are actually just references to the ones in the physical root...
  142. /// </summary>
  143. /// <value>The actual children.</value>
  144. protected override IEnumerable<BaseItem> ActualChildren
  145. {
  146. get { return _actualChildren ?? (_actualChildren = GetActualChildren()); }
  147. }
  148. private IEnumerable<BaseItem> GetActualChildren()
  149. {
  150. Dictionary<string, string> locationsDicionary;
  151. try
  152. {
  153. locationsDicionary = PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  154. }
  155. catch (IOException ex)
  156. {
  157. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  158. return new BaseItem[] { };
  159. }
  160. return
  161. LibraryManager.RootFolder.Children
  162. .OfType<Folder>()
  163. .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path))
  164. .SelectMany(c => c.Children)
  165. .ToList();
  166. }
  167. public void ResetDynamicChildren()
  168. {
  169. _actualChildren = null;
  170. _linkedChildren = null;
  171. }
  172. }
  173. }