ServerConfigurationManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. }
  29. /// <summary>
  30. /// Gets the type of the configuration.
  31. /// </summary>
  32. /// <value>The type of the configuration.</value>
  33. protected override Type ConfigurationType
  34. {
  35. get { return typeof(ServerConfiguration); }
  36. }
  37. /// <summary>
  38. /// Gets the application paths.
  39. /// </summary>
  40. /// <value>The application paths.</value>
  41. public IServerApplicationPaths ApplicationPaths
  42. {
  43. get { return (IServerApplicationPaths)CommonApplicationPaths; }
  44. }
  45. /// <summary>
  46. /// Gets the configuration.
  47. /// </summary>
  48. /// <value>The configuration.</value>
  49. public ServerConfiguration Configuration
  50. {
  51. get { return (ServerConfiguration)CommonConfiguration; }
  52. }
  53. /// <summary>
  54. /// Called when [configuration updated].
  55. /// </summary>
  56. protected override void OnConfigurationUpdated()
  57. {
  58. UpdateItemsByNamePath();
  59. UpdateTranscodingTempPath();
  60. base.OnConfigurationUpdated();
  61. }
  62. /// <summary>
  63. /// Updates the items by name path.
  64. /// </summary>
  65. private void UpdateItemsByNamePath()
  66. {
  67. ((ServerApplicationPaths) ApplicationPaths).ItemsByNamePath = string.IsNullOrEmpty(Configuration.ItemsByNamePath) ?
  68. null :
  69. Configuration.ItemsByNamePath;
  70. }
  71. /// <summary>
  72. /// Updates the transcoding temporary path.
  73. /// </summary>
  74. private void UpdateTranscodingTempPath()
  75. {
  76. ((ServerApplicationPaths)ApplicationPaths).TranscodingTempPath = string.IsNullOrEmpty(Configuration.TranscodingTempPath) ?
  77. null :
  78. Configuration.TranscodingTempPath;
  79. }
  80. /// <summary>
  81. /// Replaces the configuration.
  82. /// </summary>
  83. /// <param name="newConfiguration">The new configuration.</param>
  84. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  85. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  86. {
  87. var newConfig = (ServerConfiguration) newConfiguration;
  88. ValidateItemByNamePath(newConfig);
  89. ValidateTranscodingTempPath(newConfig);
  90. ValidatePathSubstitutions(newConfig);
  91. base.ReplaceConfiguration(newConfiguration);
  92. }
  93. private void ValidatePathSubstitutions(ServerConfiguration newConfig)
  94. {
  95. foreach (var map in newConfig.PathSubstitutions)
  96. {
  97. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  98. {
  99. throw new ArgumentException("Invalid path substitution");
  100. }
  101. if (!map.From.EndsWith(":\\") && !map.From.EndsWith(":/"))
  102. {
  103. map.From = map.From.TrimEnd('/').TrimEnd('\\');
  104. }
  105. if (!map.To.EndsWith(":\\") && !map.To.EndsWith(":/"))
  106. {
  107. map.To = map.To.TrimEnd('/').TrimEnd('\\');
  108. }
  109. if (string.IsNullOrWhiteSpace(map.From) || string.IsNullOrWhiteSpace(map.To))
  110. {
  111. throw new ArgumentException("Invalid path substitution");
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Replaces the item by name path.
  117. /// </summary>
  118. /// <param name="newConfig">The new configuration.</param>
  119. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  120. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  121. {
  122. var newPath = newConfig.ItemsByNamePath;
  123. if (!string.IsNullOrWhiteSpace(newPath)
  124. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  125. {
  126. // Validate
  127. if (!Directory.Exists(newPath))
  128. {
  129. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// Validates the transcoding temporary path.
  135. /// </summary>
  136. /// <param name="newConfig">The new configuration.</param>
  137. /// <exception cref="DirectoryNotFoundException"></exception>
  138. private void ValidateTranscodingTempPath(ServerConfiguration newConfig)
  139. {
  140. var newPath = newConfig.TranscodingTempPath;
  141. if (!string.IsNullOrWhiteSpace(newPath)
  142. && !string.Equals(Configuration.TranscodingTempPath ?? string.Empty, newPath))
  143. {
  144. // Validate
  145. if (!Directory.Exists(newPath))
  146. {
  147. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  148. }
  149. }
  150. }
  151. }
  152. }