ServerConfigurationManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. base.ReplaceConfiguration(newConfiguration);
  90. }
  91. /// <summary>
  92. /// Replaces the item by name path.
  93. /// </summary>
  94. /// <param name="newConfig">The new configuration.</param>
  95. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  96. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  97. {
  98. var newPath = newConfig.ItemsByNamePath;
  99. if (!string.IsNullOrWhiteSpace(newPath)
  100. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  101. {
  102. // Validate
  103. if (!Directory.Exists(newPath))
  104. {
  105. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// Validates the transcoding temporary path.
  111. /// </summary>
  112. /// <param name="newConfig">The new configuration.</param>
  113. /// <exception cref="DirectoryNotFoundException"></exception>
  114. private void ValidateTranscodingTempPath(ServerConfiguration newConfig)
  115. {
  116. var newPath = newConfig.TranscodingTempPath;
  117. if (!string.IsNullOrWhiteSpace(newPath)
  118. && !string.Equals(Configuration.TranscodingTempPath ?? string.Empty, newPath))
  119. {
  120. // Validate
  121. if (!Directory.Exists(newPath))
  122. {
  123. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  124. }
  125. }
  126. }
  127. }
  128. }