PlaylistResolver.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 : FolderResolver<Playlist>
  17. {
  18. private string[] _musicPlaylistCollectionTypes = new string[] {
  19. string.Empty,
  20. CollectionType.Music
  21. };
  22. /// <inheritdoc/>
  23. protected override Playlist Resolve(ItemResolveArgs args)
  24. {
  25. if (args.IsDirectory)
  26. {
  27. // It's a boxset if the path is a directory with [playlist] in it's the name
  28. // TODO: Should this use Path.GetDirectoryName() instead?
  29. bool isBoxSet = Path.GetFileName(args.Path)
  30. ?.Contains("[playlist]", StringComparison.OrdinalIgnoreCase)
  31. ?? false;
  32. if (isBoxSet)
  33. {
  34. return new Playlist
  35. {
  36. Path = args.Path,
  37. Name = Path.GetFileName(args.Path).Replace("[playlist]", string.Empty, StringComparison.OrdinalIgnoreCase).Trim()
  38. };
  39. }
  40. // It's a directory-based playlist if the directory contains a playlist file
  41. var filePaths = Directory.EnumerateFiles(args.Path, "*", new EnumerationOptions { IgnoreInaccessible = true });
  42. if (filePaths.Any(f => f.EndsWith(PlaylistXmlSaver.DefaultPlaylistFilename, StringComparison.OrdinalIgnoreCase)))
  43. {
  44. return new Playlist
  45. {
  46. Path = args.Path,
  47. Name = Path.GetFileName(args.Path)
  48. };
  49. }
  50. }
  51. // Check if this is a music playlist file
  52. // It should have the correct collection type and a supported file extension
  53. else if (_musicPlaylistCollectionTypes.Contains(args.CollectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  54. {
  55. var extension = Path.GetExtension(args.Path);
  56. if (Playlist.SupportedExtensions.Contains(extension ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  57. {
  58. return new Playlist
  59. {
  60. Path = args.Path,
  61. Name = Path.GetFileNameWithoutExtension(args.Path),
  62. IsInMixedFolder = true,
  63. PlaylistMediaType = MediaType.Audio
  64. };
  65. }
  66. }
  67. return null;
  68. }
  69. }
  70. }