jellyfin/Emby.Naming/Video/ExtraRuleResolver.cs

89 lines
3.3 KiB
C#
Raw Normal View History

using System;
2018-09-12 17:26:21 +00:00
using System.IO;
using System.Text.RegularExpressions;
2019-01-13 19:17:29 +00:00
using Emby.Naming.Audio;
using Emby.Naming.Common;
2018-09-12 17:26:21 +00:00
namespace Emby.Naming.Video
{
2020-11-10 18:23:10 +00:00
/// <summary>
/// Resolve if file is extra for video.
/// </summary>
2021-12-27 23:37:40 +00:00
public static class ExtraRuleResolver
2018-09-12 17:26:21 +00:00
{
2021-12-07 14:18:17 +00:00
private static readonly char[] _digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
2018-09-12 17:26:21 +00:00
2020-11-10 18:23:10 +00:00
/// <summary>
/// Attempts to resolve if file is extra.
/// </summary>
/// <param name="path">Path to file.</param>
2021-12-07 14:18:17 +00:00
/// <param name="namingOptions">The naming options.</param>
2020-11-10 18:23:10 +00:00
/// <returns>Returns <see cref="ExtraResult"/> object.</returns>
2021-12-07 14:18:17 +00:00
public static ExtraResult GetExtraInfo(string path, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
var result = new ExtraResult();
2021-12-07 14:18:17 +00:00
for (var i = 0; i < namingOptions.VideoExtraRules.Length; i++)
2018-09-12 17:26:21 +00:00
{
2021-12-07 14:18:17 +00:00
var rule = namingOptions.VideoExtraRules[i];
if ((rule.MediaType == MediaType.Audio && !AudioFileParser.IsAudioFile(path, namingOptions))
|| (rule.MediaType == MediaType.Video && !VideoResolver.IsVideoFile(path, namingOptions)))
2018-09-12 17:26:21 +00:00
{
2021-12-07 14:18:17 +00:00
continue;
2018-09-12 17:26:21 +00:00
}
2021-05-16 12:49:11 +00:00
var pathSpan = path.AsSpan();
if (rule.RuleType == ExtraRuleType.Filename)
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
var filename = Path.GetFileNameWithoutExtension(pathSpan);
2018-09-12 17:26:21 +00:00
2021-05-16 12:49:11 +00:00
if (filename.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
{
result.ExtraType = rule.ExtraType;
result.Rule = rule;
}
}
else if (rule.RuleType == ExtraRuleType.Suffix)
2018-09-12 17:26:21 +00:00
{
2021-10-05 19:47:59 +00:00
// Trim the digits from the end of the filename so we can recognize things like -trailer2
var filename = Path.GetFileNameWithoutExtension(pathSpan).TrimEnd(_digits);
2021-05-16 12:49:11 +00:00
2021-10-05 19:47:59 +00:00
if (filename.EndsWith(rule.Token, StringComparison.OrdinalIgnoreCase))
2021-05-16 12:49:11 +00:00
{
result.ExtraType = rule.ExtraType;
result.Rule = rule;
}
2018-09-12 17:26:21 +00:00
}
2021-05-16 12:49:11 +00:00
else if (rule.RuleType == ExtraRuleType.Regex)
{
var filename = Path.GetFileName(path);
2018-09-12 17:26:21 +00:00
2021-12-13 07:27:20 +00:00
var isMatch = Regex.IsMatch(filename, rule.Token, RegexOptions.IgnoreCase | RegexOptions.Compiled);
2018-09-12 17:26:21 +00:00
2021-12-13 07:27:20 +00:00
if (isMatch)
2021-05-16 12:49:11 +00:00
{
result.ExtraType = rule.ExtraType;
result.Rule = rule;
}
}
else if (rule.RuleType == ExtraRuleType.DirectoryName)
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
var directoryName = Path.GetFileName(Path.GetDirectoryName(pathSpan));
if (directoryName.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
{
result.ExtraType = rule.ExtraType;
result.Rule = rule;
}
2018-09-12 17:26:21 +00:00
}
2021-05-16 12:49:11 +00:00
if (result.ExtraType != null)
{
2021-05-16 12:49:11 +00:00
return result;
}
}
2018-09-12 17:26:21 +00:00
return result;
}
}
}