CollectionFolder.cs 10 KB

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