< Summary - Jellyfin

Information
Class: MediaBrowser.Providers.MediaInfo.MediaInfoResolver
Assembly: MediaBrowser.Providers
File(s): /srv/git/jellyfin/MediaBrowser.Providers/MediaInfo/MediaInfoResolver.cs
Line coverage
56%
Covered lines: 47
Uncovered lines: 36
Coverable lines: 83
Total lines: 347
Line coverage: 56.6%
Branch coverage
53%
Covered branches: 28
Total branches: 52
Branch coverage: 53.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetExternalStreams(...)0%4260%
GetExternalFiles(...)100%2020100%
GetExternalFiles(...)0%342180%
GetMediaInfo(...)100%11100%
MergeMetadata(...)100%88100%

File(s)

/srv/git/jellyfin/MediaBrowser.Providers/MediaInfo/MediaInfoResolver.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Emby.Naming.Common;
 8using Emby.Naming.ExternalFiles;
 9using MediaBrowser.Controller.Entities;
 10using MediaBrowser.Controller.Entities.Audio;
 11using MediaBrowser.Controller.MediaEncoding;
 12using MediaBrowser.Controller.Providers;
 13using MediaBrowser.Model.Dlna;
 14using MediaBrowser.Model.Dto;
 15using MediaBrowser.Model.Entities;
 16using MediaBrowser.Model.Globalization;
 17using MediaBrowser.Model.IO;
 18using MediaBrowser.Model.MediaInfo;
 19using Microsoft.Extensions.Logging;
 20
 21namespace MediaBrowser.Providers.MediaInfo
 22{
 23    /// <summary>
 24    /// Resolves external files for <see cref="Video"/>.
 25    /// </summary>
 26    public abstract class MediaInfoResolver
 27    {
 28        /// <summary>
 29        /// The <see cref="ExternalPathParser"/> instance.
 30        /// </summary>
 31        private readonly ExternalPathParser _externalPathParser;
 32
 33        /// <summary>
 34        /// The <see cref="IMediaEncoder"/> instance.
 35        /// </summary>
 36        private readonly IMediaEncoder _mediaEncoder;
 37
 38        private readonly ILogger _logger;
 39        private readonly IFileSystem _fileSystem;
 40
 41        /// <summary>
 42        /// The <see cref="NamingOptions"/> instance.
 43        /// </summary>
 44        private readonly NamingOptions _namingOptions;
 45
 46        /// <summary>
 47        /// The <see cref="DlnaProfileType"/> of the files this resolver should resolve.
 48        /// </summary>
 49        private readonly DlnaProfileType _type;
 50
 51        /// <summary>
 52        /// Initializes a new instance of the <see cref="MediaInfoResolver"/> class.
 53        /// </summary>
 54        /// <param name="logger">The logger.</param>
 55        /// <param name="localizationManager">The localization manager.</param>
 56        /// <param name="mediaEncoder">The media encoder.</param>
 57        /// <param name="fileSystem">The file system.</param>
 58        /// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFl
 59        /// <param name="type">The <see cref="DlnaProfileType"/> of the parsed file.</param>
 60        protected MediaInfoResolver(
 61            ILogger logger,
 62            ILocalizationManager localizationManager,
 63            IMediaEncoder mediaEncoder,
 64            IFileSystem fileSystem,
 65            NamingOptions namingOptions,
 66            DlnaProfileType type)
 67        {
 12868            _logger = logger;
 12869            _mediaEncoder = mediaEncoder;
 12870            _fileSystem = fileSystem;
 12871            _namingOptions = namingOptions;
 12872            _type = type;
 12873            _externalPathParser = new ExternalPathParser(namingOptions, localizationManager, _type);
 12874        }
 75
 76        /// <summary>
 77        /// Retrieves the external streams for the provided video.
 78        /// </summary>
 79        /// <param name="video">The <see cref="Video"/> object to search external streams for.</param>
 80        /// <param name="startIndex">The stream index to start adding external streams at.</param>
 81        /// <param name="directoryService">The directory service to search for files.</param>
 82        /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
 83        /// <param name="cancellationToken">The cancellation token.</param>
 84        /// <returns>The external streams located.</returns>
 85        public async Task<IReadOnlyList<MediaStream>> GetExternalStreamsAsync(
 86            Video video,
 87            int startIndex,
 88            IDirectoryService directoryService,
 89            bool clearCache,
 90            CancellationToken cancellationToken)
 91        {
 92            if (!video.IsFileProtocol)
 93            {
 94                return Array.Empty<MediaStream>();
 95            }
 96
 97            var pathInfos = GetExternalFiles(video, directoryService, clearCache);
 98
 99            if (!pathInfos.Any())
 100            {
 101                return Array.Empty<MediaStream>();
 102            }
 103
 104            var mediaStreams = new List<MediaStream>();
 105
 106            foreach (var pathInfo in pathInfos)
 107            {
 108                if (!pathInfo.Path.AsSpan().EndsWith(".strm", StringComparison.OrdinalIgnoreCase))
 109                {
 110                    try
 111                    {
 112                        var mediaInfo = await GetMediaInfo(pathInfo.Path, _type, cancellationToken).ConfigureAwait(false
 113
 114                        if (mediaInfo.MediaStreams.Count == 1)
 115                        {
 116                            MediaStream mediaStream = mediaInfo.MediaStreams[0];
 117
 118                            if ((mediaStream.Type == MediaStreamType.Audio && _type == DlnaProfileType.Audio)
 119                                || (mediaStream.Type == MediaStreamType.Subtitle && _type == DlnaProfileType.Subtitle))
 120                            {
 121                                mediaStream.Index = startIndex++;
 122                                mediaStream.IsDefault = pathInfo.IsDefault || mediaStream.IsDefault;
 123                                mediaStream.IsForced = pathInfo.IsForced || mediaStream.IsForced;
 124                                mediaStream.IsHearingImpaired = pathInfo.IsHearingImpaired || mediaStream.IsHearingImpai
 125
 126                                mediaStreams.Add(MergeMetadata(mediaStream, pathInfo));
 127                            }
 128                        }
 129                        else
 130                        {
 131                            foreach (MediaStream mediaStream in mediaInfo.MediaStreams)
 132                            {
 133                                if ((mediaStream.Type == MediaStreamType.Audio && _type == DlnaProfileType.Audio)
 134                                    || (mediaStream.Type == MediaStreamType.Subtitle && _type == DlnaProfileType.Subtitl
 135                                {
 136                                    mediaStream.Index = startIndex++;
 137
 138                                    mediaStreams.Add(MergeMetadata(mediaStream, pathInfo));
 139                                }
 140                            }
 141                        }
 142                    }
 143                    catch (Exception ex)
 144                    {
 145                        _logger.LogError(ex, "Error getting external streams from {Path}", pathInfo.Path);
 146
 147                        continue;
 148                    }
 149                }
 150            }
 151
 152            return mediaStreams;
 153        }
 154
 155        /// <summary>
 156        /// Retrieves the external streams for the provided audio.
 157        /// </summary>
 158        /// <param name="audio">The <see cref="Audio"/> object to search external streams for.</param>
 159        /// <param name="startIndex">The stream index to start adding external streams at.</param>
 160        /// <param name="directoryService">The directory service to search for files.</param>
 161        /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
 162        /// <returns>The external streams located.</returns>
 163        public IReadOnlyList<MediaStream> GetExternalStreams(
 164            Audio audio,
 165            int startIndex,
 166            IDirectoryService directoryService,
 167            bool clearCache)
 168        {
 0169            if (!audio.IsFileProtocol)
 170            {
 0171                return Array.Empty<MediaStream>();
 172            }
 173
 0174            var pathInfos = GetExternalFiles(audio, directoryService, clearCache);
 175
 0176            if (pathInfos.Count == 0)
 177            {
 0178                return Array.Empty<MediaStream>();
 179            }
 180
 0181            var mediaStreams = new MediaStream[pathInfos.Count];
 182
 0183            for (var i = 0; i < pathInfos.Count; i++)
 184            {
 0185                mediaStreams[i] = new MediaStream
 0186                {
 0187                    Type = MediaStreamType.Lyric,
 0188                    Path = pathInfos[i].Path,
 0189                    Language = pathInfos[i].Language,
 0190                    Index = startIndex++
 0191                };
 192            }
 193
 0194            return mediaStreams;
 195        }
 196
 197        /// <summary>
 198        /// Returns the external file infos for the given video.
 199        /// </summary>
 200        /// <param name="video">The <see cref="Video"/> object to search external files for.</param>
 201        /// <param name="directoryService">The directory service to search for files.</param>
 202        /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
 203        /// <returns>The external file paths located.</returns>
 204        public IReadOnlyList<ExternalPathParserResult> GetExternalFiles(
 205            Video video,
 206            IDirectoryService directoryService,
 207            bool clearCache)
 208        {
 32209            if (!video.IsFileProtocol)
 210            {
 1211                return Array.Empty<ExternalPathParserResult>();
 212            }
 213
 214            // Check if video folder exists
 31215            string folder = video.ContainingFolderPath;
 31216            if (!_fileSystem.DirectoryExists(folder))
 217            {
 2218                return Array.Empty<ExternalPathParserResult>();
 219            }
 220
 29221            var files = directoryService.GetFilePaths(folder, clearCache, true).ToList();
 29222            files.Remove(video.Path);
 29223            var internalMetadataPath = video.GetInternalMetadataPath();
 29224            if (_fileSystem.DirectoryExists(internalMetadataPath))
 225            {
 28226                files.AddRange(directoryService.GetFilePaths(internalMetadataPath, clearCache, true));
 227            }
 228
 29229            if (files.Count == 0)
 230            {
 1231                return Array.Empty<ExternalPathParserResult>();
 232            }
 233
 28234            var externalPathInfos = new List<ExternalPathParserResult>();
 28235            ReadOnlySpan<char> prefix = video.FileNameWithoutExtension;
 116236            foreach (var file in files)
 237            {
 30238                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.AsSpan());
 30239                if (fileNameWithoutExtension.Length >= prefix.Length
 30240                    && prefix.Equals(fileNameWithoutExtension[..prefix.Length], StringComparison.OrdinalIgnoreCase)
 30241                    && (fileNameWithoutExtension.Length == prefix.Length || _namingOptions.MediaFlagDelimiters.Contains(
 242                {
 27243                    var externalPathInfo = _externalPathParser.ParseFile(file, fileNameWithoutExtension[prefix.Length..]
 244
 27245                    if (externalPathInfo is not null)
 246                    {
 20247                        externalPathInfos.Add(externalPathInfo);
 248                    }
 249                }
 250            }
 251
 28252            return externalPathInfos;
 253        }
 254
 255        /// <summary>
 256        /// Returns the external file infos for the given audio.
 257        /// </summary>
 258        /// <param name="audio">The <see cref="Audio"/> object to search external files for.</param>
 259        /// <param name="directoryService">The directory service to search for files.</param>
 260        /// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
 261        /// <returns>The external file paths located.</returns>
 262        public IReadOnlyList<ExternalPathParserResult> GetExternalFiles(
 263            Audio audio,
 264            IDirectoryService directoryService,
 265            bool clearCache)
 266        {
 0267            if (!audio.IsFileProtocol)
 268            {
 0269                return Array.Empty<ExternalPathParserResult>();
 270            }
 271
 0272            string folder = audio.ContainingFolderPath;
 0273            var files = directoryService.GetFilePaths(folder, clearCache, true).ToList();
 0274            files.Remove(audio.Path);
 0275            var internalMetadataPath = audio.GetInternalMetadataPath();
 0276            if (_fileSystem.DirectoryExists(internalMetadataPath))
 277            {
 0278                files.AddRange(directoryService.GetFilePaths(internalMetadataPath, clearCache, true));
 279            }
 280
 0281            if (files.Count == 0)
 282            {
 0283                return Array.Empty<ExternalPathParserResult>();
 284            }
 285
 0286            var externalPathInfos = new List<ExternalPathParserResult>();
 0287            ReadOnlySpan<char> prefix = audio.FileNameWithoutExtension;
 0288            foreach (var file in files)
 289            {
 0290                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.AsSpan());
 0291                if (fileNameWithoutExtension.Length >= prefix.Length
 0292                    && prefix.Equals(fileNameWithoutExtension[..prefix.Length], StringComparison.OrdinalIgnoreCase)
 0293                    && (fileNameWithoutExtension.Length == prefix.Length || _namingOptions.MediaFlagDelimiters.Contains(
 294                {
 0295                    var externalPathInfo = _externalPathParser.ParseFile(file, fileNameWithoutExtension[prefix.Length..]
 296
 0297                    if (externalPathInfo is not null)
 298                    {
 0299                        externalPathInfos.Add(externalPathInfo);
 300                    }
 301                }
 302            }
 303
 0304            return externalPathInfos;
 305        }
 306
 307        /// <summary>
 308        /// Returns the media info of the given file.
 309        /// </summary>
 310        /// <param name="path">The path to the file.</param>
 311        /// <param name="type">The <see cref="DlnaProfileType"/>.</param>
 312        /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
 313        /// <returns>The media info for the given file.</returns>
 314        private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(string path, DlnaProfileType type, CancellationToken cancel
 315        {
 15316            cancellationToken.ThrowIfCancellationRequested();
 317
 15318            return _mediaEncoder.GetMediaInfo(
 15319                new MediaInfoRequest
 15320                {
 15321                    MediaType = type,
 15322                    MediaSource = new MediaSourceInfo
 15323                    {
 15324                        Path = path,
 15325                        Protocol = MediaProtocol.File
 15326                    }
 15327                },
 15328                cancellationToken);
 329        }
 330
 331        /// <summary>
 332        /// Merges path metadata into stream metadata.
 333        /// </summary>
 334        /// <param name="mediaStream">The <see cref="MediaStream"/> object.</param>
 335        /// <param name="pathInfo">The <see cref="ExternalPathParserResult"/> object.</param>
 336        /// <returns>The modified mediaStream.</returns>
 337        private MediaStream MergeMetadata(MediaStream mediaStream, ExternalPathParserResult pathInfo)
 338        {
 19339            mediaStream.Path = pathInfo.Path;
 19340            mediaStream.IsExternal = true;
 19341            mediaStream.Title = string.IsNullOrEmpty(mediaStream.Title) ? (string.IsNullOrEmpty(pathInfo.Title) ? null :
 19342            mediaStream.Language = string.IsNullOrEmpty(mediaStream.Language) ? (string.IsNullOrEmpty(pathInfo.Language)
 343
 19344            return mediaStream;
 345        }
 346    }
 347}