ServiceCollectionExtensions.cs 4.1 KB

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