mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-21 22:04:31 -04:00
New: Improve path validation when handling paths from different OSes
(cherry picked from commit 0321368cc392d7a0a488409bf6bd586ba45497af) Closes #2309
This commit is contained in:
@@ -31,7 +31,7 @@ namespace NzbDrone.Common.Extensions
|
||||
public static string CleanFilePath(this string path)
|
||||
{
|
||||
Ensure.That(path, () => path).IsNotNullOrWhiteSpace();
|
||||
Ensure.That(path, () => path).IsValidPath();
|
||||
Ensure.That(path, () => path).IsValidPath(PathValidationType.AnyOs);
|
||||
|
||||
var info = new FileInfo(path.Trim());
|
||||
return info.FullName.CleanFilePathBasic();
|
||||
@@ -40,7 +40,7 @@ namespace NzbDrone.Common.Extensions
|
||||
public static string CleanFilePathBasic(this string path)
|
||||
{
|
||||
//UNC
|
||||
if (OsInfo.IsWindows && path.StartsWith(@"\\"))
|
||||
if (!path.Contains('/') && path.StartsWith(@"\\"))
|
||||
{
|
||||
return path.TrimEnd('/', '\\', ' ');
|
||||
}
|
||||
@@ -138,24 +138,24 @@ namespace NzbDrone.Common.Extensions
|
||||
|
||||
private static readonly Regex WindowsPathWithDriveRegex = new Regex(@"^[a-zA-Z]:\\", RegexOptions.Compiled);
|
||||
|
||||
public static bool IsPathValid(this string path)
|
||||
public static bool IsPathValid(this string path, PathValidationType validationType)
|
||||
{
|
||||
if (path.ContainsInvalidPathChars() || string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (validationType == PathValidationType.AnyOs)
|
||||
{
|
||||
return IsPathValidForWindows(path) || IsPathValidForNonWindows(path);
|
||||
}
|
||||
|
||||
if (OsInfo.IsNotWindows)
|
||||
{
|
||||
return path.StartsWith(Path.DirectorySeparatorChar.ToString());
|
||||
return IsPathValidForNonWindows(path);
|
||||
}
|
||||
|
||||
if (path.StartsWith("\\") || WindowsPathWithDriveRegex.IsMatch(path))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return IsPathValidForWindows(path);
|
||||
}
|
||||
|
||||
public static bool ContainsInvalidPathChars(this string text)
|
||||
@@ -371,5 +371,15 @@ namespace NzbDrone.Common.Extensions
|
||||
{
|
||||
return Path.Combine(appFolderInfo.StartUpFolder, NLOG_CONFIG_FILE);
|
||||
}
|
||||
|
||||
private static bool IsPathValidForWindows(string path)
|
||||
{
|
||||
return path.StartsWith("\\") || WindowsPathWithDriveRegex.IsMatch(path);
|
||||
}
|
||||
|
||||
private static bool IsPathValidForNonWindows(string path)
|
||||
{
|
||||
return path.StartsWith("/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user