| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using Emby.Server.Implementations.Playlists; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using Jellyfin.Extensions; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using MediaBrowser.Controller.Playlists; |
| | | 12 | | using MediaBrowser.Controller.Resolvers; |
| | | 13 | | using MediaBrowser.LocalMetadata.Savers; |
| | | 14 | | |
| | | 15 | | namespace Emby.Server.Implementations.Library.Resolvers |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// <see cref="IItemResolver"/> for <see cref="Playlist"/> library items. |
| | | 19 | | /// </summary> |
| | | 20 | | public class PlaylistResolver : GenericFolderResolver<Playlist> |
| | | 21 | | { |
| | 22 | 22 | | private readonly CollectionType?[] _musicPlaylistCollectionTypes = |
| | 22 | 23 | | [ |
| | 22 | 24 | | null, |
| | 22 | 25 | | CollectionType.music |
| | 22 | 26 | | ]; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | | 29 | | protected override Playlist Resolve(ItemResolveArgs args) |
| | | 30 | | { |
| | 61 | 31 | | if (args.IsDirectory) |
| | | 32 | | { |
| | | 33 | | // It's a playlist if the path is a directory with [playlist] in its name |
| | 51 | 34 | | var filename = Path.GetFileName(Path.TrimEndingDirectorySeparator(args.Path)); |
| | 51 | 35 | | if (string.IsNullOrEmpty(filename)) |
| | | 36 | | { |
| | 0 | 37 | | return null; |
| | | 38 | | } |
| | | 39 | | |
| | 51 | 40 | | if (filename.Contains("[playlist]", StringComparison.OrdinalIgnoreCase)) |
| | | 41 | | { |
| | 0 | 42 | | return new Playlist |
| | 0 | 43 | | { |
| | 0 | 44 | | Path = args.Path, |
| | 0 | 45 | | Name = filename.Replace("[playlist]", string.Empty, StringComparison.OrdinalIgnoreCase).Trim(), |
| | 0 | 46 | | OpenAccess = true |
| | 0 | 47 | | }; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Anything directly inside the internal playlists folder is a playlist, even when its |
| | | 51 | | // playlist.xml is missing: failing to resolve here makes the library scan treat the |
| | | 52 | | // playlist as deleted from disk and remove it, taking its items with it. |
| | 51 | 53 | | if (args.Parent is PlaylistsFolder) |
| | | 54 | | { |
| | 0 | 55 | | return new Playlist |
| | 0 | 56 | | { |
| | 0 | 57 | | Path = args.Path, |
| | 0 | 58 | | Name = filename, |
| | 0 | 59 | | OpenAccess = true |
| | 0 | 60 | | }; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // It's a directory-based playlist if the directory contains a playlist file |
| | | 64 | | IEnumerable<string> filePaths; |
| | | 65 | | try |
| | | 66 | | { |
| | 51 | 67 | | filePaths = Directory.EnumerateFiles(args.Path, "*", new EnumerationOptions { IgnoreInaccessible = t |
| | 51 | 68 | | } |
| | 0 | 69 | | catch (IOException) |
| | | 70 | | { |
| | 0 | 71 | | return null; |
| | | 72 | | } |
| | | 73 | | |
| | 51 | 74 | | if (filePaths.Any(f => f.EndsWith(PlaylistXmlSaver.DefaultPlaylistFilename, StringComparison.OrdinalIgno |
| | | 75 | | { |
| | 0 | 76 | | return new Playlist |
| | 0 | 77 | | { |
| | 0 | 78 | | Path = args.Path, |
| | 0 | 79 | | Name = filename, |
| | 0 | 80 | | OpenAccess = true |
| | 0 | 81 | | }; |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // Check if this is a music playlist file |
| | | 86 | | // It should have the correct collection type and a supported file extension |
| | 10 | 87 | | else if (_musicPlaylistCollectionTypes.Contains(args.CollectionType)) |
| | | 88 | | { |
| | 10 | 89 | | var extension = Path.GetExtension(args.Path.AsSpan()); |
| | 10 | 90 | | if (Playlist.SupportedExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase)) |
| | | 91 | | { |
| | 0 | 92 | | return new Playlist |
| | 0 | 93 | | { |
| | 0 | 94 | | Path = args.Path, |
| | 0 | 95 | | Name = Path.GetFileNameWithoutExtension(args.Path), |
| | 0 | 96 | | IsInMixedFolder = true, |
| | 0 | 97 | | PlaylistMediaType = MediaType.Audio, |
| | 0 | 98 | | OpenAccess = true |
| | 0 | 99 | | }; |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | 61 | 103 | | return null; |
| | 0 | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |