2013-11-15 08:53:12 +00:00
using System ;
2014-04-17 18:26:48 +00:00
using System.Text.RegularExpressions ;
2013-11-15 08:53:12 +00:00
using FluentValidation ;
using FluentValidation.Validators ;
namespace NzbDrone.Core.Organizer
{
public static class FileNameValidation
{
2014-04-17 18:26:48 +00:00
private static readonly Regex SeasonFolderRegex = new Regex ( @"(\{season(\:\d+)?\})" ,
RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
2013-11-15 08:53:12 +00:00
public static IRuleBuilderOptions < T , string > ValidEpisodeFormat < T > ( this IRuleBuilder < T , string > ruleBuilder )
{
ruleBuilder . SetValidator ( new NotEmptyValidator ( null ) ) ;
return ruleBuilder . SetValidator ( new RegularExpressionValidator ( FileNameBuilder . SeasonEpisodePatternRegex ) ) . WithMessage ( "Must contain season and episode numbers" ) ;
}
public static IRuleBuilderOptions < T , string > ValidDailyEpisodeFormat < T > ( this IRuleBuilder < T , string > ruleBuilder )
{
ruleBuilder . SetValidator ( new NotEmptyValidator ( null ) ) ;
return ruleBuilder . SetValidator ( new ValidDailyEpisodeFormatValidator ( ) ) ;
}
2014-01-17 18:43:15 +00:00
2014-05-19 19:14:41 +00:00
public static IRuleBuilderOptions < T , string > ValidAnimeEpisodeFormat < T > ( this IRuleBuilder < T , string > ruleBuilder )
{
ruleBuilder . SetValidator ( new NotEmptyValidator ( null ) ) ;
return ruleBuilder . SetValidator ( new ValidAnimeEpisodeFormatValidator ( ) ) ;
}
2014-01-17 18:43:15 +00:00
public static IRuleBuilderOptions < T , string > ValidSeriesFolderFormat < T > ( this IRuleBuilder < T , string > ruleBuilder )
{
ruleBuilder . SetValidator ( new NotEmptyValidator ( null ) ) ;
return ruleBuilder . SetValidator ( new RegularExpressionValidator ( FileNameBuilder . SeriesTitleRegex ) ) . WithMessage ( "Must contain series title" ) ;
}
2014-04-17 18:26:48 +00:00
public static IRuleBuilderOptions < T , string > ValidSeasonFolderFormat < T > ( this IRuleBuilder < T , string > ruleBuilder )
{
ruleBuilder . SetValidator ( new NotEmptyValidator ( null ) ) ;
return ruleBuilder . SetValidator ( new RegularExpressionValidator ( SeasonFolderRegex ) ) . WithMessage ( "Must contain season number" ) ;
}
2013-11-15 08:53:12 +00:00
}
public class ValidDailyEpisodeFormatValidator : PropertyValidator
{
public ValidDailyEpisodeFormatValidator ( )
: base ( "Must contain Air Date or Season and Episode" )
{
}
protected override bool IsValid ( PropertyValidatorContext context )
{
var value = context . PropertyValue as String ;
if ( ! FileNameBuilder . SeasonEpisodePatternRegex . IsMatch ( value ) & &
! FileNameBuilder . AirDateRegex . IsMatch ( value ) )
{
return false ;
}
return true ;
}
}
2014-05-19 19:14:41 +00:00
public class ValidAnimeEpisodeFormatValidator : PropertyValidator
{
public ValidAnimeEpisodeFormatValidator ( )
: base ( "Must contain Absolute Episode number or Season and Episode" )
{
}
protected override bool IsValid ( PropertyValidatorContext context )
{
var value = context . PropertyValue as String ;
if ( ! FileNameBuilder . SeasonEpisodePatternRegex . IsMatch ( value ) & &
! FileNameBuilder . AbsoluteEpisodePatternRegex . IsMatch ( value ) )
{
return false ;
}
return true ;
}
}
2013-11-15 08:53:12 +00:00
}