mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-28 23:07:13 -04:00
Validation for samples and saving
This commit is contained in:
@@ -1,15 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Nancy.Responses;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Api.REST;
|
||||
using NzbDrone.Core.Organizer;
|
||||
using NzbDrone.Core.Parser;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
using NzbDrone.Core.Qualities;
|
||||
using NzbDrone.Core.Tv;
|
||||
using Nancy.ModelBinding;
|
||||
using NzbDrone.Api.Mapping;
|
||||
using NzbDrone.Api.Extensions;
|
||||
@@ -19,13 +13,17 @@ namespace NzbDrone.Api.Config
|
||||
public class NamingModule : NzbDroneRestModule<NamingConfigResource>
|
||||
{
|
||||
private readonly INamingConfigService _namingConfigService;
|
||||
private readonly IBuildFileNames _buildFileNames;
|
||||
private readonly IFilenameSampleService _filenameSampleService;
|
||||
private readonly IFilenameValidationService _filenameValidationService;
|
||||
|
||||
public NamingModule(INamingConfigService namingConfigService, IBuildFileNames buildFileNames)
|
||||
public NamingModule(INamingConfigService namingConfigService,
|
||||
IFilenameSampleService filenameSampleService,
|
||||
IFilenameValidationService filenameValidationService)
|
||||
: base("config/naming")
|
||||
{
|
||||
_namingConfigService = namingConfigService;
|
||||
_buildFileNames = buildFileNames;
|
||||
_filenameSampleService = filenameSampleService;
|
||||
_filenameValidationService = filenameValidationService;
|
||||
GetResourceSingle = GetNamingConfig;
|
||||
GetResourceById = GetNamingConfig;
|
||||
UpdateResource = UpdateNamingConfig;
|
||||
@@ -57,185 +55,56 @@ namespace NzbDrone.Api.Config
|
||||
|
||||
private JsonResponse<NamingSampleResource> GetExamples(NamingConfigResource config)
|
||||
{
|
||||
//TODO: Validate that the format is valid
|
||||
var nameSpec = config.InjectTo<NamingConfig>();
|
||||
|
||||
var series = new Core.Tv.Series
|
||||
{
|
||||
SeriesType = SeriesTypes.Standard,
|
||||
Title = "Series Title"
|
||||
};
|
||||
|
||||
var episode1 = new Episode
|
||||
{
|
||||
SeasonNumber = 1,
|
||||
EpisodeNumber = 1,
|
||||
Title = "Episode Title (1)",
|
||||
AirDate = "2013-10-30"
|
||||
};
|
||||
|
||||
var episode2 = new Episode
|
||||
{
|
||||
SeasonNumber = 1,
|
||||
EpisodeNumber = 2,
|
||||
Title = "Episode Title (2)"
|
||||
};
|
||||
|
||||
var episodeFile = new EpisodeFile
|
||||
{
|
||||
Quality = new QualityModel(Quality.HDTV720p),
|
||||
Path = @"C:\Test\Series.Title.S01E01.720p.HDTV.x264-EVOLVE.mkv"
|
||||
};
|
||||
|
||||
var sampleResource = new NamingSampleResource();
|
||||
|
||||
var singleEpisodeSampleResult = _filenameSampleService.GetStandardSample(nameSpec);
|
||||
var multiEpisodeSampleResult = _filenameSampleService.GetMultiEpisodeSample(nameSpec);
|
||||
var dailyEpisodeSampleResult = _filenameSampleService.GetDailySample(nameSpec);
|
||||
|
||||
sampleResource.SingleEpisodeExample = BuildSample(new List<Episode> { episode1 },
|
||||
series,
|
||||
episodeFile,
|
||||
nameSpec);
|
||||
sampleResource.SingleEpisodeExample = _filenameValidationService.ValidateStandardFilename(singleEpisodeSampleResult) != null
|
||||
? "Invalid format"
|
||||
: singleEpisodeSampleResult.Filename;
|
||||
|
||||
episodeFile.Path = @"C:\Test\Series.Title.S01E01-E02.720p.HDTV.x264-EVOLVE.mkv";
|
||||
sampleResource.MultiEpisodeExample = _filenameValidationService.ValidateStandardFilename(multiEpisodeSampleResult) != null
|
||||
? "Invalid format"
|
||||
: multiEpisodeSampleResult.Filename;
|
||||
|
||||
sampleResource.MultiEpisodeExample = BuildSample(new List<Episode> { episode1, episode2 },
|
||||
series,
|
||||
episodeFile,
|
||||
nameSpec);
|
||||
|
||||
episodeFile.Path = @"C:\Test\Series.Title.2013.10.30.HDTV.x264-EVOLVE.mkv";
|
||||
series.SeriesType = SeriesTypes.Daily;
|
||||
|
||||
sampleResource.DailyEpisodeExample = BuildSample(new List<Episode> { episode1 },
|
||||
series,
|
||||
episodeFile,
|
||||
nameSpec);
|
||||
sampleResource.DailyEpisodeExample = _filenameValidationService.ValidateDailyFilename(dailyEpisodeSampleResult) != null
|
||||
? "Invalid format"
|
||||
: dailyEpisodeSampleResult.Filename;
|
||||
|
||||
return sampleResource.AsResponse();
|
||||
}
|
||||
|
||||
private string BuildSample(List<Episode> episodes, Core.Tv.Series series, EpisodeFile episodeFile, NamingConfig nameSpec)
|
||||
{
|
||||
try
|
||||
{
|
||||
//TODO: Validate the result is parsable
|
||||
return _buildFileNames.BuildFilename(episodes,
|
||||
series,
|
||||
episodeFile,
|
||||
nameSpec);
|
||||
}
|
||||
catch (NamingFormatException ex)
|
||||
{
|
||||
//Catching to avoid blowing up all samples
|
||||
//TODO: Use validation to report error to client
|
||||
|
||||
return String.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateFormatResult(NamingConfig nameSpec)
|
||||
{
|
||||
if (!nameSpec.RenameEpisodes)
|
||||
var singleEpisodeSampleResult = _filenameSampleService.GetStandardSample(nameSpec);
|
||||
var multiEpisodeSampleResult = _filenameSampleService.GetMultiEpisodeSample(nameSpec);
|
||||
var dailyEpisodeSampleResult = _filenameSampleService.GetDailySample(nameSpec);
|
||||
var singleEpisodeValidationResult = _filenameValidationService.ValidateStandardFilename(singleEpisodeSampleResult);
|
||||
var multiEpisodeValidationResult = _filenameValidationService.ValidateStandardFilename(multiEpisodeSampleResult);
|
||||
var dailyEpisodeValidationResult = _filenameValidationService.ValidateDailyFilename(dailyEpisodeSampleResult);
|
||||
|
||||
var validationFailures = new List<ValidationFailure>();
|
||||
|
||||
if (singleEpisodeValidationResult != null)
|
||||
{
|
||||
return;
|
||||
validationFailures.Add(singleEpisodeValidationResult);
|
||||
}
|
||||
|
||||
var series = new Core.Tv.Series
|
||||
if (multiEpisodeValidationResult != null)
|
||||
{
|
||||
SeriesType = SeriesTypes.Standard,
|
||||
Title = "Series Title"
|
||||
};
|
||||
|
||||
var episode1 = new Episode
|
||||
{
|
||||
SeasonNumber = 1,
|
||||
EpisodeNumber = 1,
|
||||
Title = "Episode Title (1)",
|
||||
AirDate = "2013-10-30"
|
||||
};
|
||||
|
||||
var episode2 = new Episode
|
||||
{
|
||||
SeasonNumber = 1,
|
||||
EpisodeNumber = 2,
|
||||
Title = "Episode Title (2)",
|
||||
AirDate = "2013-10-30"
|
||||
};
|
||||
|
||||
var episodeFile = new EpisodeFile
|
||||
{
|
||||
Quality = new QualityModel(Quality.HDTV720p)
|
||||
};
|
||||
|
||||
if (!ValidateStandardFormat(nameSpec, series, new List<Episode> { episode1 }, episodeFile))
|
||||
{
|
||||
throw new ValidationException(new List<ValidationFailure>
|
||||
{
|
||||
new ValidationFailure("StandardEpisodeFormat", "Results in unparsable filenames")
|
||||
}.ToArray());
|
||||
validationFailures.Add(multiEpisodeValidationResult);
|
||||
}
|
||||
|
||||
if (!ValidateStandardFormat(nameSpec, series, new List<Episode> { episode1, episode2 }, episodeFile))
|
||||
if (dailyEpisodeValidationResult != null)
|
||||
{
|
||||
throw new ValidationException(new List<ValidationFailure>
|
||||
{
|
||||
new ValidationFailure("StandardEpisodeFormat", "Results in unparsable multi-episode filenames")
|
||||
}.ToArray());
|
||||
validationFailures.Add(dailyEpisodeValidationResult);
|
||||
}
|
||||
|
||||
if (!ValidateDailyFormat(nameSpec, series, episode1, episodeFile))
|
||||
{
|
||||
throw new ValidationException(new List<ValidationFailure>
|
||||
{
|
||||
new ValidationFailure("DailyEpisodeFormat", "Results in unparsable filenames")
|
||||
}.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
private bool ValidateStandardFormat(NamingConfig nameSpec, Core.Tv.Series series, List<Episode> episodes, EpisodeFile episodeFile)
|
||||
{
|
||||
var filename = _buildFileNames.BuildFilename(episodes, series, episodeFile, nameSpec);
|
||||
var parsedEpisodeInfo = Parser.ParseTitle(filename);
|
||||
|
||||
if (parsedEpisodeInfo == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return ValidateSeasonAndEpisodeNumbers(episodes, parsedEpisodeInfo);
|
||||
}
|
||||
|
||||
private bool ValidateDailyFormat(NamingConfig nameSpec, Core.Tv.Series series, Episode episode, EpisodeFile episodeFile)
|
||||
{
|
||||
series.SeriesType = SeriesTypes.Daily;
|
||||
|
||||
var filename = _buildFileNames.BuildFilename(new List<Episode> { episode }, series, episodeFile, nameSpec);
|
||||
var parsedEpisodeInfo = Parser.ParseTitle(filename);
|
||||
|
||||
if (parsedEpisodeInfo == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parsedEpisodeInfo.IsDaily())
|
||||
{
|
||||
if (!parsedEpisodeInfo.AirDate.Equals(episode.AirDate))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return ValidateSeasonAndEpisodeNumbers(new List<Episode> {episode}, parsedEpisodeInfo);
|
||||
}
|
||||
|
||||
private bool ValidateSeasonAndEpisodeNumbers(List<Episode> episodes, ParsedEpisodeInfo parsedEpisodeInfo)
|
||||
{
|
||||
if (parsedEpisodeInfo.SeasonNumber != episodes.First().SeasonNumber ||
|
||||
!parsedEpisodeInfo.EpisodeNumbers.OrderBy(e => e).SequenceEqual(episodes.Select(e => e.EpisodeNumber).OrderBy(e => e)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
throw new ValidationException(validationFailures.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user