CollectionFolder.cs 12 KB

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