| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using Jellyfin.Data.Enums; |
| | | 11 | | using Jellyfin.Extensions; |
| | | 12 | | using MediaBrowser.Controller.Entities; |
| | | 13 | | using MediaBrowser.Controller.Library; |
| | | 14 | | using MediaBrowser.Controller.Playlists; |
| | | 15 | | using MediaBrowser.Controller.Providers; |
| | | 16 | | using MediaBrowser.Model.IO; |
| | | 17 | | using Microsoft.Extensions.Logging; |
| | | 18 | | using PlaylistsNET.Content; |
| | | 19 | | using UtfUnknown; |
| | | 20 | | |
| | | 21 | | namespace MediaBrowser.Providers.Playlists; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Local playlist provider. |
| | | 25 | | /// </summary> |
| | | 26 | | public class PlaylistItemsProvider : ILocalMetadataProvider<Playlist>, |
| | | 27 | | IHasOrder, |
| | | 28 | | IForcedProvider, |
| | | 29 | | IHasItemChangeMonitor |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// Minimum confidence required before a detected encoding is preferred over UTF-8. |
| | | 33 | | /// </summary> |
| | | 34 | | private const float MinimumEncodingConfidence = 0.5f; |
| | | 35 | | |
| | | 36 | | private readonly IFileSystem _fileSystem; |
| | | 37 | | private readonly ILibraryManager _libraryManager; |
| | | 38 | | private readonly ILogger<PlaylistItemsProvider> _logger; |
| | 34 | 39 | | private readonly CollectionType[] _ignoredCollections = [CollectionType.livetv, CollectionType.boxsets, CollectionTy |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="PlaylistItemsProvider"/> class. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="logger">Instance of the <see cref="ILogger{PlaylistItemsProvider}"/> interface.</param> |
| | | 45 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 46 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 47 | | public PlaylistItemsProvider(ILogger<PlaylistItemsProvider> logger, ILibraryManager libraryManager, IFileSystem file |
| | | 48 | | { |
| | 34 | 49 | | _logger = logger; |
| | 34 | 50 | | _libraryManager = libraryManager; |
| | 34 | 51 | | _fileSystem = fileSystem; |
| | 34 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 0 | 55 | | public string Name => "Playlist Item Provider"; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 0 | 58 | | public int Order => 100; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public Task<MetadataResult<Playlist>> GetMetadata( |
| | | 62 | | ItemInfo info, |
| | | 63 | | IDirectoryService directoryService, |
| | | 64 | | CancellationToken cancellationToken) |
| | | 65 | | { |
| | 0 | 66 | | var result = new MetadataResult<Playlist>() |
| | 0 | 67 | | { |
| | 0 | 68 | | Item = new Playlist |
| | 0 | 69 | | { |
| | 0 | 70 | | Path = info.Path |
| | 0 | 71 | | } |
| | 0 | 72 | | }; |
| | 0 | 73 | | Fetch(result); |
| | | 74 | | |
| | 0 | 75 | | return Task.FromResult(result); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private void Fetch(MetadataResult<Playlist> result) |
| | | 79 | | { |
| | 0 | 80 | | var item = result.Item; |
| | 0 | 81 | | var path = item.Path; |
| | 0 | 82 | | if (!Playlist.IsPlaylistFile(path)) |
| | | 83 | | { |
| | 0 | 84 | | return; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | var extension = Path.GetExtension(path); |
| | 0 | 88 | | if (!Playlist.SupportedExtensions.Contains(extension ?? string.Empty, StringComparison.OrdinalIgnoreCase)) |
| | | 89 | | { |
| | 0 | 90 | | return; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | var items = GetItems(path, extension).ToArray(); |
| | 0 | 94 | | if (items.Length > 0) |
| | | 95 | | { |
| | 0 | 96 | | result.HasMetadata = true; |
| | 0 | 97 | | item.LinkedChildren = items; |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | return; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private IEnumerable<LinkedChild> GetItems(string path, string extension) |
| | | 104 | | { |
| | 0 | 105 | | var libraryRoots = _libraryManager.GetUserRootFolder().Children |
| | 0 | 106 | | .OfType<CollectionFolder>() |
| | 0 | 107 | | .Where(f => f.CollectionType.HasValue && !_ignoredCollections.Contains(f.CollectionType.Valu |
| | 0 | 108 | | .SelectMany(f => f.PhysicalLocations) |
| | 0 | 109 | | .Distinct() |
| | 0 | 110 | | .ToList(); |
| | | 111 | | |
| | 0 | 112 | | using (var stream = File.OpenRead(path)) |
| | | 113 | | { |
| | 0 | 114 | | if (string.Equals(".wpl", extension, StringComparison.OrdinalIgnoreCase)) |
| | | 115 | | { |
| | 0 | 116 | | return GetWplItems(stream, path, libraryRoots); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (string.Equals(".zpl", extension, StringComparison.OrdinalIgnoreCase)) |
| | | 120 | | { |
| | 0 | 121 | | return GetZplItems(stream, path, libraryRoots); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | if (string.Equals(".m3u", extension, StringComparison.OrdinalIgnoreCase)) |
| | | 125 | | { |
| | 0 | 126 | | return GetM3uItems(stream, path, libraryRoots); |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | if (string.Equals(".m3u8", extension, StringComparison.OrdinalIgnoreCase)) |
| | | 130 | | { |
| | 0 | 131 | | return GetM3uItems(stream, path, libraryRoots); |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | if (string.Equals(".pls", extension, StringComparison.OrdinalIgnoreCase)) |
| | | 135 | | { |
| | 0 | 136 | | return GetPlsItems(stream, path, libraryRoots); |
| | | 137 | | } |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | return Enumerable.Empty<LinkedChild>(); |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | private IEnumerable<LinkedChild> GetPlsItems(Stream stream, string playlistPath, List<string> libraryRoots) |
| | | 144 | | { |
| | 0 | 145 | | var content = new PlsContent(); |
| | 0 | 146 | | var playlist = content.GetFromStream(stream, DetectEncoding(stream, playlistPath)); |
| | | 147 | | |
| | 0 | 148 | | return playlist.PlaylistEntries |
| | 0 | 149 | | .Select(i => GetLinkedChild(i.Path, playlistPath, libraryRoots)) |
| | 0 | 150 | | .Where(i => i is not null); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | internal IEnumerable<LinkedChild> GetM3uItems(Stream stream, string playlistPath, List<string> libraryRoots) |
| | | 154 | | { |
| | 7 | 155 | | var content = new M3uContent(); |
| | 7 | 156 | | var playlist = content.GetFromStream(stream, DetectEncoding(stream, playlistPath)); |
| | | 157 | | |
| | 7 | 158 | | return playlist.PlaylistEntries |
| | 7 | 159 | | .Select(i => GetLinkedChild(i.Path, playlistPath, libraryRoots)) |
| | 7 | 160 | | .Where(i => i is not null); |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | private Encoding DetectEncoding(Stream stream, string playlistPath) |
| | | 164 | | { |
| | 7 | 165 | | var detected = CharsetDetector.DetectFromStream(stream).Detected; |
| | 7 | 166 | | stream.Seek(0, SeekOrigin.Begin); |
| | | 167 | | |
| | 7 | 168 | | if (detected?.Encoding is null || detected.Confidence < MinimumEncodingConfidence) |
| | | 169 | | { |
| | 0 | 170 | | _logger.LogDebug("Could not detect the encoding of playlist {Path}, assuming UTF-8", playlistPath); |
| | 0 | 171 | | return Encoding.UTF8; |
| | | 172 | | } |
| | | 173 | | |
| | 7 | 174 | | _logger.LogDebug("Detected encoding {Encoding} for playlist {Path}", detected.Encoding.WebName, playlistPath); |
| | 7 | 175 | | return detected.Encoding; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | private IEnumerable<LinkedChild> GetZplItems(Stream stream, string playlistPath, List<string> libraryRoots) |
| | | 179 | | { |
| | 0 | 180 | | var content = new ZplContent(); |
| | 0 | 181 | | var playlist = content.GetFromStream(stream); |
| | | 182 | | |
| | 0 | 183 | | return playlist.PlaylistEntries |
| | 0 | 184 | | .Select(i => GetLinkedChild(i.Path, playlistPath, libraryRoots)) |
| | 0 | 185 | | .Where(i => i is not null); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | private IEnumerable<LinkedChild> GetWplItems(Stream stream, string playlistPath, List<string> libraryRoots) |
| | | 189 | | { |
| | 0 | 190 | | var content = new WplContent(); |
| | 0 | 191 | | var playlist = content.GetFromStream(stream); |
| | | 192 | | |
| | 0 | 193 | | return playlist.PlaylistEntries |
| | 0 | 194 | | .Select(i => GetLinkedChild(i.Path, playlistPath, libraryRoots)) |
| | 0 | 195 | | .Where(i => i is not null); |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | private LinkedChild GetLinkedChild(string itemPath, string playlistPath, List<string> libraryRoots) |
| | | 199 | | { |
| | 8 | 200 | | if (TryResolvePlaylistItem(itemPath, playlistPath, libraryRoots, out var item)) |
| | | 201 | | { |
| | 7 | 202 | | return new LinkedChild |
| | 7 | 203 | | { |
| | 7 | 204 | | ItemId = item.Id, |
| | 7 | 205 | | Type = LinkedChildType.Manual |
| | 7 | 206 | | }; |
| | | 207 | | } |
| | | 208 | | |
| | 1 | 209 | | return null; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | private bool TryResolvePlaylistItem(string itemPath, string playlistPath, List<string> libraryPaths, out BaseItem it |
| | | 213 | | { |
| | 8 | 214 | | item = null; |
| | 8 | 215 | | string pathToCheck = _fileSystem.MakeAbsolutePath(Path.GetDirectoryName(playlistPath), itemPath); |
| | 8 | 216 | | if (!File.Exists(pathToCheck) && !TryNormalizePath(ref pathToCheck)) |
| | | 217 | | { |
| | 1 | 218 | | return false; |
| | | 219 | | } |
| | | 220 | | |
| | 21 | 221 | | foreach (var libraryPath in libraryPaths) |
| | | 222 | | { |
| | 7 | 223 | | if (pathToCheck.StartsWith(libraryPath, StringComparison.OrdinalIgnoreCase)) |
| | | 224 | | { |
| | 7 | 225 | | item = _libraryManager.FindByPath(pathToCheck, null); |
| | 7 | 226 | | return item is not null; |
| | | 227 | | } |
| | | 228 | | } |
| | | 229 | | |
| | 0 | 230 | | return false; |
| | 7 | 231 | | } |
| | | 232 | | |
| | | 233 | | private static bool TryNormalizePath(ref string path) |
| | | 234 | | { |
| | 9 | 235 | | foreach (var form in new[] { NormalizationForm.FormC, NormalizationForm.FormD }) |
| | | 236 | | { |
| | | 237 | | string normalized; |
| | | 238 | | try |
| | | 239 | | { |
| | 3 | 240 | | if (path.IsNormalized(form)) |
| | | 241 | | { |
| | 1 | 242 | | continue; |
| | | 243 | | } |
| | | 244 | | |
| | 2 | 245 | | normalized = path.Normalize(form); |
| | 2 | 246 | | } |
| | 0 | 247 | | catch (ArgumentException) |
| | | 248 | | { |
| | | 249 | | // The path is not valid Unicode, there is nothing to normalize. |
| | 0 | 250 | | return false; |
| | | 251 | | } |
| | | 252 | | |
| | 2 | 253 | | if (File.Exists(normalized)) |
| | | 254 | | { |
| | 1 | 255 | | path = normalized; |
| | 1 | 256 | | return true; |
| | | 257 | | } |
| | | 258 | | } |
| | | 259 | | |
| | 1 | 260 | | return false; |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <inheritdoc /> |
| | | 264 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 265 | | { |
| | 0 | 266 | | var path = item.Path; |
| | 0 | 267 | | if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol) |
| | | 268 | | { |
| | 0 | 269 | | var file = directoryService.GetFile(path); |
| | 0 | 270 | | if (file is not null && item.HasChanged(file.LastWriteTimeUtc)) |
| | | 271 | | { |
| | 0 | 272 | | _logger.LogDebug("Refreshing {Path} due to date modified timestamp change.", path); |
| | 0 | 273 | | return true; |
| | | 274 | | } |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | return false; |
| | | 278 | | } |
| | | 279 | | } |