aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming/Video/ExtraRuleResolver.cs
blob: 5289065898cfd5c3d17e80d52e14573f4afe2b80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.IO;
using System.Text.RegularExpressions;
using Emby.Naming.Audio;
using Emby.Naming.Common;

namespace Emby.Naming.Video
{
    /// <summary>
    /// Resolve if file is extra for video.
    /// </summary>
    public static class ExtraRuleResolver
    {
        private static readonly char[] _digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        /// <summary>
        /// Attempts to resolve if file is extra.
        /// </summary>
        /// <param name="path">Path to file.</param>
        /// <param name="namingOptions">The naming options.</param>
        /// <param name="libraryRoot">Top-level folder for the containing library.</param>
        /// <returns>Returns <see cref="ExtraResult"/> object.</returns>
        public static ExtraResult GetExtraInfo(string path, NamingOptions namingOptions, string? libraryRoot = "")
        {
            var result = new ExtraResult();

            for (var i = 0; i < namingOptions.VideoExtraRules.Length; i++)
            {
                var rule = namingOptions.VideoExtraRules[i];
                if ((rule.MediaType == MediaType.Audio && !AudioFileParser.IsAudioFile(path, namingOptions))
                    || (rule.MediaType == MediaType.Video && !VideoResolver.IsVideoFile(path, namingOptions)))
                {
                    continue;
                }

                var pathSpan = path.AsSpan();
                if (rule.RuleType == ExtraRuleType.Filename)
                {
                    var filename = Path.GetFileNameWithoutExtension(pathSpan);

                    if (filename.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
                    {
                        result.ExtraType = rule.ExtraType;
                        result.Rule = rule;
                    }
                }
                else if (rule.RuleType == ExtraRuleType.Suffix)
                {
                    // Trim the digits from the end of the filename so we can recognize things like -trailer2
                    var filename = Path.GetFileNameWithoutExtension(pathSpan).TrimEnd(_digits);

                    if (filename.EndsWith(rule.Token, StringComparison.OrdinalIgnoreCase))
                    {
                        result.ExtraType = rule.ExtraType;
                        result.Rule = rule;
                    }
                }
                else if (rule.RuleType == ExtraRuleType.Regex)
                {
                    var filename = Path.GetFileName(path.AsSpan());

                    var isMatch = Regex.IsMatch(filename, rule.Token, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (isMatch)
                    {
                        result.ExtraType = rule.ExtraType;
                        result.Rule = rule;
                    }
                }
                else if (rule.RuleType == ExtraRuleType.DirectoryName)
                {
                    var directoryName = Path.GetFileName(Path.GetDirectoryName(pathSpan));
                    string fullDirectory = Path.GetDirectoryName(pathSpan).ToString();
                    if (directoryName.Equals(rule.Token, StringComparison.OrdinalIgnoreCase)
                        && !string.Equals(fullDirectory, libraryRoot, StringComparison.OrdinalIgnoreCase))
                    {
                        result.ExtraType = rule.ExtraType;
                        result.Rule = rule;
                    }
                }

                if (result.ExtraType is not null)
                {
                    return result;
                }
            }

            return result;
        }
    }
}