ServerConfigurationManager.cs 7.6 KB

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