ServiceCollectionExtensions.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Jellyfin.Database.Providers.SqLite;
  7. using Jellyfin.Server.Implementations.DatabaseConfiguration;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Controller.Configuration;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using JellyfinDbProviderFactory = System.Func<System.IServiceProvider, Jellyfin.Server.Implementations.IJellyfinDatabaseProvider>;
  13. namespace Jellyfin.Server.Implementations.Extensions;
  14. /// <summary>
  15. /// Extensions for the <see cref="IServiceCollection"/> interface.
  16. /// </summary>
  17. public static class ServiceCollectionExtensions
  18. {
  19. private static IEnumerable<Type> DatabaseProviderTypes()
  20. {
  21. yield return typeof(SqliteDatabaseProvider);
  22. }
  23. private static IDictionary<string, JellyfinDbProviderFactory> GetSupportedDbProviders()
  24. {
  25. var items = new Dictionary<string, JellyfinDbProviderFactory>();
  26. foreach (var providerType in DatabaseProviderTypes())
  27. {
  28. var keyAttribute = providerType.GetCustomAttribute<JellyfinDatabaseProviderKeyAttribute>();
  29. if (keyAttribute is null || string.IsNullOrWhiteSpace(keyAttribute.DatabaseProviderKey))
  30. {
  31. continue;
  32. }
  33. var provider = providerType;
  34. items[keyAttribute.DatabaseProviderKey] = (services) => (IJellyfinDatabaseProvider)ActivatorUtilities.CreateInstance(services, providerType);
  35. }
  36. return items;
  37. }
  38. /// <summary>
  39. /// Adds the <see cref="IDbContextFactory{TContext}"/> interface to the service collection with second level caching enabled.
  40. /// </summary>
  41. /// <param name="serviceCollection">An instance of the <see cref="IServiceCollection"/> interface.</param>
  42. /// <param name="configurationManager">The server configuration manager.</param>
  43. /// <returns>The updated service collection.</returns>
  44. public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection, IServerConfigurationManager configurationManager)
  45. {
  46. var efCoreConfiguration = configurationManager.GetConfiguration<DatabaseConfigurationOptions>("database");
  47. var providers = GetSupportedDbProviders();
  48. JellyfinDbProviderFactory? providerFactory = null;
  49. if (efCoreConfiguration?.DatabaseType is null)
  50. {
  51. // when nothing is setup via new Database configuration, fallback to SqLite with default settings.
  52. efCoreConfiguration = new DatabaseConfigurationOptions()
  53. {
  54. DatabaseType = "Jellyfin-SqLite",
  55. };
  56. }
  57. if (!providers.TryGetValue(efCoreConfiguration.DatabaseType, out providerFactory!))
  58. {
  59. throw new InvalidOperationException($"Jellyfin cannot find the database provider of type '{efCoreConfiguration.DatabaseType}'. Supported types are {string.Join(", ", providers.Keys)}");
  60. }
  61. serviceCollection.AddSingleton<IJellyfinDatabaseProvider>(providerFactory!);
  62. serviceCollection.AddPooledDbContextFactory<JellyfinDbContext>((serviceProvider, opt) =>
  63. {
  64. var provider = serviceProvider.GetRequiredService<IJellyfinDatabaseProvider>();
  65. provider.Initialise(opt);
  66. });
  67. return serviceCollection;
  68. }
  69. }