CollectionFolder.cs 8.2 KB

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