mirror of
https://github.com/Readarr/Readarr
synced 2025-12-27 10:43:01 +01:00
New: Validate that naming formats don't contain illegal characters
Fixes #620 (cherry picked from commit 145c644c9d8f1636027da8a782a7e74f3182c678)
This commit is contained in:
parent
e69371deca
commit
ed0722bae4
1 changed files with 46 additions and 0 deletions
|
|
@ -1,6 +1,10 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using FluentValidation;
|
||||
using FluentValidation.Validators;
|
||||
using NzbDrone.Common.Extensions;
|
||||
|
||||
namespace NzbDrone.Core.Organizer
|
||||
{
|
||||
|
|
@ -12,12 +16,16 @@ public static class FileNameValidation
|
|||
public static IRuleBuilderOptions<T, string> ValidBookFormat<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||
{
|
||||
ruleBuilder.SetValidator(new NotEmptyValidator(null));
|
||||
ruleBuilder.SetValidator(new IllegalCharactersValidator());
|
||||
|
||||
return ruleBuilder.SetValidator(new ValidStandardTrackFormatValidator());
|
||||
}
|
||||
|
||||
public static IRuleBuilderOptions<T, string> ValidAuthorFolderFormat<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||
{
|
||||
ruleBuilder.SetValidator(new NotEmptyValidator(null));
|
||||
ruleBuilder.SetValidator(new IllegalCharactersValidator());
|
||||
|
||||
return ruleBuilder.SetValidator(new RegularExpressionValidator(FileNameBuilder.AuthorNameRegex)).WithMessage("Must contain Author name");
|
||||
}
|
||||
}
|
||||
|
|
@ -42,4 +50,42 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class IllegalCharactersValidator : PropertyValidator
|
||||
{
|
||||
private readonly char[] _invalidPathChars = Path.GetInvalidPathChars();
|
||||
|
||||
public IllegalCharactersValidator()
|
||||
: base("Contains illegal characters: {InvalidCharacters}")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
var value = context.PropertyValue as string;
|
||||
var invalidCharacters = new List<char>();
|
||||
|
||||
if (value.IsNullOrWhiteSpace())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var i in _invalidPathChars)
|
||||
{
|
||||
if (value.IndexOf(i) >= 0)
|
||||
{
|
||||
invalidCharacters.Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (invalidCharacters.Any())
|
||||
{
|
||||
context.MessageFormatter.AppendArgument("InvalidCharacters", string.Join("", invalidCharacters));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue