PlaylistResolver.cs 2.7 KB

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