ServerConfigurationManager.cs 8.7 KB

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