CoreAppHost.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using Emby.Drawing;
  6. using Emby.Server.Implementations;
  7. using Jellyfin.Drawing.Skia;
  8. using Jellyfin.Server.Implementations;
  9. using Jellyfin.Server.Implementations.Activity;
  10. using Jellyfin.Server.Implementations.Users;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Controller;
  13. using MediaBrowser.Controller.Drawing;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Model.Activity;
  16. using MediaBrowser.Model.IO;
  17. using Microsoft.EntityFrameworkCore;
  18. using Microsoft.Extensions.DependencyInjection;
  19. using Microsoft.Extensions.Logging;
  20. namespace Jellyfin.Server
  21. {
  22. /// <summary>
  23. /// Implementation of the abstract <see cref="ApplicationHost" /> class.
  24. /// </summary>
  25. public class CoreAppHost : ApplicationHost
  26. {
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="CoreAppHost" /> class.
  29. /// </summary>
  30. /// <param name="applicationPaths">The <see cref="ServerApplicationPaths" /> to be used by the <see cref="CoreAppHost" />.</param>
  31. /// <param name="loggerFactory">The <see cref="ILoggerFactory" /> to be used by the <see cref="CoreAppHost" />.</param>
  32. /// <param name="options">The <see cref="StartupOptions" /> to be used by the <see cref="CoreAppHost" />.</param>
  33. /// <param name="fileSystem">The <see cref="IFileSystem" /> to be used by the <see cref="CoreAppHost" />.</param>
  34. /// <param name="networkManager">The <see cref="INetworkManager" /> to be used by the <see cref="CoreAppHost" />.</param>
  35. public CoreAppHost(
  36. IServerApplicationPaths applicationPaths,
  37. ILoggerFactory loggerFactory,
  38. IStartupOptions options,
  39. IFileSystem fileSystem,
  40. INetworkManager networkManager)
  41. : base(
  42. applicationPaths,
  43. loggerFactory,
  44. options,
  45. fileSystem,
  46. networkManager)
  47. {
  48. }
  49. /// <inheritdoc/>
  50. protected override void RegisterServices(IServiceCollection serviceCollection)
  51. {
  52. // Register an image encoder
  53. bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();
  54. Type imageEncoderType = useSkiaEncoder
  55. ? typeof(SkiaEncoder)
  56. : typeof(NullImageEncoder);
  57. serviceCollection.AddSingleton(typeof(IImageEncoder), imageEncoderType);
  58. // Log a warning if the Skia encoder could not be used
  59. if (!useSkiaEncoder)
  60. {
  61. Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}.");
  62. }
  63. // TODO: Set up scoping and use AddDbContextPool,
  64. // can't register as Transient since tracking transient in GC is funky
  65. // serviceCollection.AddDbContext<JellyfinDb>(
  66. // options => options
  67. // .UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"),
  68. // ServiceLifetime.Transient);
  69. serviceCollection.AddSingleton<JellyfinDbProvider>();
  70. serviceCollection.AddSingleton<IActivityManager, ActivityManager>();
  71. serviceCollection.AddSingleton<IUserManager, UserManager>();
  72. serviceCollection.AddSingleton<IDisplayPreferencesManager, DisplayPreferencesManager>();
  73. base.RegisterServices(serviceCollection);
  74. }
  75. /// <inheritdoc />
  76. protected override void RestartInternal() => Program.Restart();
  77. /// <inheritdoc />
  78. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  79. {
  80. // Jellyfin.Server
  81. yield return typeof(CoreAppHost).Assembly;
  82. // Jellyfin.Server.Implementations
  83. yield return typeof(JellyfinDb).Assembly;
  84. }
  85. /// <inheritdoc />
  86. protected override void ShutdownInternal() => Program.Shutdown();
  87. }
  88. }