CollectionFolder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService, bool setPhysicalLocations)
  222. {
  223. var path = ContainingFolderPath;
  224. var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, directoryService)
  225. {
  226. FileInfo = FileSystem.GetDirectoryInfo(path),
  227. Path = path,
  228. Parent = GetParent() as Folder,
  229. CollectionType = CollectionType
  230. };
  231. // Gather child folder and files
  232. if (args.IsDirectory)
  233. {
  234. var isPhysicalRoot = args.IsPhysicalRoot;
  235. // When resolving the root, we need it's grandchildren (children of user views)
  236. var flattenFolderDepth = isPhysicalRoot ? 2 : 0;
  237. var files = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, Logger, args, flattenFolderDepth: flattenFolderDepth, resolveShortcuts: isPhysicalRoot || args.IsVf);
  238. // Need to remove subpaths that may have been resolved from shortcuts
  239. // Example: if \\server\movies exists, then strip out \\server\movies\action
  240. if (isPhysicalRoot)
  241. {
  242. files = LibraryManager.NormalizeRootPathList(files).ToArray();
  243. }
  244. args.FileSystemChildren = files;
  245. }
  246. _requiresRefresh = _requiresRefresh || !args.PhysicalLocations.SequenceEqual(PhysicalLocations);
  247. if (setPhysicalLocations)
  248. {
  249. PhysicalLocationsList = args.PhysicalLocations;
  250. }
  251. return args;
  252. }
  253. /// <summary>
  254. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  255. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  256. /// </summary>
  257. /// <param name="progress">The progress.</param>
  258. /// <param name="cancellationToken">The cancellation token.</param>
  259. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  260. /// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param>
  261. /// <param name="refreshOptions">The refresh options.</param>
  262. /// <param name="directoryService">The directory service.</param>
  263. /// <returns>Task.</returns>
  264. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  265. {
  266. return Task.FromResult(true);
  267. }
  268. /// <summary>
  269. /// Our children are actually just references to the ones in the physical root...
  270. /// </summary>
  271. /// <value>The actual children.</value>
  272. [IgnoreDataMember]
  273. public override IEnumerable<BaseItem> Children
  274. {
  275. get { return GetActualChildren(); }
  276. }
  277. public IEnumerable<BaseItem> GetActualChildren()
  278. {
  279. return GetPhysicalFolders(true).SelectMany(c => c.Children);
  280. }
  281. public IEnumerable<Folder> GetPhysicalFolders()
  282. {
  283. return GetPhysicalFolders(true);
  284. }
  285. private IEnumerable<Folder> GetPhysicalFolders(bool enableCache)
  286. {
  287. if (enableCache)
  288. {
  289. return PhysicalFolderIds.Select(i => LibraryManager.GetItemById(i)).OfType<Folder>();
  290. }
  291. var rootChildren = LibraryManager.RootFolder.Children
  292. .OfType<Folder>()
  293. .ToList();
  294. return PhysicalLocations.Where(i => !FileSystem.AreEqual(i, Path)).SelectMany(i => GetPhysicalParents(i, rootChildren)).DistinctBy(i => i.Id);
  295. }
  296. private IEnumerable<Folder> GetPhysicalParents(string path, List<Folder> rootChildren)
  297. {
  298. var result = rootChildren
  299. .Where(i => FileSystem.AreEqual(i.Path, path))
  300. .ToList();
  301. if (result.Count == 0)
  302. {
  303. var folder = LibraryManager.FindByPath(path, true) as Folder;
  304. if (folder != null)
  305. {
  306. result.Add(folder);
  307. }
  308. }
  309. return result;
  310. }
  311. [IgnoreDataMember]
  312. public override bool SupportsPeople
  313. {
  314. get
  315. {
  316. return false;
  317. }
  318. }
  319. }
  320. }