< Summary - Jellyfin

Information
Class: Emby.Naming.Video.VideoResolver
Assembly: Emby.Naming
File(s): /srv/git/jellyfin/Emby.Naming/Video/VideoResolver.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 157
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
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
ResolveDirectory(...)100%11100%
ResolveFile(...)100%11100%
Resolve(...)100%1414100%
IsVideoFile(...)100%11100%
IsStubFile(...)100%11100%
TryCleanString(...)100%11100%
CleanDateTime(...)100%11100%

File(s)

/srv/git/jellyfin/Emby.Naming/Video/VideoResolver.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics.CodeAnalysis;
 3using System.IO;
 4using Emby.Naming.Common;
 5using Jellyfin.Extensions;
 6
 7namespace Emby.Naming.Video
 8{
 9    /// <summary>
 10    /// Resolves <see cref="VideoFileInfo"/> from file path.
 11    /// </summary>
 12    public static class VideoResolver
 13    {
 14        /// <summary>
 15        /// Resolves the directory.
 16        /// </summary>
 17        /// <param name="path">The path.</param>
 18        /// <param name="namingOptions">The naming options.</param>
 19        /// <param name="parseName">Whether to parse the name or use the filename.</param>
 20        /// <returns>VideoFileInfo.</returns>
 21        public static VideoFileInfo? ResolveDirectory(string? path, NamingOptions namingOptions, bool parseName = true)
 22        {
 323            return Resolve(path, true, namingOptions, parseName);
 24        }
 25
 26        /// <summary>
 27        /// Resolves the file.
 28        /// </summary>
 29        /// <param name="path">The path.</param>
 30        /// <param name="namingOptions">The naming options.</param>
 31        /// <returns>VideoFileInfo.</returns>
 32        public static VideoFileInfo? ResolveFile(string? path, NamingOptions namingOptions)
 33        {
 1934            return Resolve(path, false, namingOptions);
 35        }
 36
 37        /// <summary>
 38        /// Resolves the specified path.
 39        /// </summary>
 40        /// <param name="path">The path.</param>
 41        /// <param name="isDirectory">if set to <c>true</c> [is folder].</param>
 42        /// <param name="namingOptions">The naming options.</param>
 43        /// <param name="parseName">Whether or not the name should be parsed for info.</param>
 44        /// <returns>VideoFileInfo.</returns>
 45        /// <exception cref="ArgumentNullException"><c>path</c> is <c>null</c>.</exception>
 46        public static VideoFileInfo? Resolve(string? path, bool isDirectory, NamingOptions namingOptions, bool parseName
 47        {
 22248            if (string.IsNullOrEmpty(path))
 49            {
 250                return null;
 51            }
 52
 22053            bool isStub = false;
 22054            ReadOnlySpan<char> container = ReadOnlySpan<char>.Empty;
 22055            string? stubType = null;
 56
 22057            if (!isDirectory)
 58            {
 21159                var extension = Path.GetExtension(path.AsSpan());
 60
 61                // Check supported extensions
 21162                if (!namingOptions.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
 63                {
 64                    // It's not supported. Check stub extensions
 665                    if (!StubResolver.TryResolveFile(path, namingOptions, out stubType))
 66                    {
 167                        return null;
 68                    }
 69
 570                    isStub = true;
 71                }
 72
 21073                container = extension.TrimStart('.');
 74            }
 75
 21976            var format3DResult = Format3DParser.Parse(path, namingOptions);
 77
 21978            var extraResult = ExtraRuleResolver.GetExtraInfo(path, namingOptions);
 79
 21980            var name = Path.GetFileNameWithoutExtension(path);
 81
 21982            int? year = null;
 83
 21984            if (parseName)
 85            {
 21086                var cleanDateTimeResult = CleanDateTime(name, namingOptions);
 21087                name = cleanDateTimeResult.Name;
 21088                year = cleanDateTimeResult.Year;
 89
 21090                if (TryCleanString(name, namingOptions, out var newName))
 91                {
 4792                    name = newName;
 93                }
 94            }
 95
 21996            return new VideoFileInfo(
 21997                path: path,
 21998                container: container.IsEmpty ? null : container.ToString(),
 21999                isStub: isStub,
 219100                name: name,
 219101                year: year,
 219102                stubType: stubType,
 219103                is3D: format3DResult.Is3D,
 219104                format3D: format3DResult.Format3D,
 219105                extraType: extraResult.ExtraType,
 219106                isDirectory: isDirectory,
 219107                extraRule: extraResult.Rule);
 108        }
 109
 110        /// <summary>
 111        /// Determines if path is video file based on extension.
 112        /// </summary>
 113        /// <param name="path">Path to file.</param>
 114        /// <param name="namingOptions">The naming options.</param>
 115        /// <returns>True if is video file.</returns>
 116        public static bool IsVideoFile(string path, NamingOptions namingOptions)
 117        {
 8470118            var extension = Path.GetExtension(path.AsSpan());
 8470119            return namingOptions.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
 120        }
 121
 122        /// <summary>
 123        /// Determines if path is video file stub based on extension.
 124        /// </summary>
 125        /// <param name="path">Path to file.</param>
 126        /// <param name="namingOptions">The naming options.</param>
 127        /// <returns>True if is video file stub.</returns>
 128        public static bool IsStubFile(string path, NamingOptions namingOptions)
 129        {
 1130            var extension = Path.GetExtension(path.AsSpan());
 1131            return namingOptions.StubFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
 132        }
 133
 134        /// <summary>
 135        /// Tries to clean name of clutter.
 136        /// </summary>
 137        /// <param name="name">Raw name.</param>
 138        /// <param name="namingOptions">The naming options.</param>
 139        /// <param name="newName">Clean name.</param>
 140        /// <returns>True if cleaning of name was successful.</returns>
 141        public static bool TryCleanString([NotNullWhen(true)] string? name, NamingOptions namingOptions, out string newN
 142        {
 237143            return CleanStringParser.TryClean(name, namingOptions.CleanStringRegexes, out newName);
 144        }
 145
 146        /// <summary>
 147        /// Tries to get name and year from raw name.
 148        /// </summary>
 149        /// <param name="name">Raw name.</param>
 150        /// <param name="namingOptions">The naming options.</param>
 151        /// <returns>Returns <see cref="CleanDateTimeResult"/> with name and optional year.</returns>
 152        public static CleanDateTimeResult CleanDateTime(string name, NamingOptions namingOptions)
 153        {
 252154            return CleanDateTimeParser.Clean(name, namingOptions.CleanDateTimeRegexes);
 155        }
 156    }
 157}