| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | | using Emby.Naming.Audio; |
| | 5 | | using Emby.Naming.Common; |
| | 6 | |
|
| | 7 | | namespace Emby.Naming.Video |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Resolve if file is extra for video. |
| | 11 | | /// </summary> |
| | 12 | | public static class ExtraRuleResolver |
| | 13 | | { |
| 2 | 14 | | private static readonly char[] _digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Attempts to resolve if file is extra. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="path">Path to file.</param> |
| | 20 | | /// <param name="namingOptions">The naming options.</param> |
| | 21 | | /// <param name="libraryRoot">Top-level folder for the containing library.</param> |
| | 22 | | /// <returns>Returns <see cref="ExtraResult"/> object.</returns> |
| | 23 | | public static ExtraResult GetExtraInfo(string path, NamingOptions namingOptions, string? libraryRoot = "") |
| | 24 | | { |
| 366 | 25 | | ExtraResult result = new ExtraResult(); |
| | 26 | |
|
| 366 | 27 | | bool isAudioFile = AudioFileParser.IsAudioFile(path, namingOptions); |
| 366 | 28 | | bool isVideoFile = VideoResolver.IsVideoFile(path, namingOptions); |
| | 29 | |
|
| 366 | 30 | | ReadOnlySpan<char> pathSpan = path.AsSpan(); |
| 366 | 31 | | ReadOnlySpan<char> fileName = Path.GetFileName(pathSpan); |
| 366 | 32 | | ReadOnlySpan<char> fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathSpan); |
| | 33 | | // Trim the digits from the end of the filename so we can recognize things like -trailer2 |
| 366 | 34 | | ReadOnlySpan<char> trimmedFileNameWithoutExtension = fileNameWithoutExtension.TrimEnd(_digits); |
| 366 | 35 | | ReadOnlySpan<char> directoryName = Path.GetFileName(Path.GetDirectoryName(pathSpan)); |
| 366 | 36 | | string fullDirectory = Path.GetDirectoryName(pathSpan).ToString(); |
| | 37 | |
|
| 20236 | 38 | | foreach (ExtraRule rule in namingOptions.VideoExtraRules) |
| | 39 | | { |
| 9824 | 40 | | if ((rule.MediaType == MediaType.Audio && !isAudioFile) |
| 9824 | 41 | | || (rule.MediaType == MediaType.Video && !isVideoFile)) |
| | 42 | | { |
| | 43 | | continue; |
| | 44 | | } |
| | 45 | |
|
| 8560 | 46 | | bool isMatch = rule.RuleType switch |
| 8560 | 47 | | { |
| 541 | 48 | | ExtraRuleType.Filename => fileNameWithoutExtension.Equals(rule.Token, StringComparison.OrdinalIgnore |
| 4023 | 49 | | ExtraRuleType.Suffix => trimmedFileNameWithoutExtension.EndsWith(rule.Token, StringComparison.Ordina |
| 1 | 50 | | ExtraRuleType.Regex => Regex.IsMatch(fileName, rule.Token, RegexOptions.IgnoreCase | RegexOptions.Co |
| 3995 | 51 | | ExtraRuleType.DirectoryName => directoryName.Equals(rule.Token, StringComparison.OrdinalIgnoreCase) |
| 3995 | 52 | | && !string.Equals(fullDirectory, libraryRoot, StringComparison.OrdinalI |
| 0 | 53 | | _ => false, |
| 8560 | 54 | | }; |
| | 55 | |
|
| 8560 | 56 | | if (!isMatch) |
| | 57 | | { |
| | 58 | | continue; |
| | 59 | | } |
| | 60 | |
|
| 144 | 61 | | result.ExtraType = rule.ExtraType; |
| 144 | 62 | | result.Rule = rule; |
| 144 | 63 | | return result; |
| | 64 | | } |
| | 65 | |
|
| 222 | 66 | | return result; |
| | 67 | | } |
| | 68 | | } |
| | 69 | | } |