ServerConfigurationManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. base.OnConfigurationUpdated();
  62. }
  63. /// <summary>
  64. /// Updates the items by name path.
  65. /// </summary>
  66. private void UpdateItemsByNamePath()
  67. {
  68. ((ServerApplicationPaths) ApplicationPaths).ItemsByNamePath = string.IsNullOrEmpty(Configuration.ItemsByNamePath) ?
  69. null :
  70. Configuration.ItemsByNamePath;
  71. }
  72. /// <summary>
  73. /// Updates the metadata path.
  74. /// </summary>
  75. private void UpdateMetadataPath()
  76. {
  77. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = string.IsNullOrEmpty(Configuration.MetadataPath) ?
  78. null :
  79. Configuration.MetadataPath;
  80. }
  81. /// <summary>
  82. /// Updates the transcoding temporary path.
  83. /// </summary>
  84. private void UpdateTranscodingTempPath()
  85. {
  86. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(Configuration.TranscodingTempPath) ?
  87. null :
  88. Configuration.TranscodingTempPath;
  89. }
  90. /// <summary>
  91. /// Replaces the configuration.
  92. /// </summary>
  93. /// <param name="newConfiguration">The new configuration.</param>
  94. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  95. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  96. {
  97. var newConfig = (ServerConfiguration) newConfiguration;
  98. ValidateItemByNamePath(newConfig);
  99. ValidateTranscodingTempPath(newConfig);
  100. ValidatePathSubstitutions(newConfig);
  101. ValidateMetadataPath(newConfig);
  102. base.ReplaceConfiguration(newConfiguration);
  103. }
  104. private void ValidatePathSubstitutions(ServerConfiguration newConfig)
  105. {
  106. foreach (var map in newConfig.PathSubstitutions)
  107. {
  108. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  109. {
  110. throw new ArgumentException("Invalid path substitution");
  111. }
  112. if (!map.From.EndsWith(":\\") && !map.From.EndsWith(":/"))
  113. {
  114. map.From = map.From.TrimEnd('/').TrimEnd('\\');
  115. }
  116. if (!map.To.EndsWith(":\\") && !map.To.EndsWith(":/"))
  117. {
  118. map.To = map.To.TrimEnd('/').TrimEnd('\\');
  119. }
  120. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  121. {
  122. throw new ArgumentException("Invalid path substitution");
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// Replaces the item by name path.
  128. /// </summary>
  129. /// <param name="newConfig">The new configuration.</param>
  130. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  131. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  132. {
  133. var newPath = newConfig.ItemsByNamePath;
  134. if (!string.IsNullOrWhiteSpace(newPath)
  135. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  136. {
  137. // Validate
  138. if (!Directory.Exists(newPath))
  139. {
  140. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Validates the transcoding temporary path.
  146. /// </summary>
  147. /// <param name="newConfig">The new configuration.</param>
  148. /// <exception cref="DirectoryNotFoundException"></exception>
  149. private void ValidateTranscodingTempPath(ServerConfiguration newConfig)
  150. {
  151. var newPath = newConfig.TranscodingTempPath;
  152. if (!string.IsNullOrWhiteSpace(newPath)
  153. && !string.Equals(Configuration.TranscodingTempPath ?? string.Empty, newPath))
  154. {
  155. // Validate
  156. if (!Directory.Exists(newPath))
  157. {
  158. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// Validates the metadata path.
  164. /// </summary>
  165. /// <param name="newConfig">The new configuration.</param>
  166. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  167. private void ValidateMetadataPath(ServerConfiguration newConfig)
  168. {
  169. var newPath = newConfig.MetadataPath;
  170. if (!string.IsNullOrWhiteSpace(newPath)
  171. && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
  172. {
  173. // Validate
  174. if (!Directory.Exists(newPath))
  175. {
  176. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  177. }
  178. }
  179. }
  180. }
  181. }