ServerConfigurationManager.cs 7.5 KB

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