CollectionFolder.cs 6.5 KB

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