SpecialFolderResolver.cs 2.5 KB

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