fix(admin): fixed various issues lmao idk it was a ballache and is probably quite shit. sorry to future me

This commit is contained in:
Fishandchips321 2026-03-05 19:09:26 +00:00
parent 56ea7fb7f0
commit 2cbbc00489
29 changed files with 382 additions and 131 deletions

View file

@ -1,4 +1,5 @@
using JellyGlass.Exceptions;
using JellyGlass.Models;
using JellyGlass.Repositories;
namespace JellyGlass.Services;
@ -6,7 +7,7 @@ namespace JellyGlass.Services;
public class ClientService : IClientService
{
private IServerRepository _repository;
private static JellyfinApiClient[] _clients = [];
private static List<JellyfinApiClient> _clients = new List<JellyfinApiClient>();
private ILogger<ClientService> _logger;
public ClientService(IServerRepository repository, ILogger<ClientService> logger)
@ -22,59 +23,70 @@ public class ClientService : IClientService
await LoadClients();
}
return _clients;
return _clients.ToArray();
}
public async Task<JellyfinApiClient> GetClientForServerId(string serverId)
public async Task<JellyfinApiClient> GetClientFromUrl(string url)
{
if (!_clients.Any())
{
await LoadClients();
}
foreach (var client in _clients)
var client = _clients.FirstOrDefault(c => c.InstanceUrl == url);
if (client == null)
{
if (client.ID == serverId)
{
return client;
}
throw new Exception($"Could not find client with url {url}");
}
throw new Exception($"Client with ID {serverId} not found");
return client;
}
public async Task LoadNewClient(Server server)
{
var client = new JellyfinApiClient(server.Url, server.ApiToken);
try
{
await client.GetServerInfo();
}
catch (JellyfinApiClientException e)
{
_logger.LogError($"Error authenticating to {server.Url}. Error: {e.Message} Client will not be used");
throw;
}
_clients.Add(client);
}
public void UnloadClient(JellyfinApiClient client)
{
if (_clients.Contains(client))
{
_clients.Remove(client);
}
else
{
throw new Exception($"Haven't loaded Jellyfin api client with ID of {client.ID}");
}
}
private async Task LoadClients()
{
var servers = await _repository.GetServers();
var clients = new List<JellyfinApiClient>();
foreach (var server in servers)
{
var client = new JellyfinApiClient(server.Url, server.ApiToken);
// try
// {
// await client.Authenticate();
// }
// catch (JellyfinApiClientException e)
// {
// _logger.LogError($"Error authenticating to {server.Url}. Error: {e.Message} Client will not be used");
// continue;
// }
try
{
await client.GetServerInfo(); //test the connection
await LoadNewClient(server);
}
catch (JellyfinApiClientException e)
catch (JellyfinApiClientException)
{
_logger.LogError($"Error authenticating to {server.Url}. Error: {e.Message} Client will not be used");
continue;
}
clients.Add(client);
}
_clients = clients.ToArray();
}
}