aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Library/Resolvers/ExtraResolver.cs
blob: 30c52e19d3a309a16053b1dc30fd92638016d732 (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
92
93
94
95
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Emby.Naming.Common;
using Emby.Naming.Video;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;
using static Emby.Naming.Video.ExtraRuleResolver;

namespace Emby.Server.Implementations.Library.Resolvers
{
    /// <summary>
    /// Resolves a Path into a Video or Video subclass.
    /// </summary>
    internal class ExtraResolver
    {
        private readonly NamingOptions _namingOptions;
        private readonly IItemResolver[] _trailerResolvers;
        private readonly IItemResolver[] _videoResolvers;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExtraResolver"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="namingOptions">An instance of <see cref="NamingOptions"/>.</param>
        public ExtraResolver(ILogger<ExtraResolver> logger, NamingOptions namingOptions)
        {
            _namingOptions = namingOptions;
            _trailerResolvers = new IItemResolver[] { new GenericVideoResolver<Trailer>(logger, namingOptions) };
            _videoResolvers = new IItemResolver[] { new GenericVideoResolver<Video>(logger, namingOptions) };
        }

        /// <summary>
        /// Gets the resolvers for the extra type.
        /// </summary>
        /// <param name="extraType">The extra type.</param>
        /// <returns>The resolvers for the extra type.</returns>
        public IItemResolver[]? GetResolversForExtraType(ExtraType extraType) => extraType switch
        {
            ExtraType.Trailer => _trailerResolvers,
            // For audio we'll have to rely on the AudioResolver, which is a "built-in"
            ExtraType.ThemeSong => null,
            _ => _videoResolvers
        };

        public bool TryGetExtraTypeForOwner(string path, VideoFileInfo ownerVideoFileInfo, [NotNullWhen(true)] out ExtraType? extraType)
        {
            var extraResult = GetExtraInfo(path, _namingOptions);
            if (extraResult.ExtraType is null)
            {
                extraType = null;
                return false;
            }

            var cleanDateTimeResult = CleanDateTimeParser.Clean(Path.GetFileNameWithoutExtension(path), _namingOptions.CleanDateTimeRegexes);
            var name = cleanDateTimeResult.Name;
            var year = cleanDateTimeResult.Year;

            var parentDir = ownerVideoFileInfo.IsDirectory ? ownerVideoFileInfo.Path : Path.GetDirectoryName(ownerVideoFileInfo.Path.AsSpan());

            var trimmedFileNameWithoutExtension = TrimFilenameDelimiters(ownerVideoFileInfo.FileNameWithoutExtension, _namingOptions.VideoFlagDelimiters);
            var trimmedVideoInfoName = TrimFilenameDelimiters(ownerVideoFileInfo.Name, _namingOptions.VideoFlagDelimiters);
            var trimmedExtraFileName = TrimFilenameDelimiters(name, _namingOptions.VideoFlagDelimiters);

            // first check filenames
            bool isValid = StartsWith(trimmedExtraFileName, trimmedFileNameWithoutExtension)
                           || (StartsWith(trimmedExtraFileName, trimmedVideoInfoName) && year == ownerVideoFileInfo.Year);

            if (!isValid)
            {
                // When the extra rule type is DirectoryName we must go one level higher to get the "real" dir name
                var currentParentDir = extraResult.Rule?.RuleType == ExtraRuleType.DirectoryName
                    ? Path.GetDirectoryName(Path.GetDirectoryName(path.AsSpan()))
                    : Path.GetDirectoryName(path.AsSpan());

                isValid = !currentParentDir.IsEmpty && !parentDir.IsEmpty && currentParentDir.Equals(parentDir, StringComparison.OrdinalIgnoreCase);
            }

            extraType = extraResult.ExtraType;
            return isValid;
        }

        private static ReadOnlySpan<char> TrimFilenameDelimiters(ReadOnlySpan<char> name, ReadOnlySpan<char> videoFlagDelimiters)
        {
            return name.IsEmpty ? name : name.TrimEnd().TrimEnd(videoFlagDelimiters).TrimEnd();
        }

        private static bool StartsWith(ReadOnlySpan<char> fileName, ReadOnlySpan<char> baseName)
        {
            return !baseName.IsEmpty && fileName.StartsWith(baseName, StringComparison.OrdinalIgnoreCase);
        }
    }
}