ServerConfigurationManager.cs 7.8 KB

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