2014-11-17 05:31:53 +00:00
|
|
|
|
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);
|
2014-05-19 19:14:41 +00:00
|
|
|
|
ValidationFailure ValidateAnimeFilename(SampleResult sampleResult);
|
2013-11-16 05:03:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-28 21:14:34 +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);
|
2014-07-28 21:14:34 +00:00
|
|
|
|
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);
|
2014-07-28 21:14:34 +00:00
|
|
|
|
var parsedEpisodeInfo = Parser.Parser.ParseTitle(sampleResult.FileName);
|
2013-11-16 05:03:42 +00:00
|
|
|
|
|
|
|
|
|
if (parsedEpisodeInfo == null)
|
|
|
|
|
{
|
|
|
|
|
return validationFailure;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-09 07:13:01 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 19:14:41 +00:00
|
|
|
|
public ValidationFailure ValidateAnimeFilename(SampleResult sampleResult)
|
|
|
|
|
{
|
|
|
|
|
var validationFailure = new ValidationFailure("AnimeEpisodeFormat", ERROR_MESSAGE);
|
2014-07-28 21:14:34 +00:00
|
|
|
|
var parsedEpisodeInfo = Parser.Parser.ParseTitle(sampleResult.FileName);
|
2014-05-19 19:14:41 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|