ServerConfigurationManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using Emby.Server.Implementations.AppBase;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Controller;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Model.Configuration;
  10. using MediaBrowser.Model.Events;
  11. using MediaBrowser.Model.IO;
  12. using MediaBrowser.Model.Serialization;
  13. using Microsoft.Extensions.Logging;
  14. namespace Emby.Server.Implementations.Configuration
  15. {
  16. /// <summary>
  17. /// Class ServerConfigurationManager.
  18. /// </summary>
  19. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  20. {
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  23. /// </summary>
  24. /// <param name="applicationPaths">The application paths.</param>
  25. /// <param name="loggerFactory">The paramref name="loggerFactory" factory.</param>
  26. /// <param name="xmlSerializer">The XML serializer.</param>
  27. /// <param name="fileSystem">The file system.</param>
  28. public ServerConfigurationManager(IApplicationPaths applicationPaths, ILoggerFactory loggerFactory, IXmlSerializer xmlSerializer, IFileSystem fileSystem)
  29. : base(applicationPaths, loggerFactory, xmlSerializer, fileSystem)
  30. {
  31. UpdateMetadataPath();
  32. }
  33. /// <summary>
  34. /// Configuration updating event.
  35. /// </summary>
  36. public event EventHandler<GenericEventArgs<ServerConfiguration>> ConfigurationUpdating;
  37. /// <summary>
  38. /// Gets the type of the configuration.
  39. /// </summary>
  40. /// <value>The type of the configuration.</value>
  41. protected override Type ConfigurationType => typeof(ServerConfiguration);
  42. /// <summary>
  43. /// Gets the application paths.
  44. /// </summary>
  45. /// <value>The application paths.</value>
  46. public IServerApplicationPaths ApplicationPaths => (IServerApplicationPaths)CommonApplicationPaths;
  47. /// <summary>
  48. /// Gets the configuration.
  49. /// </summary>
  50. /// <value>The configuration.</value>
  51. public ServerConfiguration Configuration => (ServerConfiguration)CommonConfiguration;
  52. /// <summary>
  53. /// Called when [configuration updated].
  54. /// </summary>
  55. protected override void OnConfigurationUpdated()
  56. {
  57. UpdateMetadataPath();
  58. base.OnConfigurationUpdated();
  59. }
  60. /// <summary>
  61. /// Updates the metadata path.
  62. /// </summary>
  63. private void UpdateMetadataPath()
  64. {
  65. if (string.IsNullOrWhiteSpace(Configuration.MetadataPath))
  66. {
  67. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  68. }
  69. else
  70. {
  71. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = Configuration.MetadataPath;
  72. }
  73. }
  74. /// <summary>
  75. /// Replaces the configuration.
  76. /// </summary>
  77. /// <param name="newConfiguration">The new configuration.</param>
  78. /// <exception cref="DirectoryNotFoundException"></exception>
  79. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  80. {
  81. var newConfig = (ServerConfiguration)newConfiguration;
  82. ValidateMetadataPath(newConfig);
  83. ValidateSslCertificate(newConfig);
  84. ConfigurationUpdating?.Invoke(this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig });
  85. base.ReplaceConfiguration(newConfiguration);
  86. }
  87. /// <summary>
  88. /// Validates the SSL certificate.
  89. /// </summary>
  90. /// <param name="newConfig">The new configuration.</param>
  91. /// <exception cref="FileNotFoundException">The certificate path doesn't exist.</exception>
  92. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  93. {
  94. var serverConfig = (ServerConfiguration)newConfig;
  95. var newPath = serverConfig.CertificatePath;
  96. if (!string.IsNullOrWhiteSpace(newPath)
  97. && !string.Equals(Configuration.CertificatePath, newPath, StringComparison.Ordinal))
  98. {
  99. // Validate
  100. if (!File.Exists(newPath))
  101. {
  102. throw new FileNotFoundException(
  103. string.Format(
  104. CultureInfo.InvariantCulture,
  105. "Certificate file '{0}' does not exist.",
  106. newPath));
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// Validates the metadata path.
  112. /// </summary>
  113. /// <param name="newConfig">The new configuration.</param>
  114. /// <exception cref="DirectoryNotFoundException">The new config path doesn't exist.</exception>
  115. private void ValidateMetadataPath(ServerConfiguration newConfig)
  116. {
  117. var newPath = newConfig.MetadataPath;
  118. if (!string.IsNullOrWhiteSpace(newPath)
  119. && !string.Equals(Configuration.MetadataPath, newPath, StringComparison.Ordinal))
  120. {
  121. // Validate
  122. if (!Directory.Exists(newPath))
  123. {
  124. throw new DirectoryNotFoundException(
  125. string.Format(
  126. CultureInfo.InvariantCulture,
  127. "{0} does not exist.",
  128. newPath));
  129. }
  130. EnsureWriteAccess(newPath);
  131. }
  132. }
  133. /// <summary>
  134. /// Sets all configuration values to their optimal values.
  135. /// </summary>
  136. /// <returns>If the configuration changed.</returns>
  137. public bool SetOptimalValues()
  138. {
  139. var config = Configuration;
  140. var changed = false;
  141. if (!config.EnableCaseSensitiveItemIds)
  142. {
  143. config.EnableCaseSensitiveItemIds = true;
  144. changed = true;
  145. }
  146. if (!config.SkipDeserializationForBasicTypes)
  147. {
  148. config.SkipDeserializationForBasicTypes = true;
  149. changed = true;
  150. }
  151. if (!config.EnableSimpleArtistDetection)
  152. {
  153. config.EnableSimpleArtistDetection = true;
  154. changed = true;
  155. }
  156. if (!config.EnableNormalizedItemByNameIds)
  157. {
  158. config.EnableNormalizedItemByNameIds = true;
  159. changed = true;
  160. }
  161. if (!config.DisableLiveTvChannelUserDataName)
  162. {
  163. config.DisableLiveTvChannelUserDataName = true;
  164. changed = true;
  165. }
  166. if (!config.EnableNewOmdbSupport)
  167. {
  168. config.EnableNewOmdbSupport = true;
  169. changed = true;
  170. }
  171. if (!config.CameraUploadUpgraded)
  172. {
  173. config.CameraUploadUpgraded = true;
  174. changed = true;
  175. }
  176. if (!config.CollectionsUpgraded)
  177. {
  178. config.CollectionsUpgraded = true;
  179. changed = true;
  180. }
  181. return changed;
  182. }
  183. }
  184. }