UserRootFolder.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Library;
  4. using MediaBrowser.Model.Querying;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.Entities
  11. {
  12. /// <summary>
  13. /// Special class used for User Roots. Children contain actual ones defined for this user
  14. /// PLUS the virtual folders from the physical root (added by plug-ins).
  15. /// </summary>
  16. public class UserRootFolder : Folder
  17. {
  18. public override async Task<QueryResult<BaseItem>> GetItems(InternalItemsQuery query)
  19. {
  20. if (query.Recursive)
  21. {
  22. var items = query.User.RootFolder.GetRecursiveChildren(query.User);
  23. return SortAndFilter(items, query);
  24. }
  25. var result = await UserViewManager.GetUserViews(new UserViewQuery
  26. {
  27. UserId = query.User.Id.ToString("N")
  28. }, CancellationToken.None).ConfigureAwait(false);
  29. return SortAndFilter(result, query);
  30. }
  31. public override bool IsPreSorted
  32. {
  33. get
  34. {
  35. return true;
  36. }
  37. }
  38. /// <summary>
  39. /// Get the children of this folder from the actual file system
  40. /// </summary>
  41. /// <returns>IEnumerable{BaseItem}.</returns>
  42. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  43. {
  44. return base.GetNonCachedChildren(directoryService).Concat(LibraryManager.RootFolder.VirtualChildren);
  45. }
  46. public override bool BeforeMetadataRefresh()
  47. {
  48. var hasChanges = base.BeforeMetadataRefresh();
  49. if (string.Equals("default", Name, StringComparison.OrdinalIgnoreCase))
  50. {
  51. Name = "Media Folders";
  52. hasChanges = true;
  53. }
  54. return hasChanges;
  55. }
  56. protected override async Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  57. {
  58. await base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService)
  59. .ConfigureAwait(false);
  60. // Not the best way to handle this, but it solves an issue
  61. // CollectionFolders aren't always getting saved after changes
  62. // This means that grabbing the item by Id may end up returning the old one
  63. // Fix is in two places - make sure the folder gets saved
  64. // And here to remedy it for affected users.
  65. // In theory this can be removed eventually.
  66. foreach (var item in Children)
  67. {
  68. LibraryManager.RegisterItem(item);
  69. }
  70. }
  71. public override void FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, User user)
  72. {
  73. // Nothing meaninful here and will only waste resources
  74. }
  75. }
  76. }