2013-02-17 20:19:38 +01:00
using System.Diagnostics ;
2013-05-25 01:40:40 +02:00
using System.IO ;
2013-02-17 20:19:38 +01:00
using System.Text.RegularExpressions ;
using NzbDrone.Common.EnsureThat.Resources ;
2013-06-28 02:04:52 +02:00
using NzbDrone.Common.EnvironmentInfo ;
2013-02-17 20:19:38 +01:00
namespace NzbDrone.Common.EnsureThat
{
[DebuggerStepThrough]
public static class EnsureStringExtensions
{
[DebuggerStepThrough]
public static Param < string > IsNotNullOrWhiteSpace ( this Param < string > param )
{
if ( string . IsNullOrWhiteSpace ( param . Value ) )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotNullOrWhiteSpace ) ;
return param ;
}
[DebuggerStepThrough]
public static Param < string > IsNotNullOrEmpty ( this Param < string > param )
{
if ( string . IsNullOrEmpty ( param . Value ) )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotNullOrEmpty ) ;
return param ;
}
[DebuggerStepThrough]
public static Param < string > HasLengthBetween ( this Param < string > param , int minLength , int maxLength )
{
if ( string . IsNullOrEmpty ( param . Value ) )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotNullOrEmpty ) ;
var length = param . Value . Length ;
if ( length < minLength )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotInRange_ToShort . Inject ( minLength , maxLength , length ) ) ;
if ( length > maxLength )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotInRange_ToLong . Inject ( minLength , maxLength , length ) ) ;
return param ;
}
[DebuggerStepThrough]
public static Param < string > IsLongerThan ( this Param < string > param , int minLength )
{
if ( string . IsNullOrEmpty ( param . Value ) )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotNullOrEmpty ) ;
var length = param . Value . Length ;
if ( length < minLength )
throw ExceptionFactory . CreateForParamValidation ( param . Name , "The string is not long enough. Must be at least '{0}' but was '{1}' characters long." . Inject ( minLength , length ) ) ;
return param ;
}
[DebuggerStepThrough]
public static Param < string > Matches ( this Param < string > param , string match )
{
return Matches ( param , new Regex ( match ) ) ;
}
[DebuggerStepThrough]
public static Param < string > Matches ( this Param < string > param , Regex match )
{
if ( ! match . IsMatch ( param . Value ) )
{
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_NoMatch . Inject ( param . Value , match ) ) ;
}
return param ;
}
[DebuggerStepThrough]
public static Param < string > IsRelativePath ( this Param < string > param )
{
if ( string . IsNullOrWhiteSpace ( param . Value ) )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotNullOrWhiteSpace ) ;
if ( ! param . Value . EndsWith ( "\\" ) )
{
throw ExceptionFactory . CreateForParamValidation ( param . Name , string . Format ( "value [{0}] is not a valid relative path. relative paths must end with \\" , param . Value ) ) ;
}
if ( param . Value . Length > 1 & & param . Value . StartsWith ( "\\" ) )
{
throw ExceptionFactory . CreateForParamValidation ( param . Name , string . Format ( "value [{0}] is not a valid relative path. relative paths can not start with \\" , param . Value ) ) ;
}
2013-05-25 00:18:37 +02:00
return param ;
}
2013-08-08 04:07:46 +02:00
private static readonly Regex windowsInvalidPathRegex = new Regex ( @"[/*<>""|]" , RegexOptions . Compiled ) ;
private static readonly Regex windowsPathRegex = new Regex ( @"^[a-zA-Z]:\\" , RegexOptions . Compiled ) ;
2013-05-25 00:18:37 +02:00
[DebuggerStepThrough]
public static Param < string > IsValidPath ( this Param < string > param )
{
if ( string . IsNullOrWhiteSpace ( param . Value ) )
throw ExceptionFactory . CreateForParamValidation ( param . Name , ExceptionMessages . EnsureExtensions_IsNotNullOrWhiteSpace ) ;
2013-06-28 02:04:52 +02:00
if ( OsInfo . IsLinux )
2013-05-25 00:18:37 +02:00
{
2013-05-25 01:40:40 +02:00
if ( ! param . Value . StartsWith ( Path . DirectorySeparatorChar . ToString ( ) ) )
2013-05-25 00:18:37 +02:00
{
2013-05-25 01:40:40 +02:00
throw ExceptionFactory . CreateForParamValidation ( param . Name , string . Format ( "value [{0}] is not a valid *nix path. paths must start with /" , param . Value ) ) ;
2013-05-25 00:18:37 +02:00
}
}
else
{
if ( windowsInvalidPathRegex . IsMatch ( param . Value ) )
{
throw ExceptionFactory . CreateForParamValidation ( param . Name , string . Format ( "value [{0}] is not a valid Windows path. It contains invalid characters" , param . Value ) ) ;
}
//Network path
2013-05-25 01:40:40 +02:00
if ( param . Value . StartsWith ( Path . DirectorySeparatorChar . ToString ( ) ) ) return param ;
2013-05-25 00:18:37 +02:00
if ( ! windowsPathRegex . IsMatch ( param . Value ) )
{
throw ExceptionFactory . CreateForParamValidation ( param . Name , string . Format ( "value [{0}] is not a valid Windows path. paths must be a full path eg. C:\\Windows" , param . Value ) ) ;
}
}
2013-02-17 20:19:38 +01:00
return param ;
}
}
}