From f23d75d031042d3c4a7215483022b4efdd9c4376 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 3 Nov 2024 16:22:32 -0800 Subject: [PATCH] Fixed: Normalize unicode characters when comparing paths for equality (cherry picked from commit ceeec091f85d0094e07537b7f62f18292655a710) --- src/NzbDrone.Common.Test/PathExtensionFixture.cs | 10 ++++++++++ src/NzbDrone.Common/Extensions/PathExtensions.cs | 4 ++++ src/NzbDrone.Common/PathEqualityComparer.cs | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Common.Test/PathExtensionFixture.cs b/src/NzbDrone.Common.Test/PathExtensionFixture.cs index 1943e3a59..38ec414a5 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Text; using FluentAssertions; using Moq; using NUnit.Framework; @@ -316,5 +317,14 @@ public void GetAncestorFolders_should_return_all_ancestors_in_path_Linux() result[2].Should().Be(@"Music"); result[3].Should().Be(@"Artist Title"); } + + [Test] + public void should_be_equal_with_different_unicode_representations() + { + var path1 = @"C:\Test\file.mkv".AsOsAgnostic().Normalize(NormalizationForm.FormC); + var path2 = @"C:\Test\file.mkv".AsOsAgnostic().Normalize(NormalizationForm.FormD); + + path1.PathEquals(path2); + } } } diff --git a/src/NzbDrone.Common/Extensions/PathExtensions.cs b/src/NzbDrone.Common/Extensions/PathExtensions.cs index dad4cdc5f..917797540 100644 --- a/src/NzbDrone.Common/Extensions/PathExtensions.cs +++ b/src/NzbDrone.Common/Extensions/PathExtensions.cs @@ -59,6 +59,10 @@ public static bool PathNotEquals(this string firstPath, string secondPath, Strin public static bool PathEquals(this string firstPath, string secondPath, StringComparison? comparison = null) { + // Normalize paths to ensure unicode characters are represented the same way + firstPath = firstPath.Normalize(); + secondPath = secondPath?.Normalize(); + if (!comparison.HasValue) { comparison = DiskProviderBase.PathStringComparison; diff --git a/src/NzbDrone.Common/PathEqualityComparer.cs b/src/NzbDrone.Common/PathEqualityComparer.cs index 5b9c3aa1c..bd6fa430d 100644 --- a/src/NzbDrone.Common/PathEqualityComparer.cs +++ b/src/NzbDrone.Common/PathEqualityComparer.cs @@ -21,10 +21,10 @@ public int GetHashCode(string obj) { if (OsInfo.IsWindows) { - return obj.CleanFilePath().ToLower().GetHashCode(); + return obj.CleanFilePath().Normalize().ToLower().GetHashCode(); } - return obj.CleanFilePath().GetHashCode(); + return obj.CleanFilePath().Normalize().GetHashCode(); } } }