diff --git a/Kyoo.Authentication/AuthenticationModule.cs b/Kyoo.Authentication/AuthenticationModule.cs
index 66cdf2fd..8e2c78c4 100644
--- a/Kyoo.Authentication/AuthenticationModule.cs
+++ b/Kyoo.Authentication/AuthenticationModule.cs
@@ -84,7 +84,7 @@ namespace Kyoo.Authentication
///
public void Configure(IServiceCollection services, ICollection availableTypes)
{
- string publicUrl = _configuration.GetValue("publicUrl").TrimEnd('/');
+ string publicUrl = _configuration.GetPublicUrl();
if (_environment.IsDevelopment())
IdentityModelEventSource.ShowPII = true;
@@ -146,7 +146,7 @@ namespace Kyoo.Authentication
app.UseAuthentication();
app.Use((ctx, next) =>
{
- ctx.SetIdentityServerOrigin(_configuration.GetValue("publicUrl").TrimEnd('/'));
+ ctx.SetIdentityServerOrigin(_configuration.GetPublicUrl());
return next();
});
app.UseIdentityServer();
diff --git a/Kyoo.Common/Controllers/IConfigurationManager.cs b/Kyoo.Common/Controllers/IConfigurationManager.cs
index 089f7312..9159d92c 100644
--- a/Kyoo.Common/Controllers/IConfigurationManager.cs
+++ b/Kyoo.Common/Controllers/IConfigurationManager.cs
@@ -1,3 +1,4 @@
+using System;
using System.Threading.Tasks;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
@@ -11,6 +12,25 @@ namespace Kyoo.Controllers
///
public interface IConfigurationManager
{
+ ///
+ /// Get the value of a setting using it's path.
+ ///
+ /// The path of the resource (can be separated by ':' or '__')
+ /// No setting found at the given path.
+ /// The value of the settings (if it's a strongly typed one, the given type is instantiated
+ object GetValue(string path);
+
+ ///
+ /// Get the value of a setting using it's path.
+ /// If your don't need a strongly typed value, see .
+ ///
+ /// The path of the resource (can be separated by ':' or '__')
+ /// A type to strongly type your option.
+ /// If your type is not the same as the registered type
+ /// No setting found at the given path.
+ /// The value of the settings (if it's a strongly typed one, the given type is instantiated
+ T GetValue(string path);
+
///
/// Edit the value of a setting using it's path. Save it to the json file.
///
diff --git a/Kyoo.Common/Module.cs b/Kyoo.Common/Module.cs
index e8fcf1b6..0b5e0d04 100644
--- a/Kyoo.Common/Module.cs
+++ b/Kyoo.Common/Module.cs
@@ -2,6 +2,7 @@ using System;
using System.Linq;
using Kyoo.Controllers;
using Kyoo.Models;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo
@@ -81,5 +82,15 @@ namespace Kyoo
services.AddSingleton(confRef);
return services;
}
+
+ ///
+ /// Get the public URL of kyoo using the given configuration instance.
+ ///
+ /// The configuration instance
+ /// The public URl of kyoo (without a slash at the end)
+ public static string GetPublicUrl(this IConfiguration configuration)
+ {
+ return configuration["basics:publicUrl"]?.TrimEnd('/') ?? "http://localhost:5000";
+ }
}
}
\ No newline at end of file
diff --git a/Kyoo.CommonAPI/CrudApi.cs b/Kyoo.CommonAPI/CrudApi.cs
index 75a2758c..b6d03580 100644
--- a/Kyoo.CommonAPI/CrudApi.cs
+++ b/Kyoo.CommonAPI/CrudApi.cs
@@ -7,7 +7,6 @@ using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Kyoo.Models.Permissions;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.Configuration;
namespace Kyoo.CommonApi
{
@@ -18,10 +17,10 @@ namespace Kyoo.CommonApi
private readonly IRepository _repository;
protected readonly string BaseURL;
- public CrudApi(IRepository repository, IConfiguration configuration)
+ public CrudApi(IRepository repository, string baseURL)
{
_repository = repository;
- BaseURL = configuration.GetValue("publicUrl").TrimEnd('/');
+ BaseURL = baseURL;
}
diff --git a/Kyoo.Postgresql/PostgresModule.cs b/Kyoo.Postgresql/PostgresModule.cs
index 7a818296..f0c8f23c 100644
--- a/Kyoo.Postgresql/PostgresModule.cs
+++ b/Kyoo.Postgresql/PostgresModule.cs
@@ -66,10 +66,6 @@ namespace Kyoo.Postgresql
if (_environment.IsDevelopment())
x.EnableDetailedErrors().EnableSensitiveDataLogging();
});
- // services.AddScoped(_ => new PostgresContext(
- // _configuration.GetDatabaseConnection("postgres"),
- // _environment.IsDevelopment()));
- // services.AddScoped(x => x.GetRequiredService());
}
///
diff --git a/Kyoo/Controllers/ConfigurationManager.cs b/Kyoo/Controllers/ConfigurationManager.cs
index 42bdf433..c0a1340b 100644
--- a/Kyoo/Controllers/ConfigurationManager.cs
+++ b/Kyoo/Controllers/ConfigurationManager.cs
@@ -35,6 +35,34 @@ namespace Kyoo.Controllers
_references = references.ToDictionary(x => x.Path, x => x.Type, StringComparer.OrdinalIgnoreCase);
}
+ ///
+ public object GetValue(string path)
+ {
+ path = path.Replace("__", ":");
+ // TODO handle lists and dictionaries.
+ if (!_references.TryGetValue(path, out Type type))
+ throw new ItemNotFoundException($"No configuration exists for the name: {path}");
+ object ret = _configuration.GetValue(type, path);
+ if (ret != null)
+ return ret;
+ object option = Activator.CreateInstance(type);
+ _configuration.Bind(path, option);
+ return option;
+ }
+
+ ///
+ public T GetValue(string path)
+ {
+ path = path.Replace("__", ":");
+ // TODO handle lists and dictionaries.
+ if (!_references.TryGetValue(path, out Type type))
+ throw new ItemNotFoundException($"No configuration exists for the name: {path}");
+ if (typeof(T).IsAssignableFrom(type))
+ throw new InvalidCastException($"The type {typeof(T).Name} is not valid for " +
+ $"a resource of type {type.Name}.");
+ return (T)GetValue(path);
+ }
+
///
public async Task EditValue(string path, object value)
{
diff --git a/Kyoo/Controllers/PluginManager.cs b/Kyoo/Controllers/PluginManager.cs
index 321d2f57..7d0c399b 100644
--- a/Kyoo/Controllers/PluginManager.cs
+++ b/Kyoo/Controllers/PluginManager.cs
@@ -4,10 +4,11 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
+using Kyoo.Models.Options;
using Microsoft.AspNetCore.Builder;
-using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
namespace Kyoo.Controllers
{
@@ -24,7 +25,7 @@ namespace Kyoo.Controllers
///
/// The configuration to get the plugin's directory.
///
- private readonly IConfiguration _config;
+ private readonly IOptionsMonitor _options;
///
/// The logger used by this class.
///
@@ -39,14 +40,14 @@ namespace Kyoo.Controllers
/// Create a new instance.
///
/// A service container to allow initialization of plugins
- /// The configuration instance, to get the plugin's directory path.
+ /// The configuration instance, to get the plugin's directory path.
/// The logger used by this class.
public PluginManager(IServiceProvider provider,
- IConfiguration config,
+ IOptionsMonitor options,
ILogger logger)
{
_provider = provider;
- _config = config;
+ _options = options;
_logger = logger;
}
@@ -97,7 +98,7 @@ namespace Kyoo.Controllers
///
public void LoadPlugins(ICollection plugins)
{
- string pluginFolder = _config.GetValue("plugins");
+ string pluginFolder = _options.CurrentValue.PluginPath;
if (!Directory.Exists(pluginFolder))
Directory.CreateDirectory(pluginFolder);
diff --git a/Kyoo/Controllers/TaskManager.cs b/Kyoo/Controllers/TaskManager.cs
index 5f281ba3..caa7a01d 100644
--- a/Kyoo/Controllers/TaskManager.cs
+++ b/Kyoo/Controllers/TaskManager.cs
@@ -7,10 +7,11 @@ using System.Threading.Tasks;
using JetBrains.Annotations;
using Kyoo.Models.Attributes;
using Kyoo.Models.Exceptions;
-using Microsoft.Extensions.Configuration;
+using Kyoo.Models.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
namespace Kyoo.Controllers
{
@@ -27,7 +28,7 @@ namespace Kyoo.Controllers
///
/// The configuration instance used to get schedule information
///
- private readonly IConfiguration _configuration;
+ private readonly IOptionsMonitor _options;
///
/// The logger instance.
///
@@ -56,15 +57,15 @@ namespace Kyoo.Controllers
///
/// The list of tasks to manage
/// The service provider to request services for tasks
- /// The configuration to load schedule information.
+ /// The configuration to load schedule information.
/// The logger.
public TaskManager(IEnumerable tasks,
IServiceProvider provider,
- IConfiguration configuration,
+ IOptionsMonitor options,
ILogger logger)
{
_provider = provider;
- _configuration = configuration.GetSection("scheduledTasks");
+ _options = options;
_logger = logger;
_tasks = tasks.Select(x => (x, GetNextTaskDate(x.Slug))).ToList();
@@ -224,10 +225,9 @@ namespace Kyoo.Controllers
/// The next date.
private DateTime GetNextTaskDate(string taskSlug)
{
- TimeSpan delay = _configuration.GetValue(taskSlug);
- if (delay == default)
- return DateTime.MaxValue;
- return DateTime.Now + delay;
+ if (_options.CurrentValue.Scheduled.TryGetValue(taskSlug, out TimeSpan delay))
+ return DateTime.Now + delay;
+ return DateTime.MaxValue;
}
///
diff --git a/Kyoo/Controllers/ThumbnailsManager.cs b/Kyoo/Controllers/ThumbnailsManager.cs
index b75e3231..1e84cef6 100644
--- a/Kyoo/Controllers/ThumbnailsManager.cs
+++ b/Kyoo/Controllers/ThumbnailsManager.cs
@@ -1,26 +1,25 @@
using Kyoo.Models;
-using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using JetBrains.Annotations;
+using Kyoo.Models.Options;
+using Microsoft.Extensions.Options;
namespace Kyoo.Controllers
{
public class ThumbnailsManager : IThumbnailsManager
{
private readonly IFileManager _files;
- private readonly string _peoplePath;
- private readonly string _providerPath;
+ private readonly IOptionsMonitor _options;
- public ThumbnailsManager(IConfiguration configuration, IFileManager files)
+ public ThumbnailsManager(IFileManager files, IOptionsMonitor options)
{
_files = files;
- _peoplePath = Path.GetFullPath(configuration.GetValue("peoplePath"));
- _providerPath = Path.GetFullPath(configuration.GetValue("providerPath"));
- Directory.CreateDirectory(_peoplePath);
- Directory.CreateDirectory(_providerPath);
+ _options = options;
+ Directory.CreateDirectory(_options.CurrentValue.PeoplePath);
+ Directory.CreateDirectory(_options.CurrentValue.ProviderPath);
}
private static async Task DownloadImage(string url, string localPath, string what)
@@ -141,16 +140,18 @@ namespace Kyoo.Controllers
{
if (people == null)
throw new ArgumentNullException(nameof(people));
- string thumbPath = Path.GetFullPath(Path.Combine(_peoplePath, $"{people.Slug}.jpg"));
- return Task.FromResult(thumbPath.StartsWith(_peoplePath) ? thumbPath : null);
+ string peoplePath = _options.CurrentValue.PeoplePath;
+ string thumbPath = Path.GetFullPath(Path.Combine(peoplePath, $"{people.Slug}.jpg"));
+ return Task.FromResult(thumbPath.StartsWith(peoplePath) ? thumbPath : null);
}
public Task GetProviderLogo(Provider provider)
{
if (provider == null)
throw new ArgumentNullException(nameof(provider));
- string thumbPath = Path.GetFullPath(Path.Combine(_providerPath, $"{provider.Slug}.{provider.LogoExtension}"));
- return Task.FromResult(thumbPath.StartsWith(_providerPath) ? thumbPath : null);
+ string providerPath = _options.CurrentValue.ProviderPath;
+ string thumbPath = Path.GetFullPath(Path.Combine(providerPath, $"{provider.Slug}.{provider.LogoExtension}"));
+ return Task.FromResult(thumbPath.StartsWith(providerPath) ? thumbPath : null);
}
}
}
diff --git a/Kyoo/Controllers/Transcoder.cs b/Kyoo/Controllers/Transcoder.cs
index 54cb5bfc..823aa9d2 100644
--- a/Kyoo/Controllers/Transcoder.cs
+++ b/Kyoo/Controllers/Transcoder.cs
@@ -3,7 +3,8 @@ using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Kyoo.Models;
-using Microsoft.Extensions.Configuration;
+using Kyoo.Models.Options;
+using Microsoft.Extensions.Options;
using Stream = Kyoo.Models.Watch.Stream;
// We use threads so tasks are not always awaited.
@@ -66,20 +67,18 @@ namespace Kyoo.Controllers
tracks = Array.Empty