ServerConfigurationManager.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System.Collections.Generic;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Events;
  4. using MediaBrowser.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 CommonIO;
  19. namespace MediaBrowser.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 if (Configuration.EnableCustomPathSubFolders)
  87. {
  88. metadataPath = Path.Combine(Configuration.MetadataPath, "metadata");
  89. }
  90. else
  91. {
  92. metadataPath = Configuration.MetadataPath;
  93. }
  94. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = metadataPath;
  95. ((ServerApplicationPaths)ApplicationPaths).ItemsByNamePath = ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath;
  96. }
  97. private string GetInternalMetadataPath()
  98. {
  99. return Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  100. }
  101. /// <summary>
  102. /// Updates the transcoding temporary path.
  103. /// </summary>
  104. private void UpdateTranscodingTempPath()
  105. {
  106. var encodingConfig = this.GetConfiguration<EncodingOptions>("encoding");
  107. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(encodingConfig.TranscodingTempPath) ?
  108. null :
  109. Path.Combine(encodingConfig.TranscodingTempPath, "transcoding-temp");
  110. }
  111. protected override void OnNamedConfigurationUpdated(string key, object configuration)
  112. {
  113. base.OnNamedConfigurationUpdated(key, configuration);
  114. if (string.Equals(key, "encoding", StringComparison.OrdinalIgnoreCase))
  115. {
  116. UpdateTranscodingTempPath();
  117. }
  118. }
  119. /// <summary>
  120. /// Replaces the configuration.
  121. /// </summary>
  122. /// <param name="newConfiguration">The new configuration.</param>
  123. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  124. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  125. {
  126. var newConfig = (ServerConfiguration)newConfiguration;
  127. ValidatePathSubstitutions(newConfig);
  128. ValidateMetadataPath(newConfig);
  129. ValidateSslCertificate(newConfig);
  130. EventHelper.FireEventIfNotNull(ConfigurationUpdating, this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig }, Logger);
  131. base.ReplaceConfiguration(newConfiguration);
  132. }
  133. /// <summary>
  134. /// Validates the SSL certificate.
  135. /// </summary>
  136. /// <param name="newConfig">The new configuration.</param>
  137. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  138. private void ValidateSslCertificate(BaseApplicationConfiguration newConfig)
  139. {
  140. var serverConfig = (ServerConfiguration)newConfig;
  141. var newPath = serverConfig.CertificatePath;
  142. if (!string.IsNullOrWhiteSpace(newPath)
  143. && !string.Equals(Configuration.CertificatePath ?? string.Empty, newPath))
  144. {
  145. // Validate
  146. if (!FileSystem.FileExists(newPath))
  147. {
  148. throw new FileNotFoundException(string.Format("Certificate file '{0}' does not exist.", newPath));
  149. }
  150. }
  151. }
  152. private void ValidatePathSubstitutions(ServerConfiguration newConfig)
  153. {
  154. foreach (var map in newConfig.PathSubstitutions)
  155. {
  156. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  157. {
  158. throw new ArgumentException("Invalid path substitution");
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// Validates the metadata path.
  164. /// </summary>
  165. /// <param name="newConfig">The new configuration.</param>
  166. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  167. private void ValidateMetadataPath(ServerConfiguration newConfig)
  168. {
  169. var newPath = newConfig.MetadataPath;
  170. if (!string.IsNullOrWhiteSpace(newPath)
  171. && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
  172. {
  173. // Validate
  174. if (!FileSystem.DirectoryExists(newPath))
  175. {
  176. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  177. }
  178. EnsureWriteAccess(newPath);
  179. }
  180. }
  181. public void DisableMetadataService(string service)
  182. {
  183. DisableMetadataService(typeof(Movie), Configuration, service);
  184. DisableMetadataService(typeof(Episode), Configuration, service);
  185. DisableMetadataService(typeof(Series), Configuration, service);
  186. DisableMetadataService(typeof(Season), Configuration, service);
  187. DisableMetadataService(typeof(MusicArtist), Configuration, service);
  188. DisableMetadataService(typeof(MusicAlbum), Configuration, service);
  189. DisableMetadataService(typeof(MusicVideo), Configuration, service);
  190. DisableMetadataService(typeof(Video), Configuration, service);
  191. }
  192. private void DisableMetadataService(Type type, ServerConfiguration config, string service)
  193. {
  194. var options = GetMetadataOptions(type, config);
  195. if (!options.DisabledMetadataSavers.Contains(service, StringComparer.OrdinalIgnoreCase))
  196. {
  197. var list = options.DisabledMetadataSavers.ToList();
  198. list.Add(service);
  199. options.DisabledMetadataSavers = list.ToArray();
  200. }
  201. }
  202. private MetadataOptions GetMetadataOptions(Type type, ServerConfiguration config)
  203. {
  204. var options = config.MetadataOptions
  205. .FirstOrDefault(i => string.Equals(i.ItemType, type.Name, StringComparison.OrdinalIgnoreCase));
  206. if (options == null)
  207. {
  208. var list = config.MetadataOptions.ToList();
  209. options = new MetadataOptions
  210. {
  211. ItemType = type.Name
  212. };
  213. list.Add(options);
  214. config.MetadataOptions = list.ToArray();
  215. }
  216. return options;
  217. }
  218. }
  219. }