CollectionFolder.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Model.Entities;
  3. using System;
  4. using System.Collections.Concurrent;
  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. namespace MediaBrowser.Controller.Entities
  12. {
  13. /// <summary>
  14. /// Specialized Folder class that points to a subset of the physical folders in the system.
  15. /// It is created from the user-specific folders within the system root
  16. /// </summary>
  17. public class CollectionFolder : Folder, ICollectionFolder
  18. {
  19. /// <summary>
  20. /// Gets a value indicating whether this instance is virtual folder.
  21. /// </summary>
  22. /// <value><c>true</c> if this instance is virtual folder; otherwise, <c>false</c>.</value>
  23. [IgnoreDataMember]
  24. public override bool IsVirtualFolder
  25. {
  26. get
  27. {
  28. return true;
  29. }
  30. }
  31. /// <summary>
  32. /// Allow different display preferences for each collection folder
  33. /// </summary>
  34. /// <value>The display prefs id.</value>
  35. protected override Guid DisplayPreferencesId
  36. {
  37. get
  38. {
  39. return Id;
  40. }
  41. }
  42. // Cache this since it will be used a lot
  43. /// <summary>
  44. /// The null task result
  45. /// </summary>
  46. private static readonly Task NullTaskResult = Task.FromResult<object>(null);
  47. /// <summary>
  48. /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
  49. /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
  50. /// </summary>
  51. /// <param name="progress">The progress.</param>
  52. /// <param name="cancellationToken">The cancellation token.</param>
  53. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  54. /// <param name="forceRefreshMetadata">if set to <c>true</c> [force refresh metadata].</param>
  55. /// <returns>Task.</returns>
  56. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool? recursive = null, bool forceRefreshMetadata = false)
  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 ConcurrentDictionary<Guid,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 ConcurrentDictionary<Guid,BaseItem>(ourChildren.ToDictionary(i => i.Id));
  87. }
  88. }
  89. }
  90. }