From fc540067c2a79ad08be983a3fc795ad5bd8dc49e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 17 Apr 2014 11:26:48 -0700 Subject: [PATCH] Series and Season folder format validation/error handling --- src/NzbDrone.Api/Config/NamingConfigModule.cs | 12 +++++++++--- src/NzbDrone.Core/Organizer/FileNameValidation.cs | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Api/Config/NamingConfigModule.cs b/src/NzbDrone.Api/Config/NamingConfigModule.cs index a571dd400..36876a406 100644 --- a/src/NzbDrone.Api/Config/NamingConfigModule.cs +++ b/src/NzbDrone.Api/Config/NamingConfigModule.cs @@ -4,6 +4,7 @@ using System.Linq; using FluentValidation; using FluentValidation.Results; using Nancy.Responses; +using NzbDrone.Common; using NzbDrone.Core.Organizer; using Nancy.ModelBinding; using NzbDrone.Api.Mapping; @@ -39,6 +40,7 @@ namespace NzbDrone.Api.Config SharedValidator.RuleFor(c => c.StandardEpisodeFormat).ValidEpisodeFormat(); SharedValidator.RuleFor(c => c.DailyEpisodeFormat).ValidDailyEpisodeFormat(); SharedValidator.RuleFor(c => c.SeriesFolderFormat).ValidSeriesFolderFormat(); + SharedValidator.RuleFor(c => c.SeasonFolderFormat).ValidSeasonFolderFormat(); } private void UpdateNamingConfig(NamingConfigResource resource) @@ -72,7 +74,6 @@ namespace NzbDrone.Api.Config private JsonResponse GetExamples(NamingConfigResource config) { - //TODO: Validate that the format is valid var nameSpec = config.InjectTo(); var sampleResource = new NamingSampleResource(); @@ -92,8 +93,13 @@ namespace NzbDrone.Api.Config ? "Invalid format" : dailyEpisodeSampleResult.Filename; - sampleResource.SeriesFolderExample = _filenameSampleService.GetSeriesFolderSample(nameSpec); - sampleResource.SeasonFolderExample = _filenameSampleService.GetSeasonFolderSample(nameSpec); + sampleResource.SeriesFolderExample = nameSpec.SeriesFolderFormat.IsNullOrWhiteSpace() + ? "Invalid format" + : _filenameSampleService.GetSeriesFolderSample(nameSpec); + + sampleResource.SeasonFolderExample = nameSpec.SeasonFolderFormat.IsNullOrWhiteSpace() + ? "Invalid format" + : _filenameSampleService.GetSeasonFolderSample(nameSpec); return sampleResource.AsResponse(); } diff --git a/src/NzbDrone.Core/Organizer/FileNameValidation.cs b/src/NzbDrone.Core/Organizer/FileNameValidation.cs index 9a291e05c..bd554d46a 100644 --- a/src/NzbDrone.Core/Organizer/FileNameValidation.cs +++ b/src/NzbDrone.Core/Organizer/FileNameValidation.cs @@ -1,4 +1,5 @@ using System; +using System.Text.RegularExpressions; using FluentValidation; using FluentValidation.Validators; @@ -6,6 +7,9 @@ namespace NzbDrone.Core.Organizer { public static class FileNameValidation { + private static readonly Regex SeasonFolderRegex = new Regex(@"(\{season(\:\d+)?\})", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + public static IRuleBuilderOptions ValidEpisodeFormat(this IRuleBuilder ruleBuilder) { ruleBuilder.SetValidator(new NotEmptyValidator(null)); @@ -23,6 +27,12 @@ namespace NzbDrone.Core.Organizer ruleBuilder.SetValidator(new NotEmptyValidator(null)); return ruleBuilder.SetValidator(new RegularExpressionValidator(FileNameBuilder.SeriesTitleRegex)).WithMessage("Must contain series title"); } + + public static IRuleBuilderOptions ValidSeasonFolderFormat(this IRuleBuilder ruleBuilder) + { + ruleBuilder.SetValidator(new NotEmptyValidator(null)); + return ruleBuilder.SetValidator(new RegularExpressionValidator(SeasonFolderRegex)).WithMessage("Must contain season number"); + } } public class ValidDailyEpisodeFormatValidator : PropertyValidator