| | 1 | | using System; |
| | 2 | | using System.Collections.Concurrent; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.IO; |
| | 6 | | using System.Linq; |
| | 7 | | using MediaBrowser.Common.Configuration; |
| | 8 | | using MediaBrowser.Common.Events; |
| | 9 | | using MediaBrowser.Common.Extensions; |
| | 10 | | using MediaBrowser.Model.Configuration; |
| | 11 | | using MediaBrowser.Model.Serialization; |
| | 12 | | using Microsoft.Extensions.Logging; |
| | 13 | |
|
| | 14 | | namespace Emby.Server.Implementations.AppBase |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Class BaseConfigurationManager. |
| | 18 | | /// </summary> |
| | 19 | | public abstract class BaseConfigurationManager : IConfigurationManager |
| | 20 | | { |
| 22 | 21 | | private readonly ConcurrentDictionary<string, object> _configurations = new(); |
| 22 | 22 | | private readonly object _configurationSyncLock = new(); |
| | 23 | |
|
| 22 | 24 | | private ConfigurationStore[] _configurationStores = Array.Empty<ConfigurationStore>(); |
| 22 | 25 | | private IConfigurationFactory[] _configurationFactories = Array.Empty<IConfigurationFactory>(); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The _configuration. |
| | 29 | | /// </summary> |
| | 30 | | private BaseApplicationConfiguration? _configuration; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Initializes a new instance of the <see cref="BaseConfigurationManager" /> class. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="applicationPaths">The application paths.</param> |
| | 36 | | /// <param name="loggerFactory">The logger factory.</param> |
| | 37 | | /// <param name="xmlSerializer">The XML serializer.</param> |
| | 38 | | protected BaseConfigurationManager( |
| | 39 | | IApplicationPaths applicationPaths, |
| | 40 | | ILoggerFactory loggerFactory, |
| | 41 | | IXmlSerializer xmlSerializer) |
| | 42 | | { |
| 22 | 43 | | CommonApplicationPaths = applicationPaths; |
| 22 | 44 | | XmlSerializer = xmlSerializer; |
| 22 | 45 | | Logger = loggerFactory.CreateLogger<BaseConfigurationManager>(); |
| | 46 | |
|
| 22 | 47 | | UpdateCachePath(); |
| 22 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Occurs when [configuration updated]. |
| | 52 | | /// </summary> |
| | 53 | | public event EventHandler<EventArgs>? ConfigurationUpdated; |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Occurs when [configuration updating]. |
| | 57 | | /// </summary> |
| | 58 | | public event EventHandler<ConfigurationUpdateEventArgs>? NamedConfigurationUpdating; |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Occurs when [named configuration updated]. |
| | 62 | | /// </summary> |
| | 63 | | public event EventHandler<ConfigurationUpdateEventArgs>? NamedConfigurationUpdated; |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Gets the type of the configuration. |
| | 67 | | /// </summary> |
| | 68 | | /// <value>The type of the configuration.</value> |
| | 69 | | protected abstract Type ConfigurationType { get; } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Gets the logger. |
| | 73 | | /// </summary> |
| | 74 | | /// <value>The logger.</value> |
| | 75 | | protected ILogger<BaseConfigurationManager> Logger { get; private set; } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Gets the XML serializer. |
| | 79 | | /// </summary> |
| | 80 | | /// <value>The XML serializer.</value> |
| | 81 | | protected IXmlSerializer XmlSerializer { get; private set; } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Gets the application paths. |
| | 85 | | /// </summary> |
| | 86 | | /// <value>The application paths.</value> |
| | 87 | | public IApplicationPaths CommonApplicationPaths { get; private set; } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Gets or sets the system configuration. |
| | 91 | | /// </summary> |
| | 92 | | /// <value>The configuration.</value> |
| | 93 | | public BaseApplicationConfiguration CommonConfiguration |
| | 94 | | { |
| | 95 | | get |
| | 96 | | { |
| 3254 | 97 | | if (_configuration is not null) |
| | 98 | | { |
| 3232 | 99 | | return _configuration; |
| | 100 | | } |
| | 101 | |
|
| 22 | 102 | | lock (_configurationSyncLock) |
| | 103 | | { |
| 22 | 104 | | if (_configuration is not null) |
| | 105 | | { |
| 0 | 106 | | return _configuration; |
| | 107 | | } |
| | 108 | |
|
| 22 | 109 | | return _configuration = (BaseApplicationConfiguration)ConfigurationHelper.GetXmlConfiguration(Config |
| | 110 | | } |
| 22 | 111 | | } |
| | 112 | |
|
| | 113 | | protected set |
| | 114 | | { |
| 0 | 115 | | _configuration = value; |
| 0 | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Manually pre-loads a factory so that it is available pre system initialisation. |
| | 121 | | /// </summary> |
| | 122 | | /// <typeparam name="T">Class to register.</typeparam> |
| | 123 | | public virtual void RegisterConfiguration<T>() |
| | 124 | | where T : IConfigurationFactory |
| | 125 | | { |
| 0 | 126 | | IConfigurationFactory factory = Activator.CreateInstance<T>(); |
| | 127 | |
|
| 0 | 128 | | if (_configurationFactories is null) |
| | 129 | | { |
| 0 | 130 | | _configurationFactories = [factory]; |
| | 131 | | } |
| | 132 | | else |
| | 133 | | { |
| 0 | 134 | | _configurationFactories = [.._configurationFactories, factory]; |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | _configurationStores = _configurationFactories |
| 0 | 138 | | .SelectMany(i => i.GetConfigurations()) |
| 0 | 139 | | .ToArray(); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Adds parts. |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="factories">The configuration factories.</param> |
| | 146 | | public virtual void AddParts(IEnumerable<IConfigurationFactory> factories) |
| | 147 | | { |
| 22 | 148 | | _configurationFactories = factories.ToArray(); |
| | 149 | |
|
| 22 | 150 | | _configurationStores = _configurationFactories |
| 22 | 151 | | .SelectMany(i => i.GetConfigurations()) |
| 22 | 152 | | .ToArray(); |
| 22 | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Saves the configuration. |
| | 157 | | /// </summary> |
| | 158 | | public void SaveConfiguration() |
| | 159 | | { |
| 40 | 160 | | Logger.LogInformation("Saving system configuration"); |
| 40 | 161 | | var path = CommonApplicationPaths.SystemConfigurationFilePath; |
| | 162 | |
|
| 40 | 163 | | Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't be |
| | 164 | |
|
| 40 | 165 | | lock (_configurationSyncLock) |
| | 166 | | { |
| 40 | 167 | | XmlSerializer.SerializeToFile(CommonConfiguration, path); |
| 40 | 168 | | } |
| | 169 | |
|
| 40 | 170 | | OnConfigurationUpdated(); |
| 40 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Called when [configuration updated]. |
| | 175 | | /// </summary> |
| | 176 | | protected virtual void OnConfigurationUpdated() |
| | 177 | | { |
| 40 | 178 | | UpdateCachePath(); |
| | 179 | |
|
| 40 | 180 | | EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger); |
| 40 | 181 | | } |
| | 182 | |
|
| | 183 | | /// <summary> |
| | 184 | | /// Replaces the configuration. |
| | 185 | | /// </summary> |
| | 186 | | /// <param name="newConfiguration">The new configuration.</param> |
| | 187 | | /// <exception cref="ArgumentNullException"><c>newConfiguration</c> is <c>null</c>.</exception> |
| | 188 | | public virtual void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) |
| | 189 | | { |
| 0 | 190 | | ArgumentNullException.ThrowIfNull(newConfiguration); |
| | 191 | |
|
| 0 | 192 | | ValidateCachePath(newConfiguration); |
| | 193 | |
|
| 0 | 194 | | CommonConfiguration = newConfiguration; |
| 0 | 195 | | SaveConfiguration(); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | /// <summary> |
| | 199 | | /// Updates the items by name path. |
| | 200 | | /// </summary> |
| | 201 | | private void UpdateCachePath() |
| | 202 | | { |
| | 203 | | string cachePath; |
| | 204 | |
|
| | 205 | | // If the configuration file has no entry (i.e. not set in UI) |
| 62 | 206 | | if (string.IsNullOrWhiteSpace(CommonConfiguration.CachePath)) |
| | 207 | | { |
| | 208 | | // If the current live configuration has no entry (i.e. not set on CLI/envvars, during startup) |
| 62 | 209 | | if (string.IsNullOrWhiteSpace(((BaseApplicationPaths)CommonApplicationPaths).CachePath)) |
| | 210 | | { |
| | 211 | | // Set cachePath to a default value under ProgramDataPath |
| 0 | 212 | | cachePath = Path.Combine(((BaseApplicationPaths)CommonApplicationPaths).ProgramDataPath, "cache"); |
| | 213 | | } |
| | 214 | | else |
| | 215 | | { |
| | 216 | | // Set cachePath to the existing live value; will require restart if UI value is removed (but not re |
| | 217 | | // TODO: Figure out how to re-grab this from the CLI/envvars while running |
| 62 | 218 | | cachePath = ((BaseApplicationPaths)CommonApplicationPaths).CachePath; |
| | 219 | | } |
| | 220 | | } |
| | 221 | | else |
| | 222 | | { |
| | 223 | | // Set cachePath to the new UI-set value |
| 0 | 224 | | cachePath = CommonConfiguration.CachePath; |
| | 225 | | } |
| | 226 | |
|
| 62 | 227 | | Logger.LogInformation("Setting cache path: {Path}", cachePath); |
| 62 | 228 | | ((BaseApplicationPaths)CommonApplicationPaths).CachePath = cachePath; |
| 62 | 229 | | } |
| | 230 | |
|
| | 231 | | /// <summary> |
| | 232 | | /// Replaces the cache path. |
| | 233 | | /// </summary> |
| | 234 | | /// <param name="newConfig">The new configuration.</param> |
| | 235 | | /// <exception cref="DirectoryNotFoundException">The new cache path doesn't exist.</exception> |
| | 236 | | private void ValidateCachePath(BaseApplicationConfiguration newConfig) |
| | 237 | | { |
| 0 | 238 | | var newPath = newConfig.CachePath; |
| | 239 | |
|
| 0 | 240 | | if (!string.IsNullOrWhiteSpace(newPath) |
| 0 | 241 | | && !string.Equals(CommonConfiguration.CachePath ?? string.Empty, newPath, StringComparison.Ordinal)) |
| | 242 | | { |
| | 243 | | // Validate |
| 0 | 244 | | if (!Directory.Exists(newPath)) |
| | 245 | | { |
| 0 | 246 | | throw new DirectoryNotFoundException( |
| 0 | 247 | | string.Format( |
| 0 | 248 | | CultureInfo.InvariantCulture, |
| 0 | 249 | | "{0} does not exist.", |
| 0 | 250 | | newPath)); |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | EnsureWriteAccess(newPath); |
| | 254 | | } |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | /// <summary> |
| | 258 | | /// Ensures that we have write access to the path. |
| | 259 | | /// </summary> |
| | 260 | | /// <param name="path">The path.</param> |
| | 261 | | protected void EnsureWriteAccess(string path) |
| | 262 | | { |
| 0 | 263 | | var file = Path.Combine(path, Guid.NewGuid().ToString()); |
| 0 | 264 | | File.WriteAllText(file, string.Empty); |
| 0 | 265 | | File.Delete(file); |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | private string GetConfigurationFile(string key) |
| | 269 | | { |
| 71 | 270 | | return Path.Combine(CommonApplicationPaths.ConfigurationDirectoryPath, key.ToLowerInvariant() + ".xml"); |
| | 271 | | } |
| | 272 | |
|
| | 273 | | /// <inheritdoc /> |
| | 274 | | public object GetConfiguration(string key) |
| | 275 | | { |
| 1057 | 276 | | return _configurations.GetOrAdd( |
| 1057 | 277 | | key, |
| 1057 | 278 | | static (k, configurationManager) => |
| 1057 | 279 | | { |
| 1057 | 280 | | var file = configurationManager.GetConfigurationFile(k); |
| 1057 | 281 | |
|
| 1057 | 282 | | var configurationInfo = Array.Find( |
| 1057 | 283 | | configurationManager._configurationStores, |
| 1057 | 284 | | i => string.Equals(i.Key, k, StringComparison.OrdinalIgnoreCase)); |
| 1057 | 285 | |
|
| 1057 | 286 | | if (configurationInfo is null) |
| 1057 | 287 | | { |
| 1057 | 288 | | throw new ResourceNotFoundException("Configuration with key " + k + " not found."); |
| 1057 | 289 | | } |
| 1057 | 290 | |
|
| 1057 | 291 | | var configurationType = configurationInfo.ConfigurationType; |
| 1057 | 292 | |
|
| 1057 | 293 | | lock (configurationManager._configurationSyncLock) |
| 1057 | 294 | | { |
| 1057 | 295 | | return configurationManager.LoadConfiguration(file, configurationType); |
| 1057 | 296 | | } |
| 1057 | 297 | | }, |
| 1057 | 298 | | this); |
| | 299 | | } |
| | 300 | |
|
| | 301 | | private object LoadConfiguration(string path, Type configurationType) |
| | 302 | | { |
| | 303 | | try |
| | 304 | | { |
| 70 | 305 | | if (File.Exists(path)) |
| | 306 | | { |
| 0 | 307 | | return XmlSerializer.DeserializeFromFile(configurationType, path); |
| | 308 | | } |
| 70 | 309 | | } |
| 0 | 310 | | catch (Exception ex) when (ex is not IOException) |
| | 311 | | { |
| 0 | 312 | | Logger.LogError(ex, "Error loading configuration file: {Path}", path); |
| 0 | 313 | | } |
| | 314 | |
|
| 70 | 315 | | return Activator.CreateInstance(configurationType) |
| 70 | 316 | | ?? throw new InvalidOperationException("Configuration type can't be Nullable<T>."); |
| 0 | 317 | | } |
| | 318 | |
|
| | 319 | | /// <inheritdoc /> |
| | 320 | | public void SaveConfiguration(string key, object configuration) |
| | 321 | | { |
| 1 | 322 | | var configurationStore = GetConfigurationStore(key); |
| 1 | 323 | | var configurationType = configurationStore.ConfigurationType; |
| | 324 | |
|
| 1 | 325 | | if (configuration.GetType() != configurationType) |
| | 326 | | { |
| 0 | 327 | | throw new ArgumentException("Expected configuration type is " + configurationType.Name); |
| | 328 | | } |
| | 329 | |
|
| 1 | 330 | | if (configurationStore is IValidatingConfiguration validatingStore) |
| | 331 | | { |
| 0 | 332 | | var currentConfiguration = GetConfiguration(key); |
| | 333 | |
|
| 0 | 334 | | validatingStore.Validate(currentConfiguration, configuration); |
| | 335 | | } |
| | 336 | |
|
| 1 | 337 | | NamedConfigurationUpdating?.Invoke(this, new ConfigurationUpdateEventArgs(key, configuration)); |
| | 338 | |
|
| 1 | 339 | | _configurations.AddOrUpdate(key, configuration, (_, _) => configuration); |
| | 340 | |
|
| 1 | 341 | | var path = GetConfigurationFile(key); |
| 1 | 342 | | Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't be |
| | 343 | |
|
| 1 | 344 | | lock (_configurationSyncLock) |
| | 345 | | { |
| 1 | 346 | | XmlSerializer.SerializeToFile(configuration, path); |
| 1 | 347 | | } |
| | 348 | |
|
| 1 | 349 | | OnNamedConfigurationUpdated(key, configuration); |
| 1 | 350 | | } |
| | 351 | |
|
| | 352 | | /// <summary> |
| | 353 | | /// Event handler for when a named configuration has been updated. |
| | 354 | | /// </summary> |
| | 355 | | /// <param name="key">The key of the configuration.</param> |
| | 356 | | /// <param name="configuration">The old configuration.</param> |
| | 357 | | protected virtual void OnNamedConfigurationUpdated(string key, object configuration) |
| | 358 | | { |
| 1 | 359 | | NamedConfigurationUpdated?.Invoke(this, new ConfigurationUpdateEventArgs(key, configuration)); |
| 1 | 360 | | } |
| | 361 | |
|
| | 362 | | /// <inheritdoc /> |
| | 363 | | public ConfigurationStore[] GetConfigurationStores() |
| | 364 | | { |
| 1 | 365 | | return _configurationStores; |
| | 366 | | } |
| | 367 | |
|
| | 368 | | /// <inheritdoc /> |
| | 369 | | public Type GetConfigurationType(string key) |
| | 370 | | { |
| 0 | 371 | | return GetConfigurationStore(key) |
| 0 | 372 | | .ConfigurationType; |
| | 373 | | } |
| | 374 | |
|
| | 375 | | private ConfigurationStore GetConfigurationStore(string key) |
| | 376 | | { |
| 1 | 377 | | return _configurationStores |
| 1 | 378 | | .First(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)); |
| | 379 | | } |
| | 380 | | } |
| | 381 | | } |