ServerConfigurationManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Emby.Server.Implementations.AppBase;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Events;
  8. using MediaBrowser.Controller;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Entities.Audio;
  12. using MediaBrowser.Controller.Entities.Movies;
  13. using MediaBrowser.Controller.Entities.TV;
  14. using MediaBrowser.Model.Configuration;
  15. using MediaBrowser.Model.Events;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Logging;
  18. using MediaBrowser.Model.Serialization;
  19. namespace Emby.Server.Implementations.Configuration
  20. {
  21. /// <summary>
  22. /// Class ServerConfigurationManager
  23. /// </summary>
  24. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  25. {
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  28. /// </summary>
  29. /// <param name="applicationPaths">The application paths.</param>
  30. /// <param name="logManager">The log manager.</param>
  31. /// <param name="xmlSerializer">The XML serializer.</param>
  32. /// <param name="fileSystem">The file system.</param>
  33. public ServerConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer, IFileSystem fileSystem)
  34. : base(applicationPaths, logManager, xmlSerializer, fileSystem)
  35. {
  36. UpdateMetadataPath();
  37. }
  38. public event EventHandler<GenericEventArgs<ServerConfiguration>> ConfigurationUpdating;
  39. /// <summary>
  40. /// Gets the type of the configuration.
  41. /// </summary>
  42. /// <value>The type of the configuration.</value>
  43. protected override Type ConfigurationType
  44. {
  45. get { return typeof(ServerConfiguration); }
  46. }
  47. /// <summary>
  48. /// Gets the application paths.
  49. /// </summary>
  50. /// <value>The application paths.</value>
  51. public IServerApplicationPaths ApplicationPaths
  52. {
  53. get { return (IServerApplicationPaths)CommonApplicationPaths; }
  54. }
  55. /// <summary>
  56. /// Gets the configuration.
  57. /// </summary>
  58. /// <value>The configuration.</value>
  59. public ServerConfiguration Configuration
  60. {
  61. get { return (ServerConfiguration)CommonConfiguration; }
  62. }
  63. /// <summary>
  64. /// Called when [configuration updated].
  65. /// </summary>
  66. protected override void OnConfigurationUpdated()
  67. {
  68. UpdateMetadataPath();
  69. base.OnConfigurationUpdated();
  70. }
  71. public override void AddParts(IEnumerable<IConfigurationFactory> factories)
  72. {
  73. base.AddParts(factories);
  74. UpdateTranscodingTempPath();
  75. }
  76. /// <summary>
  77. /// Updates the metadata path.
  78. /// </summary>
  79. private void UpdateMetadataPath()
  80. {
  81. string metadataPath;
  82. if (string.IsNullOrWhiteSpace(Configuration.MetadataPath))
  83. {
  84. metadataPath = GetInternalMetadataPath();
  85. }
  86. else
  87. {
  88. metadataPath = Path.Combine(Configuration.MetadataPath, "metadata");
  89. }
  90. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = metadataPath;
  91. ((ServerApplicationPaths)ApplicationPaths).ItemsByNamePath = ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath;
  92. }
  93. private string GetInternalMetadataPath()
  94. {
  95. return Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  96. }
  97. /// <summary>
  98. /// Updates the transcoding temporary path.
  99. /// </summary>
  100. private void UpdateTranscodingTempPath()
  101. {
  102. var encodingConfig = this.GetConfiguration<EncodingOptions>("encoding");
  103. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(encodingConfig.TranscodingTempPath) ?
  104. null :
  105. Path.Combine(encodingConfig.TranscodingTempPath, "transcoding-temp");
  106. }
  107. protected override void OnNamedConfigurationUpdated(string key, object configuration)
  108. {
  109. base.OnNamedConfigurationUpdated(key, configuration);
  110. if (string.Equals(key, "encoding", StringComparison.OrdinalIgnoreCase))
  111. {
  112. UpdateTranscodingTempPath();
  113. }
  114. }
  115. /// <summary>
  116. /// Replaces the configuration.
  117. /// </summary>
  118. /// <param name="newConfiguration">The new configuration.</param>
  119. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  120. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  121. {
  122. var newConfig = (ServerConfiguration)newConfiguration;
  123. ValidateMetadataPath(newConfig);
  124. ValidateSslCertificate(newConfig);
  125. EventHelper.FireEventIfNotNull(ConfigurationUpdating, this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig }, Logger);
  126. base.ReplaceConfiguration(newConfiguration);
  127. }
  128. /// <summary>
  129. /// Validates the SSL certificate.
  130. /// </summary>
  131. /// <param name="newConfig">The new configuration.</param>
  132. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  133. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  134. {
  135. var serverConfig = (ServerConfiguration)newConfig;
  136. var newPath = serverConfig.CertificatePath;
  137. if (!string.IsNullOrWhiteSpace(newPath)
  138. && !string.Equals(Configuration.CertificatePath ?? string.Empty, newPath))
  139. {
  140. // Validate
  141. if (!FileSystem.FileExists(newPath))
  142. {
  143. throw new FileNotFoundException(string.Format("Certificate file '{0}' does not exist.", newPath));
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// Validates the metadata path.
  149. /// </summary>
  150. /// <param name="newConfig">The new configuration.</param>
  151. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  152. private void ValidateMetadataPath(ServerConfiguration newConfig)
  153. {
  154. var newPath = newConfig.MetadataPath;
  155. if (!string.IsNullOrWhiteSpace(newPath)
  156. && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
  157. {
  158. // Validate
  159. if (!FileSystem.DirectoryExists(newPath))
  160. {
  161. throw new FileNotFoundException(string.Format("{0} does not exist.", newPath));
  162. }
  163. EnsureWriteAccess(newPath);
  164. }
  165. }
  166. public void DisableMetadataService(string service)
  167. {
  168. DisableMetadataService(typeof(Movie), Configuration, service);
  169. DisableMetadataService(typeof(Episode), Configuration, service);
  170. DisableMetadataService(typeof(Series), Configuration, service);
  171. DisableMetadataService(typeof(Season), Configuration, service);
  172. DisableMetadataService(typeof(MusicArtist), Configuration, service);
  173. DisableMetadataService(typeof(MusicAlbum), Configuration, service);
  174. DisableMetadataService(typeof(MusicVideo), Configuration, service);
  175. DisableMetadataService(typeof(Video), Configuration, service);
  176. }
  177. private void DisableMetadataService(Type type, ServerConfiguration config, string service)
  178. {
  179. var options = GetMetadataOptions(type, config);
  180. if (!options.DisabledMetadataSavers.Contains(service, StringComparer.OrdinalIgnoreCase))
  181. {
  182. var list = options.DisabledMetadataSavers.ToList();
  183. list.Add(service);
  184. options.DisabledMetadataSavers = list.ToArray();
  185. }
  186. }
  187. private MetadataOptions GetMetadataOptions(Type type, ServerConfiguration config)
  188. {
  189. var options = config.MetadataOptions
  190. .FirstOrDefault(i => string.Equals(i.ItemType, type.Name, StringComparison.OrdinalIgnoreCase));
  191. if (options == null)
  192. {
  193. var list = config.MetadataOptions.ToList();
  194. options = new MetadataOptions
  195. {
  196. ItemType = type.Name
  197. };
  198. list.Add(options);
  199. config.MetadataOptions = list.ToArray();
  200. }
  201. return options;
  202. }
  203. }
  204. }