fixup! New: Validate that folders in paths don't start or end with a space

Closes #6709
This commit is contained in:
Mark McDowall 2024-04-20 10:34:32 -07:00
parent 3b4a2c2653
commit acf0aac491
2 changed files with 9 additions and 3 deletions

View File

@ -35,7 +35,7 @@ namespace NzbDrone.Common.Test
[TestCase(@"\\Testserver\\Test\", @"\\Testserver\Test")] [TestCase(@"\\Testserver\\Test\", @"\\Testserver\Test")]
[TestCase(@"\\Testserver\Test\file.ext", @"\\Testserver\Test\file.ext")] [TestCase(@"\\Testserver\Test\file.ext", @"\\Testserver\Test\file.ext")]
[TestCase(@"\\Testserver\Test\file.ext\\", @"\\Testserver\Test\file.ext")] [TestCase(@"\\Testserver\Test\file.ext\\", @"\\Testserver\Test\file.ext")]
[TestCase(@"\\Testserver\Test\file.ext \\", @"\\Testserver\Test\file.ext")] [TestCase(@"\\Testserver\Test\file.ext ", @"\\Testserver\Test\file.ext")]
[TestCase(@"//CAPITAL//lower// ", @"\\CAPITAL\lower")] [TestCase(@"//CAPITAL//lower// ", @"\\CAPITAL\lower")]
public void Clean_Path_Windows(string dirty, string clean) public void Clean_Path_Windows(string dirty, string clean)
{ {

View File

@ -29,6 +29,12 @@ namespace NzbDrone.Common.Extensions
public static string CleanFilePath(this string path) public static string CleanFilePath(this string path)
{ {
if (path.IsNotNullOrWhiteSpace())
{
// Trim trailing spaces before checking if the path is valid so validation doesn't fail for something we can fix.
path = path.TrimEnd(' ');
}
Ensure.That(path, () => path).IsNotNullOrWhiteSpace(); Ensure.That(path, () => path).IsNotNullOrWhiteSpace();
Ensure.That(path, () => path).IsValidPath(PathValidationType.AnyOs); Ensure.That(path, () => path).IsValidPath(PathValidationType.AnyOs);
@ -37,10 +43,10 @@ namespace NzbDrone.Common.Extensions
// UNC // UNC
if (!info.FullName.Contains('/') && info.FullName.StartsWith(@"\\")) if (!info.FullName.Contains('/') && info.FullName.StartsWith(@"\\"))
{ {
return info.FullName.TrimEnd('/', '\\', ' '); return info.FullName.TrimEnd('/', '\\');
} }
return info.FullName.TrimEnd('/').Trim('\\', ' '); return info.FullName.TrimEnd('/').Trim('\\');
} }
public static bool PathNotEquals(this string firstPath, string secondPath, StringComparison? comparison = null) public static bool PathNotEquals(this string firstPath, string secondPath, StringComparison? comparison = null)