ServerConfigurationManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using Emby.Server.Implementations.AppBase;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Controller;
  7. using MediaBrowser.Controller.Configuration;
  8. using MediaBrowser.Model.Configuration;
  9. using MediaBrowser.Model.Events;
  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. private void UpdateMetadataPath()
  63. {
  64. if (string.IsNullOrWhiteSpace(Configuration.MetadataPath))
  65. {
  66. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  67. }
  68. else
  69. {
  70. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = Configuration.MetadataPath;
  71. }
  72. }
  73. /// <summary>
  74. /// Replaces the configuration.
  75. /// </summary>
  76. /// <param name="newConfiguration">The new configuration.</param>
  77. /// <exception cref="DirectoryNotFoundException"></exception>
  78. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  79. {
  80. var newConfig = (ServerConfiguration)newConfiguration;
  81. ValidateMetadataPath(newConfig);
  82. ValidateSslCertificate(newConfig);
  83. ConfigurationUpdating?.Invoke(this, new GenericEventArgs<ServerConfiguration>(newConfig));
  84. base.ReplaceConfiguration(newConfiguration);
  85. }
  86. /// <summary>
  87. /// Validates the SSL certificate.
  88. /// </summary>
  89. /// <param name="newConfig">The new configuration.</param>
  90. /// <exception cref="FileNotFoundException">The certificate path doesn't exist.</exception>
  91. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  92. {
  93. var serverConfig = (ServerConfiguration)newConfig;
  94. var newPath = serverConfig.CertificatePath;
  95. if (!string.IsNullOrWhiteSpace(newPath)
  96. && !string.Equals(Configuration.CertificatePath, newPath, StringComparison.Ordinal))
  97. {
  98. // Validate
  99. if (!File.Exists(newPath))
  100. {
  101. throw new FileNotFoundException(
  102. string.Format(
  103. CultureInfo.InvariantCulture,
  104. "Certificate file '{0}' does not exist.",
  105. newPath));
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// Validates the metadata path.
  111. /// </summary>
  112. /// <param name="newConfig">The new configuration.</param>
  113. /// <exception cref="DirectoryNotFoundException">The new config path doesn't exist.</exception>
  114. private void ValidateMetadataPath(ServerConfiguration newConfig)
  115. {
  116. var newPath = newConfig.MetadataPath;
  117. if (!string.IsNullOrWhiteSpace(newPath)
  118. && !string.Equals(Configuration.MetadataPath, newPath, StringComparison.Ordinal))
  119. {
  120. // Validate
  121. if (!Directory.Exists(newPath))
  122. {
  123. throw new DirectoryNotFoundException(
  124. string.Format(
  125. CultureInfo.InvariantCulture,
  126. "{0} does not exist.",
  127. newPath));
  128. }
  129. EnsureWriteAccess(newPath);
  130. }
  131. }
  132. /// <summary>
  133. /// Sets all configuration values to their optimal values.
  134. /// </summary>
  135. /// <returns>If the configuration changed.</returns>
  136. public bool SetOptimalValues()
  137. {
  138. var config = Configuration;
  139. var changed = false;
  140. if (!config.EnableCaseSensitiveItemIds)
  141. {
  142. config.EnableCaseSensitiveItemIds = true;
  143. changed = true;
  144. }
  145. if (!config.SkipDeserializationForBasicTypes)
  146. {
  147. config.SkipDeserializationForBasicTypes = true;
  148. changed = true;
  149. }
  150. if (!config.EnableSimpleArtistDetection)
  151. {
  152. config.EnableSimpleArtistDetection = true;
  153. changed = true;
  154. }
  155. if (!config.EnableNormalizedItemByNameIds)
  156. {
  157. config.EnableNormalizedItemByNameIds = true;
  158. changed = true;
  159. }
  160. if (!config.DisableLiveTvChannelUserDataName)
  161. {
  162. config.DisableLiveTvChannelUserDataName = true;
  163. changed = true;
  164. }
  165. if (!config.EnableNewOmdbSupport)
  166. {
  167. config.EnableNewOmdbSupport = true;
  168. changed = true;
  169. }
  170. if (!config.CameraUploadUpgraded)
  171. {
  172. config.CameraUploadUpgraded = true;
  173. changed = true;
  174. }
  175. if (!config.CollectionsUpgraded)
  176. {
  177. config.CollectionsUpgraded = true;
  178. changed = true;
  179. }
  180. return changed;
  181. }
  182. }
  183. }