ServerConfigurationManager.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. GetInternalMetadataPath() :
  92. Configuration.MetadataPath;
  93. }
  94. private string GetInternalMetadataPath()
  95. {
  96. if (Configuration.EnableStandaloneMetadata)
  97. {
  98. return Path.Combine(ApplicationPaths.ProgramDataPath, "metadata");
  99. }
  100. return null;
  101. }
  102. /// <summary>
  103. /// Updates the transcoding temporary path.
  104. /// </summary>
  105. private void UpdateTranscodingTempPath()
  106. {
  107. var encodingConfig = this.GetConfiguration<EncodingOptions>("encoding");
  108. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(encodingConfig.TranscodingTempPath) ?
  109. null :
  110. encodingConfig.TranscodingTempPath;
  111. }
  112. protected override void OnNamedConfigurationUpdated(string key, object configuration)
  113. {
  114. base.OnNamedConfigurationUpdated(key, configuration);
  115. if (string.Equals(key, "encoding", StringComparison.OrdinalIgnoreCase))
  116. {
  117. UpdateTranscodingTempPath();
  118. }
  119. }
  120. /// <summary>
  121. /// Replaces the configuration.
  122. /// </summary>
  123. /// <param name="newConfiguration">The new configuration.</param>
  124. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  125. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  126. {
  127. var newConfig = (ServerConfiguration)newConfiguration;
  128. ValidateItemByNamePath(newConfig);
  129. ValidatePathSubstitutions(newConfig);
  130. ValidateMetadataPath(newConfig);
  131. EventHelper.FireEventIfNotNull(ConfigurationUpdating, this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig }, Logger);
  132. base.ReplaceConfiguration(newConfiguration);
  133. }
  134. private void ValidatePathSubstitutions(ServerConfiguration newConfig)
  135. {
  136. foreach (var map in newConfig.PathSubstitutions)
  137. {
  138. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  139. {
  140. throw new ArgumentException("Invalid path substitution");
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Replaces the item by name path.
  146. /// </summary>
  147. /// <param name="newConfig">The new configuration.</param>
  148. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  149. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  150. {
  151. var newPath = newConfig.ItemsByNamePath;
  152. if (!string.IsNullOrWhiteSpace(newPath)
  153. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  154. {
  155. // Validate
  156. if (!Directory.Exists(newPath))
  157. {
  158. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  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 (!Directory.Exists(newPath))
  175. {
  176. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  177. }
  178. }
  179. }
  180. public void DisableMetadataService(string service)
  181. {
  182. DisableMetadataService(typeof(Movie), Configuration, service);
  183. DisableMetadataService(typeof(Episode), Configuration, service);
  184. DisableMetadataService(typeof(Series), Configuration, service);
  185. DisableMetadataService(typeof(Season), Configuration, service);
  186. DisableMetadataService(typeof(MusicArtist), Configuration, service);
  187. DisableMetadataService(typeof(MusicAlbum), Configuration, service);
  188. DisableMetadataService(typeof(MusicVideo), Configuration, service);
  189. DisableMetadataService(typeof(Video), Configuration, service);
  190. }
  191. private void DisableMetadataService(Type type, ServerConfiguration config, string service)
  192. {
  193. var options = GetMetadataOptions(type, config);
  194. if (!options.DisabledMetadataSavers.Contains(service, StringComparer.OrdinalIgnoreCase))
  195. {
  196. var list = options.DisabledMetadataSavers.ToList();
  197. list.Add(service);
  198. options.DisabledMetadataSavers = list.ToArray();
  199. }
  200. }
  201. private MetadataOptions GetMetadataOptions(Type type, ServerConfiguration config)
  202. {
  203. var options = config.MetadataOptions
  204. .FirstOrDefault(i => string.Equals(i.ItemType, type.Name, StringComparison.OrdinalIgnoreCase));
  205. if (options == null)
  206. {
  207. var list = config.MetadataOptions.ToList();
  208. options = new MetadataOptions
  209. {
  210. ItemType = type.Name
  211. };
  212. list.Add(options);
  213. config.MetadataOptions = list.ToArray();
  214. }
  215. return options;
  216. }
  217. }
  218. }