CollectionFolder.cs 8.6 KB

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