PlaylistResolver.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Playlists;
  8. using MediaBrowser.Controller.Resolvers;
  9. using MediaBrowser.LocalMetadata.Savers;
  10. using MediaBrowser.Model.Entities;
  11. namespace Emby.Server.Implementations.Library.Resolvers
  12. {
  13. /// <summary>
  14. /// <see cref="IItemResolver"/> for <see cref="Playlist"/> library items.
  15. /// </summary>
  16. public class PlaylistResolver : GenericFolderResolver<Playlist>
  17. {
  18. private string[] _musicPlaylistCollectionTypes =
  19. {
  20. string.Empty,
  21. CollectionType.Music
  22. };
  23. /// <inheritdoc/>
  24. protected override Playlist Resolve(ItemResolveArgs args)
  25. {
  26. if (args.IsDirectory)
  27. {
  28. // It's a boxset if the path is a directory with [playlist] in it's the name
  29. // TODO: Should this use Path.GetDirectoryName() instead?
  30. bool isBoxSet = Path.GetFileName(args.Path)
  31. ?.Contains("[playlist]", StringComparison.OrdinalIgnoreCase)
  32. ?? false;
  33. if (isBoxSet)
  34. {
  35. return new Playlist
  36. {
  37. Path = args.Path,
  38. Name = Path.GetFileName(args.Path).Replace("[playlist]", string.Empty, StringComparison.OrdinalIgnoreCase).Trim()
  39. };
  40. }
  41. // It's a directory-based playlist if the directory contains a playlist file
  42. var filePaths = Directory.EnumerateFiles(args.Path, "*", new EnumerationOptions { IgnoreInaccessible = true });
  43. if (filePaths.Any(f => f.EndsWith(PlaylistXmlSaver.DefaultPlaylistFilename, StringComparison.OrdinalIgnoreCase)))
  44. {
  45. return new Playlist
  46. {
  47. Path = args.Path,
  48. Name = Path.GetFileName(args.Path)
  49. };
  50. }
  51. }
  52. // Check if this is a music playlist file
  53. // It should have the correct collection type and a supported file extension
  54. else if (_musicPlaylistCollectionTypes.Contains(args.CollectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  55. {
  56. var extension = Path.GetExtension(args.Path);
  57. if (Playlist.SupportedExtensions.Contains(extension ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  58. {
  59. return new Playlist
  60. {
  61. Path = args.Path,
  62. Name = Path.GetFileNameWithoutExtension(args.Path),
  63. IsInMixedFolder = true,
  64. PlaylistMediaType = MediaType.Audio
  65. };
  66. }
  67. }
  68. return null;
  69. }
  70. }
  71. }