PlaylistResolver.cs 2.9 KB

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