CollectionFolder.cs 12 KB

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