Fixed: (Cardigann) Validate definition file and setting fields existence

Towards #2245
pull/2252/head
Bogdan 2 months ago
parent efa54a4d51
commit dd046d8a68

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Disk;
@ -118,7 +119,7 @@ namespace NzbDrone.Core.IndexerVersions
public CardigannDefinition GetCachedDefinition(string fileKey)
{
if (string.IsNullOrEmpty(fileKey))
if (string.IsNullOrWhiteSpace(fileKey))
{
throw new ArgumentNullException(nameof(fileKey));
}
@ -174,7 +175,7 @@ namespace NzbDrone.Core.IndexerVersions
private CardigannDefinition GetUncachedDefinition(string fileKey)
{
if (string.IsNullOrEmpty(fileKey))
if (string.IsNullOrWhiteSpace(fileKey))
{
throw new ArgumentNullException(nameof(fileKey));
}
@ -221,10 +222,25 @@ namespace NzbDrone.Core.IndexerVersions
}
private CardigannDefinition GetHttpDefinition(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentNullException(nameof(id));
}
CardigannDefinition definition;
try
{
var request = new HttpRequest($"https://indexers.prowlarr.com/{DEFINITION_BRANCH}/{DEFINITION_VERSION}/{id}");
var response = _httpClient.Get(request);
var definition = _deserializer.Deserialize<CardigannDefinition>(response.Content);
definition = _deserializer.Deserialize<CardigannDefinition>(response.Content);
}
catch (HttpException ex) when (ex.Response.StatusCode == HttpStatusCode.NotFound)
{
throw new Exception($"Indexer definition for '{id}' does not exist.", ex);
}
return CleanIndexerDefinition(definition);
}

@ -1,11 +1,23 @@
using System.Collections.Generic;
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Indexers.Settings;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Indexers.Definitions.Cardigann
{
public class CardigannSettingsValidator : NoAuthSettingsValidator<CardigannSettings>
{
public CardigannSettingsValidator()
{
RuleFor(c => c.DefinitionFile).NotEmpty();
}
}
public class CardigannSettings : NoAuthTorrentBaseSettings
{
private static readonly CardigannSettingsValidator Validator = new ();
public CardigannSettings()
{
ExtraFieldData = new Dictionary<string, object>();
@ -15,5 +27,10 @@ namespace NzbDrone.Core.Indexers.Definitions.Cardigann
public string DefinitionFile { get; set; }
public Dictionary<string, object> ExtraFieldData { get; set; }
public override NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

@ -119,6 +119,8 @@ namespace Prowlarr.Api.V1.Indexers
var settings = (CardigannSettings)definition.Settings;
if (settings.DefinitionFile.IsNotNullOrWhiteSpace())
{
var cardigannDefinition = _definitionService.GetCachedDefinition(settings.DefinitionFile);
foreach (var field in resource.Fields)
@ -132,11 +134,18 @@ namespace Prowlarr.Api.V1.Indexers
else
{
var cardigannSetting = cardigannDefinition.Settings.FirstOrDefault(x => x.Name == field.Name);
if (cardigannSetting == null)
{
throw new ArgumentOutOfRangeException(field.Name, "Unknown Cardigann setting.");
}
settings.ExtraFieldData[field.Name] = MapValue(cardigannSetting, field.Value);
}
}
}
}
}
definition.AppProfileId = resource.AppProfileId;
definition.Enable = resource.Enable;

Loading…
Cancel
Save