ServerConfigurationManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. UpdateTranscodingTempPath();
  60. }
  61. /// <summary>
  62. /// Updates the metadata path.
  63. /// </summary>
  64. private void UpdateMetadataPath()
  65. {
  66. string metadataPath;
  67. if (string.IsNullOrWhiteSpace(Configuration.MetadataPath))
  68. {
  69. metadataPath = GetInternalMetadataPath();
  70. }
  71. else
  72. {
  73. metadataPath = Path.Combine(Configuration.MetadataPath, "metadata");
  74. }
  75. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = metadataPath;
  76. }
  77. private string GetInternalMetadataPath()
  78. {
  79. return Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  80. }
  81. /// <summary>
  82. /// Updates the transcoding temporary path.
  83. /// </summary>
  84. private void UpdateTranscodingTempPath()
  85. {
  86. var encodingConfig = this.GetConfiguration<EncodingOptions>("encoding");
  87. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(encodingConfig.TranscodingTempPath) ?
  88. null :
  89. Path.Combine(encodingConfig.TranscodingTempPath, "transcoding-temp");
  90. }
  91. protected override void OnNamedConfigurationUpdated(string key, object configuration)
  92. {
  93. base.OnNamedConfigurationUpdated(key, configuration);
  94. if (string.Equals(key, "encoding", StringComparison.OrdinalIgnoreCase))
  95. {
  96. UpdateTranscodingTempPath();
  97. }
  98. }
  99. /// <summary>
  100. /// Replaces the configuration.
  101. /// </summary>
  102. /// <param name="newConfiguration">The new configuration.</param>
  103. /// <exception cref="DirectoryNotFoundException"></exception>
  104. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  105. {
  106. var newConfig = (ServerConfiguration)newConfiguration;
  107. ValidateMetadataPath(newConfig);
  108. ValidateSslCertificate(newConfig);
  109. ConfigurationUpdating?.Invoke(this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig });
  110. base.ReplaceConfiguration(newConfiguration);
  111. }
  112. /// <summary>
  113. /// Validates the SSL certificate.
  114. /// </summary>
  115. /// <param name="newConfig">The new configuration.</param>
  116. /// <exception cref="DirectoryNotFoundException"></exception>
  117. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  118. {
  119. var serverConfig = (ServerConfiguration)newConfig;
  120. var newPath = serverConfig.CertificatePath;
  121. if (!string.IsNullOrWhiteSpace(newPath)
  122. && !string.Equals(Configuration.CertificatePath ?? string.Empty, newPath))
  123. {
  124. // Validate
  125. if (!File.Exists(newPath))
  126. {
  127. throw new FileNotFoundException(string.Format("Certificate file '{0}' does not exist.", newPath));
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Validates the metadata path.
  133. /// </summary>
  134. /// <param name="newConfig">The new configuration.</param>
  135. /// <exception cref="DirectoryNotFoundException"></exception>
  136. private void ValidateMetadataPath(ServerConfiguration newConfig)
  137. {
  138. var newPath = newConfig.MetadataPath;
  139. if (!string.IsNullOrWhiteSpace(newPath)
  140. && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
  141. {
  142. // Validate
  143. if (!Directory.Exists(newPath))
  144. {
  145. throw new FileNotFoundException(string.Format("{0} does not exist.", newPath));
  146. }
  147. EnsureWriteAccess(newPath);
  148. }
  149. }
  150. public bool SetOptimalValues()
  151. {
  152. var config = Configuration;
  153. var changed = false;
  154. if (!config.EnableCaseSensitiveItemIds)
  155. {
  156. config.EnableCaseSensitiveItemIds = true;
  157. changed = true;
  158. }
  159. if (!config.SkipDeserializationForBasicTypes)
  160. {
  161. config.SkipDeserializationForBasicTypes = true;
  162. changed = true;
  163. }
  164. if (!config.EnableSimpleArtistDetection)
  165. {
  166. config.EnableSimpleArtistDetection = true;
  167. changed = true;
  168. }
  169. if (!config.EnableNormalizedItemByNameIds)
  170. {
  171. config.EnableNormalizedItemByNameIds = true;
  172. changed = true;
  173. }
  174. if (!config.DisableLiveTvChannelUserDataName)
  175. {
  176. config.DisableLiveTvChannelUserDataName = true;
  177. changed = true;
  178. }
  179. if (!config.EnableNewOmdbSupport)
  180. {
  181. config.EnableNewOmdbSupport = true;
  182. changed = true;
  183. }
  184. if (!config.CameraUploadUpgraded)
  185. {
  186. config.CameraUploadUpgraded = true;
  187. changed = true;
  188. }
  189. if (!config.CollectionsUpgraded)
  190. {
  191. config.CollectionsUpgraded = true;
  192. changed = true;
  193. }
  194. return changed;
  195. }
  196. }
  197. }