sonarr-repo-only/src/NzbDrone.Core/Validation/Paths/SeriesPathValidator.cs

39 lines
1.3 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Linq;
using FluentValidation.Validators;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Validation.Paths
{
public class SeriesPathValidator : PropertyValidator
{
private readonly ISeriesService _seriesService;
public SeriesPathValidator(ISeriesService seriesService)
{
_seriesService = seriesService;
}
protected override string GetDefaultMessageTemplate() => "Path '{path}' is already configured for another series";
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null)
{
return true;
}
context.MessageFormatter.AppendArgument("path", context.PropertyValue.ToString());
dynamic instance = context.ParentContext.InstanceToValidate;
var instanceId = (int)instance.Id;
// Skip the path for this series and any invalid paths
return !_seriesService.GetAllSeriesPaths().Any(s => s.Key != instanceId &&
s.Value.IsPathValid(PathValidationType.CurrentOs) &&
s.Value.PathEquals(context.PropertyValue.ToString()));
}
}
}