@ -1,5 +1,6 @@
using System.IO ;
using System.Linq ;
using System.Text.RegularExpressions ;
using FluentValidation ;
using FluentValidation.Validators ;
using NzbDrone.Common.Extensions ;
@ -8,20 +9,56 @@ namespace NzbDrone.Core.Organizer
{
public static class FileNameValidation
{
public static IRuleBuilderOptions < T , string > ValidMovieFolderFormat < T > ( this IRuleBuilder < T , string > ruleBuilder )
internal static readonly Regex OriginalTokenRegex = new ( @"(\{Original[- ._](?:Title|Filename)\})" ,
RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
public static IRuleBuilderOptions < T , string > ValidMovieFormat < T > ( this IRuleBuilder < T , string > ruleBuilder )
{
ruleBuilder . SetValidator ( new NotEmptyValidator ( null ) ) ;
ruleBuilder . SetValidator ( new IllegalCharactersValidator ( ) ) ;
return ruleBuilder . SetValidator ( new RegularExpressionValidator( FileNameBuilder . MovieTitleRegex ) ) . WithMessage ( "Must contain movie title" ) ;
return ruleBuilder . SetValidator ( new ValidMovieFormatValidator( ) ) ;
}
public static IRuleBuilderOptions < T , string > ValidMovieFo rmat< T > ( this IRuleBuilder < T , string > ruleBuilder )
public static IRuleBuilderOptions < T , string > ValidMovieFo lderFo rmat< T > ( this IRuleBuilder < T , string > ruleBuilder )
{
ruleBuilder . SetValidator ( new NotEmptyValidator ( null ) ) ;
ruleBuilder . SetValidator ( new IllegalCharactersValidator ( ) ) ;
return ruleBuilder . SetValidator ( new RegularExpressionValidator ( FileNameBuilder . MovieTitleRegex ) ) . WithMessage ( "Must contain movie title" ) ;
return ruleBuilder . SetValidator ( new ValidMovieFolderFormatValidator ( ) ) ;
}
}
public class ValidMovieFormatValidator : PropertyValidator
{
protected override string GetDefaultMessageTemplate ( ) = > "Must contain movie title and release year OR Original Title" ;
protected override bool IsValid ( PropertyValidatorContext context )
{
if ( context . PropertyValue is not string value )
{
return false ;
}
return ( FileNameBuilder . MovieTitleRegex . IsMatch ( value ) & & FileNameBuilder . ReleaseYearRegex . IsMatch ( value ) ) | |
FileNameValidation . OriginalTokenRegex . IsMatch ( value ) ;
}
}
public class ValidMovieFolderFormatValidator : PropertyValidator
{
protected override string GetDefaultMessageTemplate ( ) = > "Must contain movie title" ;
protected override bool IsValid ( PropertyValidatorContext context )
{
if ( context . PropertyValue is not string value )
{
return false ;
}
// TODO: Deprecate OriginalTokenRegex use for Movie Folder Format
return FileNameBuilder . MovieTitleRegex . IsMatch ( value ) | |
FileNameValidation . OriginalTokenRegex . IsMatch ( value ) ;
}
}