mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-19 21:44:30 -04:00
New: Author folder hint when selecting a root folder while adding a new author
(cherry picked from commit dd09f31abb4dd3f699bcff0a47577075300c70ee) Fix AuthorFolderAsRootFolderValidator (cherry picked from commit 0ce81e1ab69d43fde382cc4ae22cd46fe626dea7)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using FluentValidation.Validators;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Organizer;
|
||||
|
||||
namespace Readarr.Api.V1.Author
|
||||
{
|
||||
public class AuthorFolderAsRootFolderValidator : PropertyValidator
|
||||
{
|
||||
private readonly IBuildFileNames _fileNameBuilder;
|
||||
|
||||
public AuthorFolderAsRootFolderValidator(IBuildFileNames fileNameBuilder)
|
||||
{
|
||||
_fileNameBuilder = fileNameBuilder;
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate() => "Root folder path contains author folder";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
if (context.PropertyValue == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var authorResource = context.ParentContext.InstanceToValidate as AuthorResource;
|
||||
|
||||
if (authorResource == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var rootFolderPath = context.PropertyValue.ToString();
|
||||
var rootFolder = new DirectoryInfo(rootFolderPath).Name;
|
||||
var author = authorResource.ToModel();
|
||||
var authorFolder = _fileNameBuilder.GetAuthorFolder(author);
|
||||
|
||||
if (authorFolder == rootFolder)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var distance = authorFolder.LevenshteinDistance(rootFolder);
|
||||
|
||||
return distance >= Math.Max(1, authorFolder.Length * 0.2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user