Fix warnings

This commit is contained in:
pcjones
2024-09-04 19:39:15 +02:00
parent 238bd9cc60
commit 370e3ca06b
8 changed files with 41 additions and 47 deletions

View File

@@ -23,7 +23,7 @@ namespace UmlautAdaptarr.Controllers
return NotFound($"{domain} is not a valid URL.");
}
var initialSearchResult = await PerformSingleSearchRequest(domain, queryParameters) as ContentResult;
ContentResult? initialSearchResult = await PerformSingleSearchRequest(domain, queryParameters) as ContentResult;
if (initialSearchResult == null)
{
return null;

View File

@@ -5,29 +5,29 @@ namespace UmlautAdaptarr.Utilities;
public class IpInfo
{
[JsonPropertyName("ip")]
public string Ip { get; set; }
public string? Ip { get; set; }
[JsonPropertyName("hostname")]
public string Hostname { get; set; }
public string? Hostname { get; set; }
[JsonPropertyName("city")]
public string City { get; set; }
public string? City { get; set; }
[JsonPropertyName("region")]
public string Region { get; set; }
public string? Region { get; set; }
[JsonPropertyName("country")]
public string Country { get; set; }
public string? Country { get; set; }
[JsonPropertyName("loc")]
public string Loc { get; set; }
public string? Loc { get; set; }
[JsonPropertyName("org")]
public string Org { get; set; }
public string? Org { get; set; }
[JsonPropertyName("postal")]
public string Postal { get; set; }
public string? Postal { get; set; }
[JsonPropertyName("timezone")]
public string Timezone { get; set; }
public string? Timezone { get; set; }
}

View File

@@ -166,7 +166,7 @@ namespace UmlautAdaptarr.Models
}
}
private IEnumerable<string> GenerateVariations(string? title, string mediaType)
private static IEnumerable<string> GenerateVariations(string? title, string mediaType)
{
if (title == null)
{

View File

@@ -5,7 +5,9 @@ namespace UmlautAdaptarr.Providers;
public abstract class ArrClientBase : IArrApplication
{
#pragma warning disable CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable.
public string InstanceName;
#pragma warning restore CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable.
public abstract Task<IEnumerable<SearchItem>> FetchAllItemsAsync();
public abstract Task<SearchItem?> FetchItemByExternalIdAsync(string externalId);
public abstract Task<SearchItem?> FetchItemByTitleAsync(string title);

View File

@@ -11,8 +11,8 @@ namespace UmlautAdaptarr.Services
private readonly int _proxyPort = 5006; // TODO move to appsettings.json
private readonly IHttpClientFactory _clientFactory;
private readonly HashSet<string> _knownHosts = [];
private readonly object _hostsLock = new object();
private readonly object _hostsLock = new();
private static readonly string[] newLineSeparator = ["\r\n"];
public HttpProxyService(ILogger<HttpProxyService> logger, IHttpClientFactory clientFactory)
{
@@ -123,7 +123,7 @@ namespace UmlautAdaptarr.Services
{
var headers = new Dictionary<string, string>();
var headerString = Encoding.ASCII.GetString(buffer, 0, length);
var lines = headerString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
var lines = headerString.Split(newLineSeparator, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines.Skip(1)) // Skip the request line
{
var colonIndex = line.IndexOf(':');
@@ -137,7 +137,7 @@ namespace UmlautAdaptarr.Services
return headers;
}
private (string host, int port) ParseTargetInfo(string requestLine)
private static (string host, int port) ParseTargetInfo(string requestLine)
{
var parts = requestLine.Split(' ')[1].Split(':');
return (parts[0], int.Parse(parts[1]));
@@ -150,7 +150,7 @@ namespace UmlautAdaptarr.Services
await Task.WhenAll(clientToTargetTask, targetToClientTask);
}
private async Task RelayStream(NetworkStream input, NetworkStream output)
private static async Task RelayStream(NetworkStream input, NetworkStream output)
{
byte[] buffer = new byte[8192];
int bytesRead;

View File

@@ -69,11 +69,11 @@ namespace UmlautAdaptarr.Services
public void FindAndReplaceForBooksAndAudio(SearchItem searchItem, XElement? titleElement, string originalTitle)
{
var authorMatch = FindBestMatch(searchItem.AuthorMatchVariations, originalTitle.NormalizeForComparison(), originalTitle);
var titleMatch = FindBestMatch(searchItem.TitleMatchVariations, originalTitle.NormalizeForComparison(), originalTitle);
var (foundMatch, bestStart, bestEndInOriginal) = FindBestMatch(searchItem.TitleMatchVariations, originalTitle.NormalizeForComparison(), originalTitle);
if (authorMatch.foundMatch && titleMatch.foundMatch)
if (authorMatch.foundMatch && foundMatch)
{
int matchEndPositionInOriginal = Math.Max(authorMatch.bestEndInOriginal, titleMatch.bestEndInOriginal);
int matchEndPositionInOriginal = Math.Max(authorMatch.bestEndInOriginal, bestEndInOriginal);
// Check and adjust for immediate following delimiter
char[] delimiters = [' ', '-', '_', '.'];
@@ -103,7 +103,7 @@ namespace UmlautAdaptarr.Services
}
private (bool foundMatch, int bestStart, int bestEndInOriginal) FindBestMatch(string[] variations, string normalizedOriginal, string originalTitle)
private static (bool foundMatch, int bestStart, int bestEndInOriginal) FindBestMatch(string[] variations, string normalizedOriginal, string originalTitle)
{
bool found = false;
int bestStart = int.MaxValue;
@@ -131,7 +131,7 @@ namespace UmlautAdaptarr.Services
}
// Maps an index from the normalized string back to a corresponding index in the original string
private int MapNormalizedIndexToOriginal(string normalizedOriginal, string originalTitle, int normalizedIndex)
private static int MapNormalizedIndexToOriginal(string normalizedOriginal, string originalTitle, int normalizedIndex)
{
// Count non-special characters up to the given index in the normalized string
int nonSpecialCharCount = 0;

View File

@@ -38,8 +38,7 @@ public static class Helper
private static async Task<IpInfo?> GetPublicIpAddressInfoAsync()
{
using (var client = new HttpClient())
{
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
try
@@ -54,5 +53,4 @@ public static class Helper
return null;
}
}
}
}

View File

@@ -35,17 +35,15 @@ public static class ServicesExtensions
where TService : class, TInterface
where TInterface : class
{
try
{
if (builder.Services == null) throw new ArgumentNullException(nameof(builder), "Service collection is null.");
var singleInstance = builder.Configuration.GetSection(sectionName).Get<TOptions>();
var singleHost = (string?)typeof(TOptions).GetProperty("Host")?.GetValue(singleInstance, null);
// If we have no Single Instance , we try to parse for a Array
// If we have no Single Instance, we try to parse for an Array
var optionsArray = singleHost == null
? builder.Configuration.GetSection(sectionName).Get<TOptions[]>()
:
@@ -59,7 +57,6 @@ public static class ServicesExtensions
foreach (var option in optionsArray)
{
GlobalInstanceOptionsValidator validator = new GlobalInstanceOptionsValidator();
var results = validator.Validate(option as GlobalInstanceOptions);
@@ -100,7 +97,7 @@ public static class ServicesExtensions
}
else
{
Logger.LogWarning((prop.PropertyType + "No Support"));
Logger.LogWarning(prop.PropertyType + "No Support");
}
}
@@ -110,9 +107,9 @@ public static class ServicesExtensions
return builder;
}
catch (Exception e)
catch (Exception ex)
{
Console.WriteLine("Error while Init UmlautAdaptrr");
Console.WriteLine($"Error in AddServicesWithOptions: {ex.Message}");
throw;
}
@@ -133,11 +130,8 @@ public static class ServicesExtensions
{
if (builder.Services == null) throw new ArgumentNullException(nameof(builder), "Service collection is null.");
var options = builder.Configuration.GetSection(sectionName).Get<TOptions>();
if (options == null)
throw new InvalidOperationException(
var options = builder.Configuration.GetSection(sectionName).Get<TOptions>() ?? throw new InvalidOperationException(
$"{typeof(TService).Name} options could not be loaded from Configuration or ENV Variable.");
builder.Services.Configure<TOptions>(builder.Configuration.GetSection(sectionName));
builder.Services.AddSingleton<TService>();