CollectionFolder.cs 7.2 KB

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