CollectionFolder.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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).FileSystemChildren;
  77. }
  78. internal override bool IsValidFromResolver(BaseItem newItem)
  79. {
  80. var newCollectionFolder = newItem as CollectionFolder;
  81. if (newCollectionFolder != null)
  82. {
  83. if (!string.Equals(CollectionType, newCollectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  84. {
  85. return false;
  86. }
  87. }
  88. return base.IsValidFromResolver(newItem);
  89. }
  90. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService)
  91. {
  92. var path = ContainingFolderPath;
  93. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
  94. {
  95. FileInfo = FileSystem.GetDirectoryInfo(path),
  96. Path = path,
  97. Parent = Parent,
  98. CollectionType = CollectionType
  99. };
  100. // Gather child folder and files
  101. if (args.IsDirectory)
  102. {
  103. var isPhysicalRoot = args.IsPhysicalRoot;
  104. // When resolving the root, we need it's grandchildren (children of user views)
  105. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  106. var fileSystemDictionary = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  107. // Need to remove subpaths that may have been resolved from shortcuts
  108. // Example: if \\server\movies exists, then strip out \\server\movies\action
  109. if (isPhysicalRoot)
  110. {
  111. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Values);
  112. fileSystemDictionary = paths.ToDictionary(i => i.FullName);
  113. }
  114. args.FileSystemDictionary = fileSystemDictionary;
  115. }
  116. PhysicalLocationsList = args.PhysicalLocations.ToList();
  117. return args;
  118. }
  119. /// <summary>
  120. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  121. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  122. /// </summary>
  123. /// <param name="progress">The progress.</param>
  124. /// <param name="cancellationToken">The cancellation token.</param>
  125. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  126. /// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param>
  127. /// <param name="refreshOptions">The refresh options.</param>
  128. /// <param name="directoryService">The directory service.</param>
  129. /// <returns>Task.</returns>
  130. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  131. {
  132. var list = PhysicalLocationsList.ToList();
  133. CreateResolveArgs(directoryService);
  134. if (!list.SequenceEqual(PhysicalLocationsList))
  135. {
  136. return UpdateToRepository(ItemUpdateType.MetadataImport, cancellationToken);
  137. }
  138. return Task.FromResult(true);
  139. }
  140. /// <summary>
  141. /// Our children are actually just references to the ones in the physical root...
  142. /// </summary>
  143. /// <value>The linked children.</value>
  144. public override List<LinkedChild> LinkedChildren
  145. {
  146. get { return GetLinkedChildrenInternal(); }
  147. set
  148. {
  149. base.LinkedChildren = value;
  150. }
  151. }
  152. private List<LinkedChild> GetLinkedChildrenInternal()
  153. {
  154. return GetPhysicalParents()
  155. .SelectMany(c => c.LinkedChildren)
  156. .ToList();
  157. }
  158. /// <summary>
  159. /// Our children are actually just references to the ones in the physical root...
  160. /// </summary>
  161. /// <value>The actual children.</value>
  162. [IgnoreDataMember]
  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. var rootChildren = LibraryManager.RootFolder.Children
  174. .OfType<Folder>()
  175. .ToList();
  176. return PhysicalLocations.Where(i => !string.Equals(i, Path, StringComparison.OrdinalIgnoreCase)).SelectMany(i => GetPhysicalParents(i, rootChildren)).DistinctBy(i => i.Id);
  177. }
  178. private IEnumerable<Folder> GetPhysicalParents(string path, List<Folder> rootChildren)
  179. {
  180. var result = rootChildren
  181. .Where(i => string.Equals(i.Path, path, StringComparison.OrdinalIgnoreCase))
  182. .ToList();
  183. if (result.Count == 0)
  184. {
  185. var folder = LibraryManager.FindByPath(path, true) as Folder;
  186. if (folder != null)
  187. {
  188. result.Add(folder);
  189. }
  190. }
  191. return result;
  192. }
  193. [IgnoreDataMember]
  194. public override bool SupportsPeople
  195. {
  196. get
  197. {
  198. return false;
  199. }
  200. }
  201. }
  202. }