Strip out external file fuzzy matching

Convert MediaFlagDelimiter back to char
This commit is contained in:
Joe Rogers 2022-03-04 10:52:15 +01:00
parent 136eab9b1e
commit 3205e97e1e
No known key found for this signature in database
GPG Key ID: 0074AD57B8FDBBB4
4 changed files with 19 additions and 28 deletions

View File

@ -266,7 +266,7 @@ namespace Emby.Naming.Common
MediaFlagDelimiters = new[] MediaFlagDelimiters = new[]
{ {
"." '.'
}; };
MediaForcedFlags = new[] MediaForcedFlags = new[]
@ -715,7 +715,7 @@ namespace Emby.Naming.Common
/// <summary> /// <summary>
/// Gets or sets list of external media flag delimiters. /// Gets or sets list of external media flag delimiters.
/// </summary> /// </summary>
public string[] MediaFlagDelimiters { get; set; } public char[] MediaFlagDelimiters { get; set; }
/// <summary> /// <summary>
/// Gets or sets list of external media forced flags. /// Gets or sets list of external media forced flags.

View File

@ -61,11 +61,11 @@ namespace Emby.Naming.ExternalFiles
{ {
var languageString = extraString; var languageString = extraString;
var titleString = string.Empty; var titleString = string.Empty;
int separatorLength = separator.Length; const int SeparatorLength = 1;
while (languageString.Length > 0) while (languageString.Length > 0)
{ {
int lastSeparator = languageString.LastIndexOf(separator, StringComparison.OrdinalIgnoreCase); int lastSeparator = languageString.LastIndexOf(separator);
if (lastSeparator == -1) if (lastSeparator == -1)
{ {
@ -73,7 +73,7 @@ namespace Emby.Naming.ExternalFiles
} }
string currentSlice = languageString[lastSeparator..]; string currentSlice = languageString[lastSeparator..];
string currentSliceWithoutSeparator = currentSlice[separatorLength..]; string currentSliceWithoutSeparator = currentSlice[SeparatorLength..];
if (_namingOptions.MediaDefaultFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringComparison.OrdinalIgnoreCase))) if (_namingOptions.MediaDefaultFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringComparison.OrdinalIgnoreCase)))
{ {
@ -107,7 +107,7 @@ namespace Emby.Naming.ExternalFiles
languageString = languageString[..lastSeparator]; languageString = languageString[..lastSeparator];
} }
pathInfo.Title = separatorLength <= titleString.Length ? titleString[separatorLength..] : null; pathInfo.Title = titleString.Length >= SeparatorLength ? titleString[SeparatorLength..] : null;
} }
return pathInfo; return pathInfo;

View File

@ -24,16 +24,6 @@ namespace MediaBrowser.Providers.MediaInfo
/// </summary> /// </summary>
public abstract class MediaInfoResolver public abstract class MediaInfoResolver
{ {
/// <summary>
/// The <see cref="CompareOptions"/> instance.
/// </summary>
private const CompareOptions CompareOptions = System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace | System.Globalization.CompareOptions.IgnoreSymbols;
/// <summary>
/// The <see cref="CompareInfo"/> instance.
/// </summary>
private readonly CompareInfo _compareInfo = CultureInfo.InvariantCulture.CompareInfo;
/// <summary> /// <summary>
/// The <see cref="ExternalPathParser"/> instance. /// The <see cref="ExternalPathParser"/> instance.
/// </summary> /// </summary>
@ -175,11 +165,12 @@ namespace MediaBrowser.Providers.MediaInfo
foreach (var file in files) foreach (var file in files)
{ {
var prefixLength = video.FileNameWithoutExtension.Length;
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
if (_compareInfo.IsPrefix(fileNameWithoutExtension, video.FileNameWithoutExtension, CompareOptions, out int matchLength) if (video.FileNameWithoutExtension.Equals(fileNameWithoutExtension[..prefixLength], StringComparison.OrdinalIgnoreCase)
&& (fileNameWithoutExtension.Length == matchLength || _namingOptions.MediaFlagDelimiters.Contains(fileNameWithoutExtension[matchLength].ToString()))) && (fileNameWithoutExtension.Length == prefixLength || _namingOptions.MediaFlagDelimiters.Contains(fileNameWithoutExtension[prefixLength])))
{ {
var externalPathInfo = _externalPathParser.ParseFile(file, fileNameWithoutExtension[matchLength..]); var externalPathInfo = _externalPathParser.ParseFile(file, fileNameWithoutExtension[prefixLength..]);
if (externalPathInfo != null) if (externalPathInfo != null)
{ {

View File

@ -133,18 +133,18 @@ public class MediaInfoResolverTests
} }
[Theory] [Theory]
[InlineData("My.Video.srt", null)] // exact [InlineData("My.Video.mkv", "My.Video.srt", null)]
[InlineData("My.Video.en.srt", "eng")] [InlineData("My.Video.mkv", "My.Video.en.srt", "eng")]
[InlineData("MyVideo.en.srt", "eng")] // shorter title [InlineData("My.Video.mkv", "My.Video.en.srt", "eng", true)]
[InlineData("My _ Video.en.srt", "eng")] // longer title [InlineData("Example Movie (2021).mp4", "Example Movie (2021).English.Srt", "eng")]
[InlineData("My.Video.en.srt", "eng", true)] [InlineData("[LTDB] Who Framed Roger Rabbit (1998) - [Bluray-1080p].mkv", "[LTDB] Who Framed Roger Rabbit (1998) - [Bluray-1080p].en.srt", "eng")]
public void GetExternalFiles_FuzzyMatching_MatchesAndParsesToken(string file, string? language, bool metadataDirectory = false) public void GetExternalFiles_NameMatching_MatchesAndParsesToken(string movie, string file, string? language, bool metadataDirectory = false)
{ {
BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>(); BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
var video = new Movie var video = new Movie
{ {
Path = VideoDirectoryPath + "/My.Video.mkv" Path = VideoDirectoryPath + "/" + movie
}; };
var directoryService = GetDirectoryServiceForExternalFile(file, metadataDirectory); var directoryService = GetDirectoryServiceForExternalFile(file, metadataDirectory);
@ -162,7 +162,7 @@ public class MediaInfoResolverTests
[InlineData("My.Video.txt")] [InlineData("My.Video.txt")]
[InlineData("My.Video Sequel.srt")] [InlineData("My.Video Sequel.srt")]
[InlineData("Some.Other.Video.srt")] [InlineData("Some.Other.Video.srt")]
public void GetExternalFiles_FuzzyMatching_RejectsNonMatches(string file) public void GetExternalFiles_NameMatching_RejectsNonMatches(string file)
{ {
BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>(); BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
@ -344,7 +344,7 @@ public class MediaInfoResolverTests
var files = new string[fileCount]; var files = new string[fileCount];
for (int i = 0; i < fileCount; i++) for (int i = 0; i < fileCount; i++)
{ {
files[i] = $"{VideoDirectoryPath}/MyVideo.{i}.srt"; files[i] = $"{VideoDirectoryPath}/My.Video.{i}.srt";
} }
var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict); var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);