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