SpecialFolderResolver.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using Jellyfin.Data.Enums;
  7. using MediaBrowser.Controller;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Resolvers;
  11. using MediaBrowser.Model.IO;
  12. namespace Emby.Server.Implementations.Library.Resolvers
  13. {
  14. public class SpecialFolderResolver : GenericFolderResolver<Folder>
  15. {
  16. private readonly IFileSystem _fileSystem;
  17. private readonly IServerApplicationPaths _appPaths;
  18. public SpecialFolderResolver(IFileSystem fileSystem, IServerApplicationPaths appPaths)
  19. {
  20. _fileSystem = fileSystem;
  21. _appPaths = appPaths;
  22. }
  23. /// <summary>
  24. /// Gets the priority.
  25. /// </summary>
  26. /// <value>The priority.</value>
  27. public override ResolverPriority Priority => ResolverPriority.First;
  28. /// <summary>
  29. /// Resolves the specified args.
  30. /// </summary>
  31. /// <param name="args">The args.</param>
  32. /// <returns>Folder.</returns>
  33. protected override Folder Resolve(ItemResolveArgs args)
  34. {
  35. if (args.IsDirectory)
  36. {
  37. if (args.IsPhysicalRoot)
  38. {
  39. return new AggregateFolder();
  40. }
  41. if (string.Equals(args.Path, _appPaths.DefaultUserViewsPath, StringComparison.OrdinalIgnoreCase))
  42. {
  43. return new UserRootFolder(); // if we got here and still a root - must be user root
  44. }
  45. if (args.IsVf)
  46. {
  47. return new CollectionFolder
  48. {
  49. CollectionType = GetCollectionType(args),
  50. PhysicalLocationsList = args.PhysicalLocations
  51. };
  52. }
  53. }
  54. return null;
  55. }
  56. private CollectionType? GetCollectionType(ItemResolveArgs args)
  57. {
  58. return args.FileSystemChildren
  59. .Where(i =>
  60. {
  61. try
  62. {
  63. return !i.IsDirectory &&
  64. string.Equals(".collection", i.Extension, StringComparison.OrdinalIgnoreCase);
  65. }
  66. catch (IOException)
  67. {
  68. return false;
  69. }
  70. })
  71. .Select(i => _fileSystem.GetFileNameWithoutExtension(i))
  72. .Select(i => Enum.TryParse<CollectionType>(i, out var collectionType) ? collectionType : (CollectionType?)null)
  73. .FirstOrDefault(i => i is not null);
  74. }
  75. }
  76. }