ServerConfigurationManager.cs 9.2 KB

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