1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 07:52:37 +01:00

cleaned up path validation logic.

This commit is contained in:
kay.one 2013-08-31 13:43:35 -07:00
parent bf415563d4
commit 9e08dffa58

View File

@ -46,31 +46,28 @@ namespace NzbDrone.Common
return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), StringComparison.InvariantCultureIgnoreCase); return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), StringComparison.InvariantCultureIgnoreCase);
} }
private static readonly Regex WindowsInvalidPathRegex = new Regex(@"[/*<>""|]", RegexOptions.Compiled); private static readonly Regex WindowsPathWithDriveRegex = new Regex(@"^[a-zA-Z]:\\", RegexOptions.Compiled);
private static readonly Regex WindowsPathRegex = new Regex(@"^[a-zA-Z]:\\", RegexOptions.Compiled);
public static bool IsPathValid(this string path) public static bool IsPathValid(this string path)
{ {
if (OsInfo.IsLinux && !path.StartsWith(Path.DirectorySeparatorChar.ToString())) if (path.ContainsInvalidPathChars())
{
return false;
}
if (WindowsInvalidPathRegex.IsMatch(path))
{ {
return false; return false;
} }
//Network path if (OsInfo.IsLinux)
if (path.StartsWith(Path.DirectorySeparatorChar.ToString())) return true;
if (!WindowsPathRegex.IsMatch(path))
{ {
return false; return path.StartsWith(Path.DirectorySeparatorChar.ToString());
} }
if (path.StartsWith("\\") || WindowsPathWithDriveRegex.IsMatch(path))
{
return true; return true;
} }
return false;
}
public static bool ContainsInvalidPathChars(this string text) public static bool ContainsInvalidPathChars(this string text)
{ {