ServerConfigurationManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using Emby.Server.Implementations.AppBase;
  5. using Jellyfin.Data.Events;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Controller;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Model.Configuration;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Serialization;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.Configuration
  14. {
  15. /// <summary>
  16. /// Class ServerConfigurationManager.
  17. /// </summary>
  18. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  19. {
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  22. /// </summary>
  23. /// <param name="applicationPaths">The application paths.</param>
  24. /// <param name="loggerFactory">The paramref name="loggerFactory" factory.</param>
  25. /// <param name="xmlSerializer">The XML serializer.</param>
  26. /// <param name="fileSystem">The file system.</param>
  27. public ServerConfigurationManager(IApplicationPaths applicationPaths, ILoggerFactory loggerFactory, IXmlSerializer xmlSerializer, IFileSystem fileSystem)
  28. : base(applicationPaths, loggerFactory, xmlSerializer, fileSystem)
  29. {
  30. UpdateMetadataPath();
  31. }
  32. /// <summary>
  33. /// Configuration updating event.
  34. /// </summary>
  35. public event EventHandler<GenericEventArgs<ServerConfiguration>> ConfigurationUpdating;
  36. /// <summary>
  37. /// Gets the type of the configuration.
  38. /// </summary>
  39. /// <value>The type of the configuration.</value>
  40. protected override Type ConfigurationType => typeof(ServerConfiguration);
  41. /// <summary>
  42. /// Gets the application paths.
  43. /// </summary>
  44. /// <value>The application paths.</value>
  45. public IServerApplicationPaths ApplicationPaths => (IServerApplicationPaths)CommonApplicationPaths;
  46. /// <summary>
  47. /// Gets the configuration.
  48. /// </summary>
  49. /// <value>The configuration.</value>
  50. public ServerConfiguration Configuration => (ServerConfiguration)CommonConfiguration;
  51. /// <summary>
  52. /// Called when [configuration updated].
  53. /// </summary>
  54. protected override void OnConfigurationUpdated()
  55. {
  56. UpdateMetadataPath();
  57. base.OnConfigurationUpdated();
  58. }
  59. /// <summary>
  60. /// Updates the metadata path.
  61. /// </summary>
  62. /// <exception cref="UnauthorizedAccessException">If the directory does not exist, and the caller does not have the required permission to create it.</exception>
  63. /// <exception cref="NotSupportedException">If there is a custom path transcoding path specified, but it is invalid.</exception>
  64. /// <exception cref="IOException">If the directory does not exist, and it also could not be created.</exception>
  65. private void UpdateMetadataPath()
  66. {
  67. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = string.IsNullOrWhiteSpace(Configuration.MetadataPath)
  68. ? ApplicationPaths.DefaultInternalMetadataPath
  69. : Configuration.MetadataPath;
  70. Directory.CreateDirectory(ApplicationPaths.InternalMetadataPath);
  71. }
  72. /// <summary>
  73. /// Replaces the configuration.
  74. /// </summary>
  75. /// <param name="newConfiguration">The new configuration.</param>
  76. /// <exception cref="DirectoryNotFoundException">If the configuration path doesn't exist.</exception>
  77. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  78. {
  79. var newConfig = (ServerConfiguration)newConfiguration;
  80. ValidateMetadataPath(newConfig);
  81. ValidateSslCertificate(newConfig);
  82. ConfigurationUpdating?.Invoke(this, new GenericEventArgs<ServerConfiguration>(newConfig));
  83. base.ReplaceConfiguration(newConfiguration);
  84. }
  85. /// <summary>
  86. /// Validates the SSL certificate.
  87. /// </summary>
  88. /// <param name="newConfig">The new configuration.</param>
  89. /// <exception cref="FileNotFoundException">The certificate path doesn't exist.</exception>
  90. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  91. {
  92. var serverConfig = (ServerConfiguration)newConfig;
  93. var newPath = serverConfig.CertificatePath;
  94. if (!string.IsNullOrWhiteSpace(newPath)
  95. && !string.Equals(Configuration.CertificatePath, newPath, StringComparison.Ordinal))
  96. {
  97. if (!File.Exists(newPath))
  98. {
  99. throw new FileNotFoundException(
  100. string.Format(
  101. CultureInfo.InvariantCulture,
  102. "Certificate file '{0}' does not exist.",
  103. newPath));
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Validates the metadata path.
  109. /// </summary>
  110. /// <param name="newConfig">The new configuration.</param>
  111. /// <exception cref="DirectoryNotFoundException">The new config path doesn't exist.</exception>
  112. private void ValidateMetadataPath(ServerConfiguration newConfig)
  113. {
  114. var newPath = newConfig.MetadataPath;
  115. if (!string.IsNullOrWhiteSpace(newPath)
  116. && !string.Equals(Configuration.MetadataPath, newPath, StringComparison.Ordinal))
  117. {
  118. if (!Directory.Exists(newPath))
  119. {
  120. throw new DirectoryNotFoundException(
  121. string.Format(
  122. CultureInfo.InvariantCulture,
  123. "{0} does not exist.",
  124. newPath));
  125. }
  126. EnsureWriteAccess(newPath);
  127. }
  128. }
  129. }
  130. }