ServerConfigurationManager.cs 6.0 KB

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