ServerConfigurationManager.cs 9.1 KB

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