Fixed: Prevent adding a series if the path is the ancestor of another series

This commit is contained in:
Mark McDowall 2014-06-22 11:01:22 -07:00
parent 801b1b4ec4
commit 6184105d3c
5 changed files with 52 additions and 12 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using NzbDrone.Common;
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.MediaFiles.Events;
@ -40,7 +41,8 @@ namespace NzbDrone.Api.Series
PathExistsValidator pathExistsValidator,
SeriesPathValidator seriesPathValidator,
SeriesExistsValidator seriesExistsValidator,
DroneFactoryValidator droneFactoryValidator
DroneFactoryValidator droneFactoryValidator,
SeriesAncestorValidator seriesAncestorValidator
)
: base(commandExecutor)
{
@ -59,17 +61,21 @@ namespace NzbDrone.Api.Series
SharedValidator.RuleFor(s => s.QualityProfileId).ValidId();
PutValidator.RuleFor(s => s.Path)
.Cascade(CascadeMode.StopOnFirstFailure)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(seriesPathValidator)
.SetValidator(droneFactoryValidator);
SharedValidator.RuleFor(s => s.Path)
.Cascade(CascadeMode.StopOnFirstFailure)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(seriesPathValidator)
.SetValidator(droneFactoryValidator)
.SetValidator(seriesAncestorValidator)
.When(s => !s.Path.IsNullOrWhiteSpace());
PostValidator.RuleFor(s => s.Path).IsValidPath().When(s => String.IsNullOrEmpty(s.RootFolderPath));
PostValidator.RuleFor(s => s.RootFolderPath).IsValidPath().When(s => String.IsNullOrEmpty(s.Path));
PostValidator.RuleFor(s => s.Path).IsValidPath().When(s => s.RootFolderPath.IsNullOrWhiteSpace());
PostValidator.RuleFor(s => s.RootFolderPath).IsValidPath().When(s => s.Path.IsNullOrWhiteSpace());
PostValidator.RuleFor(s => s.Title).NotEmpty();
PostValidator.RuleFor(s => s.TvdbId).GreaterThan(0).SetValidator(seriesExistsValidator);
PutValidator.RuleFor(s => s.Path).IsValidPath();
}
private void PopulateAlternativeTitles(List<SeriesResource> resources)

View File

@ -12,6 +12,7 @@ namespace NzbDrone.Common.Test
[TestFixture]
public class PathExtensionFixture : TestBase
{
private string _parent = @"C:\Test".AsOsAgnostic();
private IAppFolderInfo GetIAppDirectoryInfo()
{
@ -86,8 +87,6 @@ namespace NzbDrone.Common.Test
first.AsOsAgnostic().PathEquals(second.AsOsAgnostic()).Should().BeFalse();
}
private string _parent = @"C:\Test".AsOsAgnostic();
[Test]
public void should_return_false_when_not_a_child()
{
@ -137,6 +136,15 @@ namespace NzbDrone.Common.Test
parentPath.IsParentPath(childPath).Should().Be(expectedResult);
}
[Test]
[Ignore]
public void should_not_be_parent_when_it_is_grandparent()
{
var path = Path.Combine(_parent, "parent", "child");
_parent.IsParentPath(path).Should().BeFalse();
}
[Test]
public void normalize_path_exception_empty()
{

View File

@ -47,7 +47,7 @@ namespace NzbDrone.Common
{
if (!parentPath.IsParentPath(childPath))
{
throw new NzbDrone.Common.Exceptions.NotParentException("{0} is not a child of {1}", childPath, parentPath);
throw new Exceptions.NotParentException("{0} is not a child of {1}", childPath, parentPath);
}
return childPath.Substring(parentPath.Length).Trim(Path.DirectorySeparatorChar);

View File

@ -709,6 +709,7 @@
<Compile Include="Update\UpdateCheckService.cs" />
<Compile Include="Update\UpdateVerification.cs" />
<Compile Include="Update\UpdateVerificationFailedException.cs" />
<Compile Include="Validation\Paths\SeriesAncestorValidator.cs" />
<Compile Include="Validation\Paths\SeriesExistsValidator.cs" />
<Compile Include="Validation\Paths\RootFolderValidator.cs" />
<Compile Include="Validation\Paths\DroneFactoryValidator.cs" />

View File

@ -0,0 +1,25 @@
using System.Linq;
using FluentValidation.Validators;
using NzbDrone.Common;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Validation.Paths
{
public class SeriesAncestorValidator : PropertyValidator
{
private readonly ISeriesService _seriesService;
public SeriesAncestorValidator(ISeriesService seriesService)
: base("Path is an ancestor of an existing path")
{
_seriesService = seriesService;
}
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
return !_seriesService.GetAllSeries().Any(s => context.PropertyValue.ToString().IsParentPath(s.Path));
}
}
}