cleaned up path validation logic.

This commit is contained in:
kay.one 2013-08-31 13:43:35 -07:00
parent bf415563d4
commit 9e08dffa58
1 changed files with 10 additions and 13 deletions

View File

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