ServerConfigurationManager.cs 6.6 KB

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