Lidarr/src/NzbDrone.Core/Organizer/FileNameValidationService.cs

106 lines
3.4 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2013-11-16 05:03:42 +00:00
using System.Linq;
using FluentValidation.Results;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Organizer
{
public interface IFilenameValidationService
{
ValidationFailure ValidateStandardFilename(SampleResult sampleResult);
ValidationFailure ValidateDailyFilename(SampleResult sampleResult);
ValidationFailure ValidateAnimeFilename(SampleResult sampleResult);
2013-11-16 05:03:42 +00:00
}
public class FileNameValidationService : IFilenameValidationService
2013-11-16 05:03:42 +00:00
{
private const string ERROR_MESSAGE = "Produces invalid file names";
public ValidationFailure ValidateStandardFilename(SampleResult sampleResult)
{
var validationFailure = new ValidationFailure("StandardEpisodeFormat", ERROR_MESSAGE);
var parsedEpisodeInfo = Parser.Parser.ParseTitle(sampleResult.FileName);
2013-11-16 05:03:42 +00:00
if (parsedEpisodeInfo == null)
{
return validationFailure;
}
if (!ValidateSeasonAndEpisodeNumbers(sampleResult.Episodes, parsedEpisodeInfo))
{
return validationFailure;
}
return null;
}
public ValidationFailure ValidateDailyFilename(SampleResult sampleResult)
{
var validationFailure = new ValidationFailure("DailyEpisodeFormat", ERROR_MESSAGE);
var parsedEpisodeInfo = Parser.Parser.ParseTitle(sampleResult.FileName);
2013-11-16 05:03:42 +00:00
if (parsedEpisodeInfo == null)
{
return validationFailure;
}
if (parsedEpisodeInfo.IsDaily)
2013-11-16 05:03:42 +00:00
{
if (!parsedEpisodeInfo.AirDate.Equals(sampleResult.Episodes.Single().AirDate))
{
return validationFailure;
}
return null;
}
if (!ValidateSeasonAndEpisodeNumbers(sampleResult.Episodes, parsedEpisodeInfo))
{
return validationFailure;
}
return null;
}
public ValidationFailure ValidateAnimeFilename(SampleResult sampleResult)
{
var validationFailure = new ValidationFailure("AnimeEpisodeFormat", ERROR_MESSAGE);
var parsedEpisodeInfo = Parser.Parser.ParseTitle(sampleResult.FileName);
if (parsedEpisodeInfo == null)
{
return validationFailure;
}
if (parsedEpisodeInfo.AbsoluteEpisodeNumbers.Any())
{
if (!parsedEpisodeInfo.AbsoluteEpisodeNumbers.First().Equals(sampleResult.Episodes.First().AbsoluteEpisodeNumber))
{
return validationFailure;
}
return null;
}
if (!ValidateSeasonAndEpisodeNumbers(sampleResult.Episodes, parsedEpisodeInfo))
{
return validationFailure;
}
return null;
}
2013-11-16 05:03:42 +00:00
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;
}
}
}