MigrateMusicBrainzTimeout.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5. using Emby.Server.Implementations;
  6. using MediaBrowser.Providers.Plugins.MusicBrainz.Configuration;
  7. using Microsoft.Extensions.Logging;
  8. namespace Jellyfin.Server.Migrations.PreStartupRoutines;
  9. /// <inheritdoc />
  10. public class MigrateMusicBrainzTimeout : IMigrationRoutine
  11. {
  12. private readonly ServerApplicationPaths _applicationPaths;
  13. private readonly ILogger<MigrateMusicBrainzTimeout> _logger;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="MigrateMusicBrainzTimeout"/> class.
  16. /// </summary>
  17. /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
  18. /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
  19. public MigrateMusicBrainzTimeout(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
  20. {
  21. _applicationPaths = applicationPaths;
  22. _logger = loggerFactory.CreateLogger<MigrateMusicBrainzTimeout>();
  23. }
  24. /// <inheritdoc />
  25. public Guid Id => Guid.Parse("A6DCACF4-C057-4Ef9-80D3-61CEF9DDB4F0");
  26. /// <inheritdoc />
  27. public string Name => nameof(MigrateMusicBrainzTimeout);
  28. /// <inheritdoc />
  29. public bool PerformOnNewInstall => false;
  30. /// <inheritdoc />
  31. public void Perform()
  32. {
  33. string path = Path.Combine(_applicationPaths.PluginConfigurationsPath, "Jellyfin.Plugin.MusicBrainz.xml");
  34. if (!File.Exists(path))
  35. {
  36. _logger.LogDebug("No MusicBrainz plugin configuration file found, skipping");
  37. return;
  38. }
  39. var serverConfigSerializer = new XmlSerializer(typeof(OldMusicBrainzConfiguration), new XmlRootAttribute("PluginConfiguration"));
  40. using var xmlReader = XmlReader.Create(path);
  41. var oldPluginConfiguration = serverConfigSerializer.Deserialize(xmlReader) as OldMusicBrainzConfiguration;
  42. if (oldPluginConfiguration is not null)
  43. {
  44. var newPluginConfiguration = new PluginConfiguration();
  45. newPluginConfiguration.Server = oldPluginConfiguration.Server;
  46. newPluginConfiguration.ReplaceArtistName = oldPluginConfiguration.ReplaceArtistName;
  47. var newRateLimit = oldPluginConfiguration.RateLimit / 1000.0;
  48. newPluginConfiguration.RateLimit = newRateLimit < 1.0 ? 1.0 : newRateLimit;
  49. var pluginConfigurationSerializer = new XmlSerializer(typeof(PluginConfiguration), new XmlRootAttribute("PluginConfiguration"));
  50. var xmlWriterSettings = new XmlWriterSettings { Indent = true };
  51. using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
  52. pluginConfigurationSerializer.Serialize(xmlWriter, newPluginConfiguration);
  53. }
  54. }
  55. #pragma warning disable
  56. public sealed class OldMusicBrainzConfiguration
  57. {
  58. private string _server = string.Empty;
  59. private long _rateLimit = 0L;
  60. public string Server
  61. {
  62. get => _server;
  63. set => _server = value.TrimEnd('/');
  64. }
  65. public long RateLimit
  66. {
  67. get => _rateLimit;
  68. set => _rateLimit = value;
  69. }
  70. public bool ReplaceArtistName { get; set; }
  71. }
  72. #pragma warning restore
  73. }