< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.Resolvers.BaseVideoResolver<T>
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs
Line coverage
35%
Covered lines: 38
Uncovered lines: 68
Coverable lines: 106
Total lines: 289
Line coverage: 35.8%
Branch coverage
21%
Covered branches: 14
Total branches: 66
Branch coverage: 21.2%
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%
Resolve(...)100%11100%
ResolveVideo(...)31.81%136.852238.09%
SetVideoType(...)40%12.331071.42%
SetIsoType(...)8.33%108.471212.5%
Set3DFormat(...)11.11%245.561811.11%
Set3DFormat(...)100%11100%
Set3DFormat(...)100%210%
IsDvdDirectory(...)0%2040%
IsDvdFile(...)100%210%
IsBluRayDirectory(...)100%210%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs

#LineLine coverage
 1#nullable disable
 2
 3#pragma warning disable CS1591
 4
 5using System;
 6using System.IO;
 7using System.Linq;
 8using DiscUtils.Udf;
 9using Emby.Naming.Common;
 10using Emby.Naming.Video;
 11using MediaBrowser.Controller.Entities;
 12using MediaBrowser.Controller.Library;
 13using MediaBrowser.Controller.Providers;
 14using MediaBrowser.Model.Entities;
 15using Microsoft.Extensions.Logging;
 16
 17namespace Emby.Server.Implementations.Library.Resolvers
 18{
 19    /// <summary>
 20    /// Resolves a Path into a Video or Video subclass.
 21    /// </summary>
 22    /// <typeparam name="T">The type of item to resolve.</typeparam>
 23    public abstract class BaseVideoResolver<T> : MediaBrowser.Controller.Resolvers.ItemResolver<T>
 24        where T : Video, new()
 25    {
 26        private readonly ILogger _logger;
 27
 10528        protected BaseVideoResolver(ILogger logger, NamingOptions namingOptions, IDirectoryService directoryService)
 29        {
 10530            _logger = logger;
 10531            NamingOptions = namingOptions;
 10532            DirectoryService = directoryService;
 10533        }
 34
 35        protected NamingOptions NamingOptions { get; }
 36
 37        protected IDirectoryService DirectoryService { get; }
 38
 39        /// <summary>
 40        /// Resolves the specified args.
 41        /// </summary>
 42        /// <param name="args">The args.</param>
 43        /// <returns>`0.</returns>
 44        protected override T Resolve(ItemResolveArgs args)
 45        {
 846            return ResolveVideo<T>(args, false);
 47        }
 48
 49        /// <summary>
 50        /// Resolves the video.
 51        /// </summary>
 52        /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
 53        /// <param name="args">The args.</param>
 54        /// <param name="parseName">if set to <c>true</c> [parse name].</param>
 55        /// <returns>``0.</returns>
 56        protected virtual TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
 57              where TVideoType : Video, new()
 58        {
 1759            VideoFileInfo videoInfo = null;
 1760            VideoType? videoType = null;
 61
 62            // If the path is a file check for a matching extensions
 1763            if (args.IsDirectory)
 64            {
 65                // Loop through each child file/folder and see if we find a video
 066                foreach (var child in args.FileSystemChildren)
 67                {
 068                    var filename = child.Name;
 069                    if (child.IsDirectory)
 70                    {
 071                        if (IsDvdDirectory(child.FullName, filename, DirectoryService))
 72                        {
 073                            var videoTmp = new TVideoType
 074                            {
 075                                Path = args.Path,
 076                                VideoType = VideoType.Dvd
 077                            };
 078                            Set3DFormat(videoTmp);
 079                            return videoTmp;
 80                        }
 81
 082                        if (IsBluRayDirectory(filename))
 83                        {
 084                            var videoTmp = new TVideoType
 085                            {
 086                                Path = args.Path,
 087                                VideoType = VideoType.BluRay
 088                            };
 089                            Set3DFormat(videoTmp);
 090                            return videoTmp;
 91                        }
 92                    }
 093                    else if (IsDvdFile(filename))
 94                    {
 095                        videoType = VideoType.Dvd;
 96                    }
 97
 098                    if (videoType is null)
 99                    {
 100                        continue;
 101                    }
 102
 0103                    videoInfo = VideoResolver.ResolveDirectory(args.Path, NamingOptions, parseName);
 0104                    break;
 105                }
 106            }
 107            else
 108            {
 17109                videoInfo = VideoResolver.Resolve(args.Path, false, NamingOptions, parseName);
 110            }
 111
 17112            if (videoInfo is null || (!videoInfo.IsStub && !VideoResolver.IsVideoFile(args.Path, NamingOptions)))
 113            {
 0114                return null;
 115            }
 116
 17117            var video = new TVideoType
 17118            {
 17119                Name = videoInfo.Name,
 17120                Path = args.Path,
 17121                ProductionYear = videoInfo.Year,
 17122                ExtraType = videoInfo.ExtraType
 17123            };
 124
 17125            if (videoType.HasValue)
 126            {
 0127                video.VideoType = videoType.Value;
 128            }
 129            else
 130            {
 17131                SetVideoType(video, videoInfo);
 132            }
 133
 17134            Set3DFormat(video, videoInfo);
 135
 17136            return video;
 137        }
 138
 139        protected void SetVideoType(Video video, VideoFileInfo videoInfo)
 140        {
 17141            var extension = Path.GetExtension(video.Path.AsSpan());
 17142            video.VideoType = extension.Equals(".iso", StringComparison.OrdinalIgnoreCase)
 17143                              || extension.Equals(".img", StringComparison.OrdinalIgnoreCase)
 17144                ? VideoType.Iso
 17145                : VideoType.VideoFile;
 146
 17147            video.IsShortcut = extension.Equals(".strm", StringComparison.OrdinalIgnoreCase);
 17148            video.IsPlaceHolder = videoInfo.IsStub;
 149
 17150            if (videoInfo.IsStub)
 151            {
 0152                if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
 153                {
 0154                    video.VideoType = VideoType.Dvd;
 155                }
 0156                else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
 157                {
 0158                    video.VideoType = VideoType.BluRay;
 159                }
 160            }
 161
 17162            SetIsoType(video);
 17163        }
 164
 165        protected void SetIsoType(Video video)
 166        {
 17167            if (video.VideoType == VideoType.Iso)
 168            {
 0169                if (video.Path.Contains("dvd", StringComparison.OrdinalIgnoreCase))
 170                {
 0171                    video.IsoType = IsoType.Dvd;
 172                }
 0173                else if (video.Path.Contains("bluray", StringComparison.OrdinalIgnoreCase))
 174                {
 0175                    video.IsoType = IsoType.BluRay;
 176                }
 177                else
 178                {
 179                    try
 180                    {
 181                        // use disc-utils, both DVDs and BDs use UDF filesystem
 0182                        using var videoFileStream = File.Open(video.Path, FileMode.Open, FileAccess.Read, FileShare.Read
 0183                        using UdfReader udfReader = new UdfReader(videoFileStream);
 0184                        if (udfReader.DirectoryExists("VIDEO_TS"))
 185                        {
 0186                            video.IsoType = IsoType.Dvd;
 187                        }
 0188                        else if (udfReader.DirectoryExists("BDMV"))
 189                        {
 0190                            video.IsoType = IsoType.BluRay;
 191                        }
 0192                    }
 0193                    catch (Exception ex)
 194                    {
 0195                        _logger.LogError(ex, "Error opening UDF/ISO image: {Value}", video.Path ?? video.Name);
 0196                    }
 197                }
 198            }
 17199        }
 200
 201        protected void Set3DFormat(Video video, bool is3D, string format3D)
 202        {
 17203            if (is3D)
 204            {
 0205                if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
 206                {
 0207                    video.Video3DFormat = Video3DFormat.FullSideBySide;
 208                }
 0209                else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
 210                {
 0211                    video.Video3DFormat = Video3DFormat.FullTopAndBottom;
 212                }
 0213                else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
 214                {
 0215                    video.Video3DFormat = Video3DFormat.HalfSideBySide;
 216                }
 0217                else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
 218                {
 0219                    video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
 220                }
 0221                else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
 222                {
 0223                    video.Video3DFormat = Video3DFormat.HalfSideBySide;
 224                }
 0225                else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
 226                {
 0227                    video.Video3DFormat = Video3DFormat.HalfSideBySide;
 228                }
 0229                else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
 230                {
 0231                    video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
 232                }
 0233                else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
 234                {
 0235                    video.Video3DFormat = Video3DFormat.MVC;
 236                }
 237            }
 17238        }
 239
 240        protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
 241        {
 17242            Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
 17243        }
 244
 245        protected void Set3DFormat(Video video)
 246        {
 0247            var result = Format3DParser.Parse(video.Path, NamingOptions);
 248
 0249            Set3DFormat(video, result.Is3D, result.Format3D);
 0250        }
 251
 252        /// <summary>
 253        /// Determines whether [is DVD directory] [the specified directory name].
 254        /// </summary>
 255        /// <param name="fullPath">The full path of the directory.</param>
 256        /// <param name="directoryName">The name of the directory.</param>
 257        /// <param name="directoryService">The directory service.</param>
 258        /// <returns><c>true</c> if the provided directory is a DVD directory, <c>false</c> otherwise.</returns>
 259        protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
 260        {
 0261            if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
 262            {
 0263                return false;
 264            }
 265
 0266            return directoryService.GetFilePaths(fullPath).Any(i => Path.GetExtension(i.AsSpan()).Equals(".vob", StringC
 267        }
 268
 269        /// <summary>
 270        /// Determines whether [is DVD file] [the specified name].
 271        /// </summary>
 272        /// <param name="name">The name.</param>
 273        /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
 274        protected bool IsDvdFile(string name)
 275        {
 0276            return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
 277        }
 278
 279        /// <summary>
 280        /// Determines whether [is bluray directory] [the specified directory name].
 281        /// </summary>
 282        /// <param name="directoryName">The directory name.</param>
 283        /// <returns>Whether the directory is a bluray directory.</returns>
 284        protected bool IsBluRayDirectory(string directoryName)
 285        {
 0286            return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
 287        }
 288    }
 289}