| | 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 | |
|
| | 9 | | namespace Jellyfin.Server.Migrations.PreStartupRoutines; |
| | 10 | |
|
| | 11 | | /// <inheritdoc /> |
| | 12 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | 13 | | [JellyfinMigration("2025-04-20T02:00:00", nameof(MigrateMusicBrainzTimeout), "A6DCACF4-C057-4Ef9-80D3-61CEF9DDB4F0", Sta |
| | 14 | | public class MigrateMusicBrainzTimeout : IMigrationRoutine |
| | 15 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | 16 | | { |
| | 17 | | private readonly ServerApplicationPaths _applicationPaths; |
| | 18 | | private readonly ILogger<MigrateMusicBrainzTimeout> _logger; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Initializes a new instance of the <see cref="MigrateMusicBrainzTimeout"/> class. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param> |
| | 24 | | /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param> |
| | 25 | | public MigrateMusicBrainzTimeout(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory) |
| | 26 | | { |
| 0 | 27 | | _applicationPaths = applicationPaths; |
| 0 | 28 | | _logger = loggerFactory.CreateLogger<MigrateMusicBrainzTimeout>(); |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc /> |
| | 32 | | public void Perform() |
| | 33 | | { |
| 0 | 34 | | string path = Path.Combine(_applicationPaths.PluginConfigurationsPath, "Jellyfin.Plugin.MusicBrainz.xml"); |
| 0 | 35 | | if (!File.Exists(path)) |
| | 36 | | { |
| 0 | 37 | | _logger.LogDebug("No MusicBrainz plugin configuration file found, skipping"); |
| 0 | 38 | | return; |
| | 39 | | } |
| | 40 | |
|
| 0 | 41 | | var oldPluginConfiguration = ReadOld(path); |
| | 42 | |
|
| 0 | 43 | | if (oldPluginConfiguration is not null) |
| | 44 | | { |
| 0 | 45 | | var newPluginConfiguration = new PluginConfiguration |
| 0 | 46 | | { |
| 0 | 47 | | Server = oldPluginConfiguration.Server, |
| 0 | 48 | | ReplaceArtistName = oldPluginConfiguration.ReplaceArtistName |
| 0 | 49 | | }; |
| 0 | 50 | | var newRateLimit = oldPluginConfiguration.RateLimit / 1000.0; |
| 0 | 51 | | newPluginConfiguration.RateLimit = newRateLimit < 1.0 ? 1.0 : newRateLimit; |
| 0 | 52 | | WriteNew(path, newPluginConfiguration); |
| | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | private OldMusicBrainzConfiguration? ReadOld(string path) |
| | 57 | | { |
| 0 | 58 | | using var xmlReader = XmlReader.Create(path); |
| 0 | 59 | | var serverConfigSerializer = new XmlSerializer(typeof(OldMusicBrainzConfiguration), new XmlRootAttribute("Plugin |
| 0 | 60 | | return serverConfigSerializer.Deserialize(xmlReader) as OldMusicBrainzConfiguration; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private void WriteNew(string path, PluginConfiguration newPluginConfiguration) |
| | 64 | | { |
| 0 | 65 | | var pluginConfigurationSerializer = new XmlSerializer(typeof(PluginConfiguration), new XmlRootAttribute("PluginC |
| 0 | 66 | | var xmlWriterSettings = new XmlWriterSettings { Indent = true }; |
| 0 | 67 | | using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings); |
| 0 | 68 | | pluginConfigurationSerializer.Serialize(xmlWriter, newPluginConfiguration); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | #pragma warning disable |
| | 72 | | public sealed class OldMusicBrainzConfiguration |
| | 73 | | { |
| 0 | 74 | | private string _server = string.Empty; |
| | 75 | |
|
| | 76 | | private long _rateLimit = 0L; |
| | 77 | |
|
| | 78 | | public string Server |
| | 79 | | { |
| 0 | 80 | | get => _server; |
| 0 | 81 | | set => _server = value.TrimEnd('/'); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | public long RateLimit |
| | 85 | | { |
| 0 | 86 | | get => _rateLimit; |
| 0 | 87 | | set => _rateLimit = value; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public bool ReplaceArtistName { get; set; } |
| | 91 | | } |
| | 92 | | } |