CollectionFolder.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.Tasks;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Controller.Entities
  13. {
  14. /// <summary>
  15. /// Specialized Folder class that points to a subset of the physical folders in the system.
  16. /// It is created from the user-specific folders within the system root
  17. /// </summary>
  18. public class CollectionFolder : Folder, ICollectionFolder, IByReferenceItem
  19. {
  20. /// <summary>
  21. /// Gets a value indicating whether this instance is virtual folder.
  22. /// </summary>
  23. /// <value><c>true</c> if this instance is virtual folder; otherwise, <c>false</c>.</value>
  24. [IgnoreDataMember]
  25. public override bool IsVirtualFolder
  26. {
  27. get
  28. {
  29. return true;
  30. }
  31. }
  32. /// <summary>
  33. /// Allow different display preferences for each collection folder
  34. /// </summary>
  35. /// <value>The display prefs id.</value>
  36. public override Guid DisplayPrefsId
  37. {
  38. get
  39. {
  40. return Id;
  41. }
  42. }
  43. // Cache this since it will be used a lot
  44. /// <summary>
  45. /// The null task result
  46. /// </summary>
  47. private static readonly Task NullTaskResult = Task.FromResult<object>(null);
  48. /// <summary>
  49. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  50. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  51. /// </summary>
  52. /// <param name="progress">The progress.</param>
  53. /// <param name="cancellationToken">The cancellation token.</param>
  54. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  55. /// <returns>Task.</returns>
  56. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null)
  57. {
  58. //we don't directly validate our children
  59. //but we do need to clear out the index cache...
  60. IndexCache = new ConcurrentDictionary<string, List<BaseItem>>(StringComparer.OrdinalIgnoreCase);
  61. return NullTaskResult;
  62. }
  63. /// <summary>
  64. /// Our children are actually just references to the ones in the physical root...
  65. /// </summary>
  66. /// <value>The actual children.</value>
  67. protected override ConcurrentBag<BaseItem> ActualChildren
  68. {
  69. get
  70. {
  71. IEnumerable<Guid> folderIds;
  72. try
  73. {
  74. // Accessing ResolveArgs could involve file system access
  75. folderIds = ResolveArgs.PhysicalLocations.Select(f => (f.GetMBId(typeof(Folder))));
  76. }
  77. catch (IOException ex)
  78. {
  79. Logger.ErrorException("Error creating FolderIds for {0}", ex, Path);
  80. folderIds = new Guid[] {};
  81. }
  82. var ourChildren =
  83. LibraryManager.RootFolder.Children.OfType<Folder>()
  84. .Where(i => folderIds.Contains(i.Id))
  85. .SelectMany(c => c.Children);
  86. return new ConcurrentBag<BaseItem>(ourChildren);
  87. }
  88. }
  89. }
  90. }