ServerConfigurationManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using Emby.Server.Implementations.AppBase;
  5. using Jellyfin.Data.Events;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Controller;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Model.Configuration;
  10. using MediaBrowser.Model.Serialization;
  11. using Microsoft.Extensions.Logging;
  12. namespace Emby.Server.Implementations.Configuration
  13. {
  14. /// <summary>
  15. /// Class ServerConfigurationManager.
  16. /// </summary>
  17. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  18. {
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  21. /// </summary>
  22. /// <param name="applicationPaths">The application paths.</param>
  23. /// <param name="loggerFactory">The logger factory.</param>
  24. /// <param name="xmlSerializer">The XML serializer.</param>
  25. public ServerConfigurationManager(
  26. IApplicationPaths applicationPaths,
  27. ILoggerFactory loggerFactory,
  28. IXmlSerializer xmlSerializer)
  29. : base(applicationPaths, loggerFactory, xmlSerializer)
  30. {
  31. UpdateMetadataPath();
  32. }
  33. /// <summary>
  34. /// Configuration updating event.
  35. /// </summary>
  36. public event EventHandler<GenericEventArgs<ServerConfiguration>>? ConfigurationUpdating;
  37. /// <summary>
  38. /// Gets the type of the configuration.
  39. /// </summary>
  40. /// <value>The type of the configuration.</value>
  41. protected override Type ConfigurationType => typeof(ServerConfiguration);
  42. /// <summary>
  43. /// Gets the application paths.
  44. /// </summary>
  45. /// <value>The application paths.</value>
  46. public IServerApplicationPaths ApplicationPaths => (IServerApplicationPaths)CommonApplicationPaths;
  47. /// <summary>
  48. /// Gets the configuration.
  49. /// </summary>
  50. /// <value>The configuration.</value>
  51. public ServerConfiguration Configuration => (ServerConfiguration)CommonConfiguration;
  52. /// <summary>
  53. /// Called when [configuration updated].
  54. /// </summary>
  55. protected override void OnConfigurationUpdated()
  56. {
  57. UpdateMetadataPath();
  58. base.OnConfigurationUpdated();
  59. }
  60. /// <summary>
  61. /// Updates the metadata path.
  62. /// </summary>
  63. /// <exception cref="UnauthorizedAccessException">If the directory does not exist, and the caller does not have the required permission to create it.</exception>
  64. /// <exception cref="NotSupportedException">If there is a custom path transcoding path specified, but it is invalid.</exception>
  65. /// <exception cref="IOException">If the directory does not exist, and it also could not be created.</exception>
  66. private void UpdateMetadataPath()
  67. {
  68. ((ServerApplicationPaths)ApplicationPaths).InternalMetadataPath = string.IsNullOrWhiteSpace(Configuration.MetadataPath)
  69. ? ApplicationPaths.DefaultInternalMetadataPath
  70. : Configuration.MetadataPath;
  71. Directory.CreateDirectory(ApplicationPaths.InternalMetadataPath);
  72. }
  73. /// <summary>
  74. /// Replaces the configuration.
  75. /// </summary>
  76. /// <param name="newConfiguration">The new configuration.</param>
  77. /// <exception cref="DirectoryNotFoundException">If the configuration path doesn't exist.</exception>
  78. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  79. {
  80. var newConfig = (ServerConfiguration)newConfiguration;
  81. ValidateMetadataPath(newConfig);
  82. ConfigurationUpdating?.Invoke(this, new GenericEventArgs<ServerConfiguration>(newConfig));
  83. base.ReplaceConfiguration(newConfiguration);
  84. }
  85. /// <summary>
  86. /// Validates the metadata path.
  87. /// </summary>
  88. /// <param name="newConfig">The new configuration.</param>
  89. /// <exception cref="DirectoryNotFoundException">The new config path doesn't exist.</exception>
  90. private void ValidateMetadataPath(ServerConfiguration newConfig)
  91. {
  92. var newPath = newConfig.MetadataPath;
  93. if (!string.IsNullOrWhiteSpace(newPath)
  94. && !string.Equals(Configuration.MetadataPath, newPath, StringComparison.Ordinal))
  95. {
  96. if (!Directory.Exists(newPath))
  97. {
  98. throw new DirectoryNotFoundException(
  99. string.Format(
  100. CultureInfo.InvariantCulture,
  101. "{0} does not exist.",
  102. newPath));
  103. }
  104. EnsureWriteAccess(newPath);
  105. }
  106. }
  107. }
  108. }