ServerConfigurationManager.cs 7.3 KB

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