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));
}
@ -222,9 +223,24 @@ namespace NzbDrone.Core.IndexerVersions
private CardigannDefinition GetHttpDefinition(string id)
{
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);
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);
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,20 +119,29 @@ namespace Prowlarr.Api.V1.Indexers
var settings = (CardigannSettings)definition.Settings;
var cardigannDefinition = _definitionService.GetCachedDefinition(settings.DefinitionFile);
foreach (var field in resource.Fields)
if (settings.DefinitionFile.IsNotNullOrWhiteSpace())
{
if (!standardFields.Contains(field.Name))
var cardigannDefinition = _definitionService.GetCachedDefinition(settings.DefinitionFile);
foreach (var field in resource.Fields)
{
if (field.Name == "cardigannCaptcha")
{
settings.ExtraFieldData["CAPTCHA"] = field.Value?.ToString() ?? string.Empty;
}
else
if (!standardFields.Contains(field.Name))
{
var cardigannSetting = cardigannDefinition.Settings.FirstOrDefault(x => x.Name == field.Name);
settings.ExtraFieldData[field.Name] = MapValue(cardigannSetting, field.Value);
if (field.Name == "cardigannCaptcha")
{
settings.ExtraFieldData["CAPTCHA"] = field.Value?.ToString() ?? string.Empty;
}
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);
}
}
}
}

Loading…
Cancel
Save