You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/Lidarr.Api.V1/Artist/ArtistFolderAsRootFolderVal...

57 lines
1.7 KiB

using System;
using System.IO;
using FluentValidation.Validators;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Organizer;
namespace Lidarr.Api.V1.Artist
{
public class ArtistFolderAsRootFolderValidator : PropertyValidator
{
private readonly IBuildFileNames _fileNameBuilder;
public ArtistFolderAsRootFolderValidator(IBuildFileNames fileNameBuilder)
{
_fileNameBuilder = fileNameBuilder;
}
protected override string GetDefaultMessageTemplate() => "Root folder path '{rootFolderPath}' contains artist folder '{artistFolder}'";
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null)
{
return true;
}
if (context.InstanceToValidate is not ArtistResource artistResource)
{
return true;
}
var rootFolderPath = context.PropertyValue.ToString();
if (rootFolderPath.IsNullOrWhiteSpace())
{
return true;
}
var rootFolder = new DirectoryInfo(rootFolderPath!).Name;
var artist = artistResource.ToModel();
var artistFolder = _fileNameBuilder.GetArtistFolder(artist);
context.MessageFormatter.AppendArgument("rootFolderPath", rootFolderPath);
context.MessageFormatter.AppendArgument("artistFolder", artistFolder);
if (artistFolder == rootFolder)
{
return false;
}
var distance = artistFolder.LevenshteinDistance(rootFolder);
return distance >= Math.Max(1, artistFolder.Length * 0.2);
}
}
}