1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-22 07:42:28 +00:00

Fixed: Improve validation for individual album adding

This commit is contained in:
Bogdan 2024-09-22 04:38:55 +03:00
parent 34c9300cbf
commit 21d9ecccd6
3 changed files with 67 additions and 7 deletions

View file

@ -42,6 +42,14 @@ public AlbumController(IArtistService artistService,
IMapCoversToLocal coverMapper, IMapCoversToLocal coverMapper,
IUpgradableSpecification upgradableSpecification, IUpgradableSpecification upgradableSpecification,
IBroadcastSignalRMessage signalRBroadcaster, IBroadcastSignalRMessage signalRBroadcaster,
RootFolderValidator rootFolderValidator,
MappedNetworkDriveValidator mappedNetworkDriveValidator,
ArtistPathValidator artistPathValidator,
ArtistAncestorValidator artistAncestorValidator,
RecycleBinValidator recycleBinValidator,
SystemFolderValidator systemFolderValidator,
AlbumExistsValidator albumExistsValidator,
RootFolderExistsValidator rootFolderExistsValidator,
QualityProfileExistsValidator qualityProfileExistsValidator, QualityProfileExistsValidator qualityProfileExistsValidator,
MetadataProfileExistsValidator metadataProfileExistsValidator) MetadataProfileExistsValidator metadataProfileExistsValidator)
@ -51,11 +59,34 @@ public AlbumController(IArtistService artistService,
_releaseService = releaseService; _releaseService = releaseService;
_addAlbumService = addAlbumService; _addAlbumService = addAlbumService;
PostValidator.RuleFor(s => s.ForeignAlbumId).NotEmpty(); PostValidator.RuleFor(s => s.ForeignAlbumId).NotEmpty().SetValidator(albumExistsValidator);
PostValidator.RuleFor(s => s.Artist.QualityProfileId).SetValidator(qualityProfileExistsValidator); PostValidator.RuleFor(s => s.Artist).NotNull();
PostValidator.RuleFor(s => s.Artist.MetadataProfileId).SetValidator(metadataProfileExistsValidator); PostValidator.RuleFor(s => s.Artist.ForeignArtistId).NotEmpty().When(s => s.Artist != null);
PostValidator.RuleFor(s => s.Artist.RootFolderPath).IsValidPath().When(s => s.Artist.Path.IsNullOrWhiteSpace());
PostValidator.RuleFor(s => s.Artist.ForeignArtistId).NotEmpty(); PostValidator.RuleFor(s => s.Artist.QualityProfileId).Cascade(CascadeMode.Stop)
.ValidId()
.SetValidator(qualityProfileExistsValidator)
.When(s => s.Artist != null);
PostValidator.RuleFor(s => s.Artist.MetadataProfileId).Cascade(CascadeMode.Stop)
.ValidId()
.SetValidator(metadataProfileExistsValidator)
.When(s => s.Artist != null);
PostValidator.RuleFor(s => s.Artist.Path).Cascade(CascadeMode.Stop)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(mappedNetworkDriveValidator)
.SetValidator(artistPathValidator)
.SetValidator(artistAncestorValidator)
.SetValidator(recycleBinValidator)
.SetValidator(systemFolderValidator)
.When(s => s.Artist != null && s.Artist.Path.IsNotNullOrWhiteSpace());
PostValidator.RuleFor(s => s.Artist.RootFolderPath)
.IsValidPath()
.SetValidator(rootFolderExistsValidator)
.When(s => s.Artist != null && s.Artist.Path.IsNullOrWhiteSpace());
} }
[HttpGet] [HttpGet]

View file

@ -110,8 +110,8 @@ public static Album ToModel(this AlbumResource resource)
AlbumType = resource.AlbumType, AlbumType = resource.AlbumType,
Monitored = resource.Monitored, Monitored = resource.Monitored,
AnyReleaseOk = resource.AnyReleaseOk, AnyReleaseOk = resource.AnyReleaseOk,
AlbumReleases = resource.Releases.ToModel(), AlbumReleases = resource.Releases?.ToModel() ?? new List<AlbumRelease>(),
AddOptions = resource.AddOptions, AddOptions = resource.AddOptions ?? new AddAlbumOptions(),
Artist = artist, Artist = artist,
ArtistMetadata = artist.Metadata.Value ArtistMetadata = artist.Metadata.Value
}; };

View file

@ -0,0 +1,29 @@
using FluentValidation.Validators;
using NzbDrone.Core.Music;
namespace NzbDrone.Core.Validation.Paths
{
public class AlbumExistsValidator : PropertyValidator
{
private readonly IAlbumService _albumService;
public AlbumExistsValidator(IAlbumService albumService)
{
_albumService = albumService;
}
protected override string GetDefaultMessageTemplate() => "This album has already been added.";
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null)
{
return true;
}
var foreignAlbumId = context.PropertyValue.ToString();
return _albumService.FindById(foreignAlbumId) == null;
}
}
}