ServerConfigurationManager.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  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. public event EventHandler<GenericEventArgs<ServerConfiguration>> ConfigurationUpdating;
  33. /// <summary>
  34. /// Gets the type of the configuration.
  35. /// </summary>
  36. /// <value>The type of the configuration.</value>
  37. protected override Type ConfigurationType => typeof(ServerConfiguration);
  38. /// <summary>
  39. /// Gets the application paths.
  40. /// </summary>
  41. /// <value>The application paths.</value>
  42. public IServerApplicationPaths ApplicationPaths => (IServerApplicationPaths)CommonApplicationPaths;
  43. /// <summary>
  44. /// Gets the configuration.
  45. /// </summary>
  46. /// <value>The configuration.</value>
  47. public ServerConfiguration Configuration => (ServerConfiguration)CommonConfiguration;
  48. /// <summary>
  49. /// Called when [configuration updated].
  50. /// </summary>
  51. protected override void OnConfigurationUpdated()
  52. {
  53. UpdateMetadataPath();
  54. base.OnConfigurationUpdated();
  55. }
  56. public override void AddParts(IEnumerable<IConfigurationFactory> factories)
  57. {
  58. base.AddParts(factories);
  59. UpdateTranscodePath();
  60. }
  61. /// <summary>
  62. /// Updates the metadata path.
  63. /// </summary>
  64. private void UpdateMetadataPath()
  65. {
  66. if (string.IsNullOrWhiteSpace(Configuration.MetadataPath))
  67. {
  68. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  69. }
  70. else
  71. {
  72. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = Configuration.MetadataPath;
  73. }
  74. }
  75. /// <summary>
  76. /// Updates the transcoding temporary path.
  77. /// </summary>
  78. private void UpdateTranscodePath()
  79. {
  80. var encodingConfig = this.GetConfiguration<EncodingOptions>("encoding");
  81. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(encodingConfig.TranscodingTempPath) ?
  82. null :
  83. Path.Combine(encodingConfig.TranscodingTempPath, "transcodes");
  84. }
  85. protected override void OnNamedConfigurationUpdated(string key, object configuration)
  86. {
  87. base.OnNamedConfigurationUpdated(key, configuration);
  88. if (string.Equals(key, "encoding", StringComparison.OrdinalIgnoreCase))
  89. {
  90. UpdateTranscodePath();
  91. }
  92. }
  93. /// <summary>
  94. /// Replaces the configuration.
  95. /// </summary>
  96. /// <param name="newConfiguration">The new configuration.</param>
  97. /// <exception cref="DirectoryNotFoundException"></exception>
  98. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  99. {
  100. var newConfig = (ServerConfiguration)newConfiguration;
  101. ValidateMetadataPath(newConfig);
  102. ValidateSslCertificate(newConfig);
  103. ConfigurationUpdating?.Invoke(this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig });
  104. base.ReplaceConfiguration(newConfiguration);
  105. }
  106. /// <summary>
  107. /// Validates the SSL certificate.
  108. /// </summary>
  109. /// <param name="newConfig">The new configuration.</param>
  110. /// <exception cref="DirectoryNotFoundException"></exception>
  111. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  112. {
  113. var serverConfig = (ServerConfiguration)newConfig;
  114. var newPath = serverConfig.CertificatePath;
  115. if (!string.IsNullOrWhiteSpace(newPath)
  116. && !string.Equals(Configuration.CertificatePath ?? string.Empty, newPath))
  117. {
  118. // Validate
  119. if (!File.Exists(newPath))
  120. {
  121. throw new FileNotFoundException(string.Format("Certificate file '{0}' does not exist.", newPath));
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Validates the metadata path.
  127. /// </summary>
  128. /// <param name="newConfig">The new configuration.</param>
  129. /// <exception cref="DirectoryNotFoundException"></exception>
  130. private void ValidateMetadataPath(ServerConfiguration newConfig)
  131. {
  132. var newPath = newConfig.MetadataPath;
  133. if (!string.IsNullOrWhiteSpace(newPath)
  134. && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
  135. {
  136. // Validate
  137. if (!Directory.Exists(newPath))
  138. {
  139. throw new FileNotFoundException(string.Format("{0} does not exist.", newPath));
  140. }
  141. EnsureWriteAccess(newPath);
  142. }
  143. }
  144. public bool SetOptimalValues()
  145. {
  146. var config = Configuration;
  147. var changed = false;
  148. if (!config.EnableCaseSensitiveItemIds)
  149. {
  150. config.EnableCaseSensitiveItemIds = true;
  151. changed = true;
  152. }
  153. if (!config.SkipDeserializationForBasicTypes)
  154. {
  155. config.SkipDeserializationForBasicTypes = true;
  156. changed = true;
  157. }
  158. if (!config.EnableSimpleArtistDetection)
  159. {
  160. config.EnableSimpleArtistDetection = true;
  161. changed = true;
  162. }
  163. if (!config.EnableNormalizedItemByNameIds)
  164. {
  165. config.EnableNormalizedItemByNameIds = true;
  166. changed = true;
  167. }
  168. if (!config.DisableLiveTvChannelUserDataName)
  169. {
  170. config.DisableLiveTvChannelUserDataName = true;
  171. changed = true;
  172. }
  173. if (!config.EnableNewOmdbSupport)
  174. {
  175. config.EnableNewOmdbSupport = true;
  176. changed = true;
  177. }
  178. if (!config.CameraUploadUpgraded)
  179. {
  180. config.CameraUploadUpgraded = true;
  181. changed = true;
  182. }
  183. if (!config.CollectionsUpgraded)
  184. {
  185. config.CollectionsUpgraded = true;
  186. changed = true;
  187. }
  188. return changed;
  189. }
  190. }
  191. }