2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2021-12-07 09:18:17 -05:00
|
|
|
using System.Collections.Generic;
|
2018-09-12 13:26:21 -04:00
|
|
|
using System.IO;
|
2021-12-07 09:18:17 -05:00
|
|
|
using System.Linq;
|
2020-11-12 07:16:33 -05:00
|
|
|
using System.Text.RegularExpressions;
|
2019-01-13 14:17:29 -05:00
|
|
|
using Emby.Naming.Audio;
|
|
|
|
using Emby.Naming.Common;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
namespace Emby.Naming.Video
|
|
|
|
{
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Resolve if file is extra for video.
|
|
|
|
/// </summary>
|
2021-12-07 09:18:17 -05:00
|
|
|
public static class ExtraResolver
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-12-07 09:18:17 -05:00
|
|
|
private static readonly char[] _digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Attempts to resolve if file is extra.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">Path to file.</param>
|
2021-12-07 09:18:17 -05:00
|
|
|
/// <param name="namingOptions">The naming options.</param>
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <returns>Returns <see cref="ExtraResult"/> object.</returns>
|
2021-12-07 09:18:17 -05:00
|
|
|
public static ExtraResult GetExtraInfo(string path, NamingOptions namingOptions)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
var result = new ExtraResult();
|
|
|
|
|
2021-12-07 09:18:17 -05:00
|
|
|
for (var i = 0; i < namingOptions.VideoExtraRules.Length; i++)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-12-07 09:18:17 -05: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 13:26:21 -04:00
|
|
|
{
|
2021-12-07 09:18:17 -05:00
|
|
|
continue;
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
2021-05-16 08:49:11 -04:00
|
|
|
var pathSpan = path.AsSpan();
|
|
|
|
if (rule.RuleType == ExtraRuleType.Filename)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-05-16 08:49:11 -04:00
|
|
|
var filename = Path.GetFileNameWithoutExtension(pathSpan);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2021-05-16 08:49:11 -04:00
|
|
|
if (filename.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
result.ExtraType = rule.ExtraType;
|
|
|
|
result.Rule = rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rule.RuleType == ExtraRuleType.Suffix)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-10-05 15:47:59 -04: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 08:49:11 -04:00
|
|
|
|
2021-10-05 15:47:59 -04:00
|
|
|
if (filename.EndsWith(rule.Token, StringComparison.OrdinalIgnoreCase))
|
2021-05-16 08:49:11 -04:00
|
|
|
{
|
|
|
|
result.ExtraType = rule.ExtraType;
|
|
|
|
result.Rule = rule;
|
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2021-05-16 08:49:11 -04:00
|
|
|
else if (rule.RuleType == ExtraRuleType.Regex)
|
|
|
|
{
|
|
|
|
var filename = Path.GetFileName(path);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2021-12-13 02:27:20 -05:00
|
|
|
var isMatch = Regex.IsMatch(filename, rule.Token, RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2021-12-13 02:27:20 -05:00
|
|
|
if (isMatch)
|
2021-05-16 08:49:11 -04:00
|
|
|
{
|
|
|
|
result.ExtraType = rule.ExtraType;
|
|
|
|
result.Rule = rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rule.RuleType == ExtraRuleType.DirectoryName)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-05-16 08:49:11 -04: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 13:26:21 -04:00
|
|
|
}
|
2021-05-16 08:49:11 -04:00
|
|
|
|
|
|
|
if (result.ExtraType != null)
|
2020-04-01 12:53:19 -04:00
|
|
|
{
|
2021-05-16 08:49:11 -04:00
|
|
|
return result;
|
2020-04-01 12:53:19 -04:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-12-07 09:18:17 -05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Finds extras matching the video info.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="files">The list of file video infos.</param>
|
|
|
|
/// <param name="videoInfo">The video to compare against.</param>
|
|
|
|
/// <param name="videoFlagDelimiters">The video flag delimiters.</param>
|
|
|
|
/// <returns>A list of video extras for [videoInfo].</returns>
|
|
|
|
public static IReadOnlyList<VideoFileInfo> GetExtras(IReadOnlyList<VideoInfo> files, VideoFileInfo videoInfo, ReadOnlySpan<char> videoFlagDelimiters)
|
|
|
|
{
|
|
|
|
var parentDir = videoInfo.IsDirectory ? videoInfo.Path : Path.GetDirectoryName(videoInfo.Path.AsSpan());
|
|
|
|
|
|
|
|
var trimmedFileNameWithoutExtension = TrimFilenameDelimiters(videoInfo.FileNameWithoutExtension, videoFlagDelimiters);
|
|
|
|
var trimmedVideoInfoName = TrimFilenameDelimiters(videoInfo.Name, videoFlagDelimiters);
|
|
|
|
|
|
|
|
var result = new List<VideoFileInfo>();
|
|
|
|
for (var pos = files.Count - 1; pos >= 0; pos--)
|
|
|
|
{
|
|
|
|
var current = files[pos];
|
|
|
|
// ignore non-extras and multi-file (can this happen?)
|
|
|
|
if (current.ExtraType == null || current.Files.Count > 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:22:17 -05:00
|
|
|
var currentFile = current.Files[0];
|
2021-12-07 09:18:17 -05:00
|
|
|
var trimmedCurrentFileName = TrimFilenameDelimiters(currentFile.Name, videoFlagDelimiters);
|
|
|
|
|
|
|
|
// first check filenames
|
|
|
|
bool isValid = StartsWith(trimmedCurrentFileName, trimmedFileNameWithoutExtension)
|
|
|
|
|| (StartsWith(trimmedCurrentFileName, trimmedVideoInfoName) && currentFile.Year == videoInfo.Year);
|
|
|
|
|
|
|
|
// then by directory
|
|
|
|
if (!isValid)
|
|
|
|
{
|
|
|
|
// When the extra rule type is DirectoryName we must go one level higher to get the "real" dir name
|
|
|
|
var currentParentDir = currentFile.ExtraRule?.RuleType == ExtraRuleType.DirectoryName
|
|
|
|
? Path.GetDirectoryName(Path.GetDirectoryName(currentFile.Path.AsSpan()))
|
|
|
|
: Path.GetDirectoryName(currentFile.Path.AsSpan());
|
|
|
|
|
|
|
|
isValid = !currentParentDir.IsEmpty && !parentDir.IsEmpty && currentParentDir.Equals(parentDir, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isValid)
|
|
|
|
{
|
|
|
|
result.Add(currentFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.OrderBy(r => r.Path).ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
}
|