CollectionFolder.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. [IgnoreDataMember]
  36. protected override bool SupportsShortcutChildren
  37. {
  38. get
  39. {
  40. return true;
  41. }
  42. }
  43. public override bool CanDelete()
  44. {
  45. return false;
  46. }
  47. public string CollectionType { get; set; }
  48. /// <summary>
  49. /// Allow different display preferences for each collection folder
  50. /// </summary>
  51. /// <value>The display prefs id.</value>
  52. [IgnoreDataMember]
  53. public override Guid DisplayPreferencesId
  54. {
  55. get
  56. {
  57. return Id;
  58. }
  59. }
  60. [IgnoreDataMember]
  61. public override IEnumerable<string> PhysicalLocations
  62. {
  63. get
  64. {
  65. return PhysicalLocationsList;
  66. }
  67. }
  68. public override bool IsSaveLocalMetadataEnabled()
  69. {
  70. return true;
  71. }
  72. public List<string> PhysicalLocationsList { get; set; }
  73. protected override IEnumerable<FileSystemInfo> GetFileSystemChildren(IDirectoryService directoryService)
  74. {
  75. return CreateResolveArgs(directoryService).FileSystemChildren;
  76. }
  77. internal override bool IsValidFromResolver(BaseItem newItem)
  78. {
  79. var newCollectionFolder = newItem as CollectionFolder;
  80. if (newCollectionFolder != null)
  81. {
  82. if (!string.Equals(CollectionType, newCollectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  83. {
  84. return false;
  85. }
  86. }
  87. return base.IsValidFromResolver(newItem);
  88. }
  89. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService)
  90. {
  91. var path = ContainingFolderPath;
  92. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
  93. {
  94. FileInfo = new DirectoryInfo(path),
  95. Path = path,
  96. Parent = Parent,
  97. CollectionType = CollectionType
  98. };
  99. // Gather child folder and files
  100. if (args.IsDirectory)
  101. {
  102. var isPhysicalRoot = args.IsPhysicalRoot;
  103. // When resolving the root, we need it's grandchildren (children of user views)
  104. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  105. var fileSystemDictionary = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  106. // Need to remove subpaths that may have been resolved from shortcuts
  107. // Example: if \\server\movies exists, then strip out \\server\movies\action
  108. if (isPhysicalRoot)
  109. {
  110. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Keys);
  111. fileSystemDictionary = paths.Select(i => (FileSystemInfo)new DirectoryInfo(i)).ToDictionary(i => i.FullName);
  112. }
  113. args.FileSystemDictionary = fileSystemDictionary;
  114. }
  115. PhysicalLocationsList = args.PhysicalLocations.ToList();
  116. return args;
  117. }
  118. /// <summary>
  119. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  120. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  121. /// </summary>
  122. /// <param name="progress">The progress.</param>
  123. /// <param name="cancellationToken">The cancellation token.</param>
  124. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  125. /// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param>
  126. /// <param name="refreshOptions">The refresh options.</param>
  127. /// <param name="directoryService">The directory service.</param>
  128. /// <returns>Task.</returns>
  129. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  130. {
  131. var list = PhysicalLocationsList.ToList();
  132. CreateResolveArgs(directoryService);
  133. if (!list.SequenceEqual(PhysicalLocationsList))
  134. {
  135. return UpdateToRepository(ItemUpdateType.MetadataImport, cancellationToken);
  136. }
  137. return Task.FromResult(true);
  138. }
  139. /// <summary>
  140. /// Our children are actually just references to the ones in the physical root...
  141. /// </summary>
  142. /// <value>The linked children.</value>
  143. public override List<LinkedChild> LinkedChildren
  144. {
  145. get { return GetLinkedChildrenInternal(); }
  146. set
  147. {
  148. base.LinkedChildren = value;
  149. }
  150. }
  151. private List<LinkedChild> GetLinkedChildrenInternal()
  152. {
  153. return LibraryManager.RootFolder.Children
  154. .OfType<Folder>()
  155. .Where(i => i.Path != null && PhysicalLocations.Contains(i.Path, StringComparer.OrdinalIgnoreCase))
  156. .SelectMany(c => c.LinkedChildren)
  157. .ToList();
  158. }
  159. /// <summary>
  160. /// Our children are actually just references to the ones in the physical root...
  161. /// </summary>
  162. /// <value>The actual children.</value>
  163. protected override IEnumerable<BaseItem> ActualChildren
  164. {
  165. get { return GetActualChildren(); }
  166. }
  167. private IEnumerable<BaseItem> GetActualChildren()
  168. {
  169. return
  170. LibraryManager.RootFolder.Children
  171. .OfType<Folder>()
  172. .Where(i => i.Path != null && PhysicalLocations.Contains(i.Path, StringComparer.OrdinalIgnoreCase))
  173. .SelectMany(c => c.Children);
  174. }
  175. [IgnoreDataMember]
  176. public override bool SupportsPeople
  177. {
  178. get
  179. {
  180. return false;
  181. }
  182. }
  183. }
  184. }