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