CollectionFolder.cs 10 KB

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