ServerConfigurationManager.cs 9.3 KB

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