2
0

CollectionFolder.cs 12 KB

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