CollectionFolder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.Threading;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Model.Configuration;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Extensions;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Serialization;
  16. namespace MediaBrowser.Controller.Entities
  17. {
  18. /// <summary>
  19. /// Specialized Folder class that points to a subset of the physical folders in the system.
  20. /// It is created from the user-specific folders within the system root
  21. /// </summary>
  22. public class CollectionFolder : Folder, ICollectionFolder
  23. {
  24. public static IXmlSerializer XmlSerializer { get; set; }
  25. public CollectionFolder()
  26. {
  27. PhysicalLocationsList = new List<string>();
  28. PhysicalFolderIds = new List<Guid>();
  29. }
  30. [IgnoreDataMember]
  31. protected override bool SupportsShortcutChildren
  32. {
  33. get
  34. {
  35. return true;
  36. }
  37. }
  38. [IgnoreDataMember]
  39. public override bool SupportsPlayedStatus
  40. {
  41. get
  42. {
  43. return false;
  44. }
  45. }
  46. [IgnoreDataMember]
  47. public override bool SupportsInheritedParentImages
  48. {
  49. get
  50. {
  51. return false;
  52. }
  53. }
  54. public override bool CanDelete()
  55. {
  56. return false;
  57. }
  58. public string CollectionType { get; set; }
  59. private static readonly Dictionary<string, LibraryOptions> LibraryOptions = new Dictionary<string, LibraryOptions>();
  60. public LibraryOptions GetLibraryOptions()
  61. {
  62. return GetLibraryOptions(Path);
  63. }
  64. private static LibraryOptions LoadLibraryOptions(string path)
  65. {
  66. try
  67. {
  68. var result = XmlSerializer.DeserializeFromFile(typeof(LibraryOptions), GetLibraryOptionsPath(path)) as LibraryOptions;
  69. if (result == null)
  70. {
  71. return new LibraryOptions();
  72. }
  73. return result;
  74. }
  75. catch (FileNotFoundException)
  76. {
  77. return new LibraryOptions();
  78. }
  79. catch (IOException)
  80. {
  81. return new LibraryOptions();
  82. }
  83. catch (Exception ex)
  84. {
  85. Logger.ErrorException("Error loading library options", ex);
  86. return new LibraryOptions();
  87. }
  88. }
  89. private static string GetLibraryOptionsPath(string path)
  90. {
  91. return System.IO.Path.Combine(path, "options.xml");
  92. }
  93. public void UpdateLibraryOptions(LibraryOptions options)
  94. {
  95. SaveLibraryOptions(Path, options);
  96. }
  97. public static LibraryOptions GetLibraryOptions(string path)
  98. {
  99. lock (LibraryOptions)
  100. {
  101. LibraryOptions options;
  102. if (!LibraryOptions.TryGetValue(path, out options))
  103. {
  104. options = LoadLibraryOptions(path);
  105. LibraryOptions[path] = options;
  106. }
  107. return options;
  108. }
  109. }
  110. public static void SaveLibraryOptions(string path, LibraryOptions options)
  111. {
  112. lock (LibraryOptions)
  113. {
  114. LibraryOptions[path] = options;
  115. XmlSerializer.SerializeToFile(options, GetLibraryOptionsPath(path));
  116. }
  117. }
  118. /// <summary>
  119. /// Allow different display preferences for each collection folder
  120. /// </summary>
  121. /// <value>The display prefs id.</value>
  122. [IgnoreDataMember]
  123. public override Guid DisplayPreferencesId
  124. {
  125. get
  126. {
  127. return Id;
  128. }
  129. }
  130. [IgnoreDataMember]
  131. public override IEnumerable<string> PhysicalLocations
  132. {
  133. get
  134. {
  135. return PhysicalLocationsList;
  136. }
  137. }
  138. public override bool IsSaveLocalMetadataEnabled()
  139. {
  140. return true;
  141. }
  142. public List<string> PhysicalLocationsList { get; set; }
  143. public List<Guid> PhysicalFolderIds { get; set; }
  144. protected override IEnumerable<FileSystemMetadata> GetFileSystemChildren(IDirectoryService directoryService)
  145. {
  146. return CreateResolveArgs(directoryService, true).FileSystemChildren;
  147. }
  148. private bool _requiresRefresh;
  149. public override bool RequiresRefresh()
  150. {
  151. var changed = base.RequiresRefresh() || _requiresRefresh;
  152. if (!changed)
  153. {
  154. var locations = PhysicalLocations.ToList();
  155. var newLocations = CreateResolveArgs(new DirectoryService(Logger, FileSystem), false).PhysicalLocations.ToList();
  156. if (!locations.SequenceEqual(newLocations))
  157. {
  158. changed = true;
  159. }
  160. }
  161. if (!changed)
  162. {
  163. var folderIds = PhysicalFolderIds.ToList();
  164. var newFolderIds = GetPhysicalFolders(false).Select(i => i.Id).ToList();
  165. if (!folderIds.SequenceEqual(newFolderIds))
  166. {
  167. changed = true;
  168. }
  169. }
  170. return changed;
  171. }
  172. public override bool BeforeMetadataRefresh()
  173. {
  174. var changed = base.BeforeMetadataRefresh() || _requiresRefresh;
  175. _requiresRefresh = false;
  176. return changed;
  177. }
  178. public override double? GetRefreshProgress()
  179. {
  180. var folders = GetPhysicalFolders(true).ToList();
  181. double totalProgresses = 0;
  182. var foldersWithProgress = 0;
  183. foreach (var folder in folders)
  184. {
  185. var progress = ProviderManager.GetRefreshProgress(folder.Id);
  186. if (progress.HasValue)
  187. {
  188. totalProgresses += progress.Value;
  189. foldersWithProgress++;
  190. }
  191. }
  192. if (foldersWithProgress == 0)
  193. {
  194. return null;
  195. }
  196. return (totalProgresses / foldersWithProgress);
  197. }
  198. protected override bool RefreshLinkedChildren(IEnumerable<FileSystemMetadata> fileSystemChildren)
  199. {
  200. return RefreshLinkedChildrenInternal(true);
  201. }
  202. private bool RefreshLinkedChildrenInternal(bool setFolders)
  203. {
  204. var physicalFolders = GetPhysicalFolders(false)
  205. .ToList();
  206. var linkedChildren = physicalFolders
  207. .SelectMany(c => c.LinkedChildren)
  208. .ToList();
  209. var changed = !linkedChildren.SequenceEqual(LinkedChildren, new LinkedChildComparer(FileSystem));
  210. LinkedChildren = linkedChildren;
  211. var folderIds = PhysicalFolderIds.ToList();
  212. var newFolderIds = physicalFolders.Select(i => i.Id).ToList();
  213. if (!folderIds.SequenceEqual(newFolderIds))
  214. {
  215. changed = true;
  216. if (setFolders)
  217. {
  218. PhysicalFolderIds = newFolderIds.ToList();
  219. }
  220. }
  221. return changed;
  222. }
  223. internal override bool IsValidFromResolver(BaseItem newItem)
  224. {
  225. var newCollectionFolder = newItem as CollectionFolder;
  226. if (newCollectionFolder != null)
  227. {
  228. if (!string.Equals(CollectionType, newCollectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  229. {
  230. return false;
  231. }
  232. }
  233. return base.IsValidFromResolver(newItem);
  234. }
  235. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService, bool setPhysicalLocations)
  236. {
  237. var path = ContainingFolderPath;
  238. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
  239. {
  240. FileInfo = FileSystem.GetDirectoryInfo(path),
  241. Path = path,
  242. Parent = Parent,
  243. CollectionType = CollectionType
  244. };
  245. // Gather child folder and files
  246. if (args.IsDirectory)
  247. {
  248. var isPhysicalRoot = args.IsPhysicalRoot;
  249. // When resolving the root, we need it's grandchildren (children of user views)
  250. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  251. var fileSystemDictionary = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  252. // Need to remove subpaths that may have been resolved from shortcuts
  253. // Example: if \\server\movies exists, then strip out \\server\movies\action
  254. if (isPhysicalRoot)
  255. {
  256. var paths = LibraryManager.NormalizeRootPathList(fileSystemDictionary.Values);
  257. fileSystemDictionary = paths.ToDictionary(i => i.FullName);
  258. }
  259. args.FileSystemDictionary = fileSystemDictionary;
  260. }
  261. _requiresRefresh = _requiresRefresh || !args.PhysicalLocations.SequenceEqual(PhysicalLocations);
  262. if (setPhysicalLocations)
  263. {
  264. PhysicalLocationsList = args.PhysicalLocations.ToList();
  265. }
  266. return args;
  267. }
  268. /// <summary>
  269. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  270. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  271. /// </summary>
  272. /// <param name="progress">The progress.</param>
  273. /// <param name="cancellationToken">The cancellation token.</param>
  274. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  275. /// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param>
  276. /// <param name="refreshOptions">The refresh options.</param>
  277. /// <param name="directoryService">The directory service.</param>
  278. /// <returns>Task.</returns>
  279. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  280. {
  281. return Task.FromResult(true);
  282. }
  283. /// <summary>
  284. /// Our children are actually just references to the ones in the physical root...
  285. /// </summary>
  286. /// <value>The actual children.</value>
  287. [IgnoreDataMember]
  288. public override IEnumerable<BaseItem> Children
  289. {
  290. get { return GetActualChildren(); }
  291. }
  292. public IEnumerable<BaseItem> GetActualChildren()
  293. {
  294. return GetPhysicalFolders(true).SelectMany(c => c.Children);
  295. }
  296. public IEnumerable<Folder> GetPhysicalFolders()
  297. {
  298. return GetPhysicalFolders(true);
  299. }
  300. private IEnumerable<Folder> GetPhysicalFolders(bool enableCache)
  301. {
  302. if (enableCache)
  303. {
  304. return PhysicalFolderIds.Select(i => LibraryManager.GetItemById(i)).OfType<Folder>();
  305. }
  306. var rootChildren = LibraryManager.RootFolder.Children
  307. .OfType<Folder>()
  308. .ToList();
  309. return PhysicalLocations.Where(i => !FileSystem.AreEqual(i, Path)).SelectMany(i => GetPhysicalParents(i, rootChildren)).DistinctBy(i => i.Id);
  310. }
  311. private IEnumerable<Folder> GetPhysicalParents(string path, List<Folder> rootChildren)
  312. {
  313. var result = rootChildren
  314. .Where(i => FileSystem.AreEqual(i.Path, path))
  315. .ToList();
  316. if (result.Count == 0)
  317. {
  318. var folder = LibraryManager.FindByPath(path, true) as Folder;
  319. if (folder != null)
  320. {
  321. result.Add(folder);
  322. }
  323. }
  324. return result;
  325. }
  326. [IgnoreDataMember]
  327. public override bool SupportsPeople
  328. {
  329. get
  330. {
  331. return false;
  332. }
  333. }
  334. }
  335. }