ServerConfigurationManager.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Events;
  3. using MediaBrowser.Common.Implementations.Configuration;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.Audio;
  8. using MediaBrowser.Controller.Entities.Movies;
  9. using MediaBrowser.Controller.Entities.TV;
  10. using MediaBrowser.Model.Configuration;
  11. using MediaBrowser.Model.Events;
  12. using MediaBrowser.Model.Logging;
  13. using MediaBrowser.Model.Serialization;
  14. using System;
  15. using System.IO;
  16. using System.Linq;
  17. namespace MediaBrowser.Server.Implementations.Configuration
  18. {
  19. /// <summary>
  20. /// Class ServerConfigurationManager
  21. /// </summary>
  22. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  23. {
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  26. /// </summary>
  27. /// <param name="applicationPaths">The application paths.</param>
  28. /// <param name="logManager">The log manager.</param>
  29. /// <param name="xmlSerializer">The XML serializer.</param>
  30. public ServerConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer)
  31. : base(applicationPaths, logManager, xmlSerializer)
  32. {
  33. UpdateItemsByNamePath();
  34. UpdateTranscodingTempPath();
  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. UpdateTranscodingTempPath();
  69. UpdateMetadataPath();
  70. base.OnConfigurationUpdated();
  71. }
  72. /// <summary>
  73. /// Updates the items by name path.
  74. /// </summary>
  75. private void UpdateItemsByNamePath()
  76. {
  77. ((ServerApplicationPaths)ApplicationPaths).ItemsByNamePath = string.IsNullOrEmpty(Configuration.ItemsByNamePath) ?
  78. null :
  79. Configuration.ItemsByNamePath;
  80. }
  81. /// <summary>
  82. /// Updates the metadata path.
  83. /// </summary>
  84. private void UpdateMetadataPath()
  85. {
  86. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = string.IsNullOrEmpty(Configuration.MetadataPath) ?
  87. null :
  88. Configuration.MetadataPath;
  89. }
  90. /// <summary>
  91. /// Updates the transcoding temporary path.
  92. /// </summary>
  93. private void UpdateTranscodingTempPath()
  94. {
  95. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(Configuration.TranscodingTempPath) ?
  96. null :
  97. Configuration.TranscodingTempPath;
  98. }
  99. /// <summary>
  100. /// Replaces the configuration.
  101. /// </summary>
  102. /// <param name="newConfiguration">The new configuration.</param>
  103. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  104. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  105. {
  106. var newConfig = (ServerConfiguration)newConfiguration;
  107. ValidateItemByNamePath(newConfig);
  108. ValidateTranscodingTempPath(newConfig);
  109. ValidatePathSubstitutions(newConfig);
  110. ValidateMetadataPath(newConfig);
  111. EventHelper.FireEventIfNotNull(ConfigurationUpdating, this, new GenericEventArgs<ServerConfiguration> { Argument = newConfig }, Logger);
  112. base.ReplaceConfiguration(newConfiguration);
  113. }
  114. private void ValidatePathSubstitutions(ServerConfiguration newConfig)
  115. {
  116. foreach (var map in newConfig.PathSubstitutions)
  117. {
  118. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  119. {
  120. throw new ArgumentException("Invalid path substitution");
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// Replaces the item by name path.
  126. /// </summary>
  127. /// <param name="newConfig">The new configuration.</param>
  128. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  129. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  130. {
  131. var newPath = newConfig.ItemsByNamePath;
  132. if (!string.IsNullOrWhiteSpace(newPath)
  133. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  134. {
  135. // Validate
  136. if (!Directory.Exists(newPath))
  137. {
  138. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// Validates the transcoding temporary path.
  144. /// </summary>
  145. /// <param name="newConfig">The new configuration.</param>
  146. /// <exception cref="DirectoryNotFoundException"></exception>
  147. private void ValidateTranscodingTempPath(ServerConfiguration newConfig)
  148. {
  149. var newPath = newConfig.TranscodingTempPath;
  150. if (!string.IsNullOrWhiteSpace(newPath)
  151. && !string.Equals(Configuration.TranscodingTempPath ?? string.Empty, newPath))
  152. {
  153. // Validate
  154. if (!Directory.Exists(newPath))
  155. {
  156. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  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 (!Directory.Exists(newPath))
  173. {
  174. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  175. }
  176. }
  177. }
  178. public void DisableMetadataService(string service)
  179. {
  180. DisableMetadataService(typeof(Movie), Configuration, service);
  181. DisableMetadataService(typeof(Episode), Configuration, service);
  182. DisableMetadataService(typeof(Series), Configuration, service);
  183. DisableMetadataService(typeof(Season), Configuration, service);
  184. DisableMetadataService(typeof(MusicArtist), Configuration, service);
  185. DisableMetadataService(typeof(MusicAlbum), Configuration, service);
  186. DisableMetadataService(typeof(MusicVideo), Configuration, service);
  187. DisableMetadataService(typeof(Video), Configuration, service);
  188. }
  189. private void DisableMetadataService(Type type, ServerConfiguration config, string service)
  190. {
  191. var options = GetMetadataOptions(type, config);
  192. if (!options.DisabledMetadataSavers.Contains(service, StringComparer.OrdinalIgnoreCase))
  193. {
  194. var list = options.DisabledMetadataSavers.ToList();
  195. list.Add(service);
  196. options.DisabledMetadataSavers = list.ToArray();
  197. }
  198. }
  199. private MetadataOptions GetMetadataOptions(Type type, ServerConfiguration config)
  200. {
  201. var options = config.MetadataOptions
  202. .FirstOrDefault(i => string.Equals(i.ItemType, type.Name, StringComparison.OrdinalIgnoreCase));
  203. if (options == null)
  204. {
  205. var list = config.MetadataOptions.ToList();
  206. options = new MetadataOptions
  207. {
  208. ItemType = type.Name
  209. };
  210. list.Add(options);
  211. config.MetadataOptions = list.ToArray();
  212. }
  213. return options;
  214. }
  215. }
  216. }