ServerConfigurationManager.cs 8.0 KB

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