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