CollectionFolder.cs 10 KB

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