ServerConfigurationManager.cs 8.7 KB

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