CollectionFolder.cs 11 KB

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